HttpRequest.ServerVariables在带有IIS7的WCF中引发ArgumentException
我们有一个WCF(.NET 3.5 SP1)服务在IIS7中运行,该服务被标记为允许ASP.NET兼容模式,因此我们可以访问HttpRequest.ServerVariables在带有IIS7的WCF中引发ArgumentException,wcf,Wcf,我们有一个WCF(.NET 3.5 SP1)服务在IIS7中运行,该服务被标记为允许ASP.NET兼容模式,因此我们可以访问HttpContext。当前: [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 但是,当尝试访问任何服务器变量时,它会抛出一个ArgumentException: System.ArgumentException:
HttpContext。当前:
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
但是,当尝试访问任何服务器变量时,它会抛出一个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.以下任何一行都会引发此异常:
ha = HttpContext.Current.Request.UserHostAddress;
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
us = HttpContext.Current.Request.UserAgent;
这似乎是内部框架调用的结果,这意味着调用代码没有错误
大概是我们的配置中有什么东西导致了这个问题,但我不知道是什么。有什么想法吗?或者我如何检索这三位数据的任何想法(不幸的是,它们似乎没有被WCF本机公开)。结果表明,问题是操作标记为
[OperationContract(IsOneWay = true)]
…并且服务器变量在单向操作中不可用
遗憾的是,例外情况没有说明这一点……我也撞到了这面墙。IsOneWay属性就到此为止:(谢谢Greg,你帮我节省了几个小时。有没有办法检测到这一点,这样我就可以避免使用servervariables了?