我在IIS中托管WCF服务。我在IIS中为该站点设置了多个主机名绑定。但是,在对任何非默认绑定进行请求时,OperationContext.IncomingMessageProperties.Via属性不会报告正确的URL。所报告的URL使用默认绑定的主机名作为基础,并具有相同的路径和查询字符串。

例如,假定以下绑定:

http://subfoo.services.myapp.com (first/default entry)
http://subbar.services.myapp.com


提出以下要求时:http://subbar.services.myapp.com/someservice?id=123

Via属性将请求URI报告为:http://subfoo.services.myapp.com/someservice?id=123

如何获得带有请求的实际主机名的URL?

最佳答案

有可能,只涉及一点点。您需要获取HTTP主机标头,并替换IncomingMessageProperties.Via Uri的主机段。这是一些带有注释的示例代码:

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

07-25 21:29