问题
I am hosting WCF services in IIS. I have multiple hostname bindings set up in IIS for the site. However, when making requests to any of the non-default bindings, the correct url doesn't get reported by the OperationContext.IncomingMessageProperties.Via property. The URL that gets reported uses the default binding's hostname as the base, with same path and querystring.
For example, assuming the following bindings:
http://subfoo.services.myapp.com (first/default entry)
http://subbar.services.myapp.com
When making a request to: http://subbar.services.myapp.com/someservice?id=123
The Via property reports the request URI as: http://subfoo.services.myapp.com/someservice?id=123
How can I get the URL with the actual hostname that was requested?
回答1:
it is possible, just a bit involved. You need to get the HTTP Host Header, and replace the host segment of the IncomingMessageProperties.Via Uri. Here's some sample code with comments:
OperationContext operationContext = OperationContext.Current;
HttpRequestMessageProperty httpRequest = operationContext.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
if (httpRequest != null)
{
// Get the OperationContext request Uri:
Uri viaUri = operationContext.IncomingMessageProperties.Via;
// Get the HTTP Host Header value:
string host = httpRequest.Headers[System.Net.HttpRequestHeader.Host];
// Build a new Uri replacing the host component of the Via Uri:
var uriBuilder = new UriBuilder(viaUri) { Host = host };
// This is the Uri which was requested:
string originalRequestUri = uriBuilder.Uri.AbsoluteUri;
}
HTH :)
来源:https://stackoverflow.com/questions/5236730/wcf-wrong-uri-being-returned-by-operationcontext-incomingmessageproperties-via