WCF:错误的URI由OperationContext.IncomingMessageProperties.Via返回错误、URI、WCF、Via

2023-09-08 09:39:31 作者:熱心网友

我在IIS托管WCF服务。我在IIS中设置的站点上的多个主机绑定。然而,在作出请求的任何一个非缺省绑定时,正确的URL没有得到所报告的OperationContext.IncomingMessageProperties.Via属性。这被报告的URL使用默认绑定的主机为基础,以相同的路径和查询字符串。

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

发出请求。当: http://subbar.services.myapp.com/someservice?id=123

威盛属性报告请求URI为: http://subfoo.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?

推荐答案

这是可能的,只是会有点麻烦。你需要得到的HTTP主机头,并更换 IncomingMessageProperties.Via URI的主机段。下面是一些示例code与评论:

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:)

HTH :)