问题描述
我们有一个在 IIS7 中运行的 WCF (.NET 3.5 SP1) 服务,它被标记为允许 ASP.NET 兼容模式,因此我们可以访问 HttpContext.Current
:
We have a WCF (.NET 3.5 SP1) service running in IIS7, which is marked to allow ASP.NET compatibility mode so we can access HttpContext.Current
:
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
然而,当试图访问任何服务器变量时,它会抛出一个ArgumentException
:
However, when trying to access any server variable it throws an ArgumentException
:
System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress()
at System.Web.HttpRequest.get_UserHostAddress()
例如以下任何行都抛出此异常:
e.g. Any of the following lines throw this exception:
ha = HttpContext.Current.Request.UserHostAddress;
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
us = HttpContext.Current.Request.UserAgent;
这似乎是内部框架调用的结果,这意味着调用代码没有问题.
This appears to be the result of an internal framework call, which means the calling code is not at fault.
大概是我们的配置中有什么东西导致了这个问题,但我不知道是什么.有任何想法吗?或者我还有什么办法可以检索这三位数据(不幸的是,它们似乎没有被 WCF 本地公开).
Presumably there is something in our configuration that is causing this problem, but I don't know what. Any ideas? Or any ideas how else I can retrieve these three bits of data (they don't appear to be exposed by WCF natively, unfortunately).
推荐答案
原来问题是操作标有...
Turns out the problem is that the operation is marked with...
[OperationContract(IsOneWay = true)]
...而且服务器变量在单向操作中不可用.
...and that server variables aren't available in one-way operations.
可惜例外并没有说明什么……
Shame the exception doesn't say something to that effect...
这篇关于HttpRequest.ServerVariables 使用 IIS7 在 WCF 中抛出 ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!