【问题标题】:HttpRequest.ServerVariables throws ArgumentException in WCF with IIS7HttpRequest.ServerVariables 在带有 IIS7 的 WCF 中引发 ArgumentException
【发布时间】:2009-03-16 13:09:59
【问题描述】:

我们有一个在 IIS7 中运行的 WCF (.NET 3.5 SP1) 服务,它被标记为允许 ASP.NET 兼容模式,因此我们可以访问HttpContext.Current

[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() 

例如以下任何行都会引发此异常:

ha = HttpContext.Current.Request.UserHostAddress;
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
us = HttpContext.Current.Request.UserAgent;

这似乎是内部框架调用的结果,这意味着调用代码没有错误。

大概是我们的配置中的某些东西导致了这个问题,但我不知道是什么。有任何想法吗?或者任何想法我可以如何检索这三位数据(不幸的是,它们似乎没有被 WCF 本机公开)。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    原来问题是操作标记为...

    [OperationContract(IsOneWay = true)]
    

    ...并且服务器变量在单向操作中不可用。

    遗憾的是,例外并没有说明这一点......

    【讨论】:

    • 我也刚碰了壁。 IsOneWay 属性就这么多。 :(
    • 谢谢 Greg,你为我节省了几个小时。
    • 有没有办法检测到这个,所以我可以避免使用服务器变量?
    猜你喜欢
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多