Mar 24, 2010 at 5:59 AM
Edited Mar 24, 2010 at 11:43 AM
This is my List of Modules in HttpContext.Current.ApplicationInstance.Modules
I do not see any Module related to WCF there, and there is no WCF Http Module in web config. As far as I understand, it seems WCF service lives outside ASP.Net albeit it can share things to ASP.Net.
seems to confirm my understanding, as it said "HttpModule extensibility: The WCF hosting infrastructure intercepts WCF requests when the
PostAuthenticateRequest event is raised and does not return processing to the ASP.NET HTTP pipeline. Modules that are coded to intercept requests at later stages of the pipeline do not intercept WCF requests."
However, on the same link further down, it mentions that there is compatibility mode to allow WCF lives as ASP.Net module with
aspNetCompatibilityEnabled in web.config. I managed to test it and it enables the proxy request by adding
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
into web.config. I believe this is alternative way aside than adding custom code, but I don't know whether this one is better or not, as I still stuck with POST problem, wrong regex probably as I used:
RewriteRule ^/Proxy/(.*).svc$ http://otherserver/Application/$1.svc [NC,P,QSA]
And that one is for full string with prefix "/Proxy/" and suffix ".svc", Need something which can modify middle one in POST contents, and hoping that WCF security does not have something which check this part with some hash code/token
verification or anything similar.
Now I am able to use WCF with the URLRewriterProxy. Basically aside above steps (The additional code or the aspNetCompatibility) I need to reimplement new type of rule (which is in my case I name it "InRewriteRule", which is mostly similar to "OutRewriteRule".
This rule is used to modify POST contents, so the WCF POST request
to parameter is replaced with the proxied one. The original URL Rewriter is not able to modify this request, as it can only modify the header, not the contents (which is exist in InputStream property)
Main thing I add:
(in SendRequestToTarget() of
Basically this part of code will buffer request, rewrite the request (using "Manager.RunInputRulesDirectly(context, bufferToTarget); " ) and then do the actual request.
// ContentLength is set to -1 if their is no data to send
if (request.ContentLength >= 0 && !knownVerb.ContentBodyNotAllowed)
StringBuilder requestFromClientContent = new StringBuilder();
int bufferSize = Manager.Configuration.Rewriter.Proxy.RequestSize;
using (Stream bufferStream = new BufferedStream(context.Request.InputStream, Manager.Configuration.Rewriter.Proxy.BufferSize))
byte buffer = new byte[bufferSize];
// make sure that the stream can be read from
int bytesReturned = bufferStream.Read(buffer, 0, bufferSize);
// if not bytes were returned the end of the stream has been reached
// and the loop should exit
if (bytesReturned == 0)
string requestFromClientContentPart = Encoding.UTF8.GetString(buffer, 0, bytesReturned);
byte bufferToTarget = Encoding.UTF8.GetBytes(requestFromClientContent.ToString());
bufferToTarget = Manager.RunInputRulesDirectly(context, bufferToTarget);
request.ContentLength = bufferToTarget.Length;
//Actual Sending of request
Stream requestStream = request.GetRequestStream();
requestStream.Write(bufferToTarget, 0, bufferToTarget.Length);
catch (Exception exc)
Manager.Log("Error on request: " + exc.Message, "Proxy");