【问题标题】:WCF: Wrong URI being returned by OperationContext.IncomingMessageProperties.ViaWCF:OperationContext.IncomingMessageProperties.Via 返回错误的 URI
【发布时间】:2011-03-08 18:31:20
【问题描述】:

我在 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?

【问题讨论】:

  • 你可以发布你的配置 - 显然是在匿名之后?目前尚不清楚默认 URI 或非默认绑定是什么意思。
  • 我指的是 IIS 中的主机头绑定。它似乎将第一个条目视为默认主机名。

标签: c# wcf iis .net-4.0


【解决方案1】:

有可能,只是有点牵扯。您需要获取HTTP Host Header,并替换IncomingMessageProperties.Via Uri 的主机段。下面是一些带有 cmets 的示例代码:

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

【讨论】:

  • 这对我有用,但我需要去掉 UriBuilder 的端口号以替换主机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2019-08-04
  • 1970-01-01
  • 2015-01-02
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多