WCF: Wrong URI being returned by OperationContext.IncomingMessageProperties.Via

送分小仙女□ 提交于 2019-12-10 21:24:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!