代码之家  ›  专栏  ›  技术社区  ›  Greg Beech

httpRequest.serverVariables在带有iis7的wcf中引发ArgumentException

wcf
  •  4
  • Greg Beech  · 技术社区  · 16 年前

    我们有一个在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本机公开)。

    1 回复  |  直到 16 年前
        1
  •  13
  •   Greg Beech    16 年前

    结果发现问题是操作被标记为…

    [OperationContract(IsOneWay = true)]
    

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

    遗憾的是,这个例外并没有说明什么…

    推荐文章