WCF:OperationContext.IncomingMessageProperties.Via返回错误的URI
我在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
: http://subfoo.services.myapp.com/someservice?id=123
id = 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 🙂
- IDisposable实现 – ‘if(disposing)’应该包含什么
- 应该“或”使用.Net4 Hasflags:enum.HasFlag(AccessRights.Read | AccessRights.Write)
- .NET 4.0 MemoryCache性能计数器在哪里?
- 无法解析System.Web.Extensions程序集
- ConcurrentDictionary似乎在删除时不标记GC的元素
- 剪贴板在.NET 3.5和4中表现不同,但为什么呢?
- 在Linq中使用.NET 4动态关键字的好例子?
- 如何从C#中的QueryPerformanceCounter获取刻度?
- 缺少.NET 4.0的引用程序集文件夹