Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web服务的HTTPContext_C#_Web Services - Fatal编程技术网

C# Web服务的HTTPContext

C# Web服务的HTTPContext,c#,web-services,C#,Web Services,HttpContext.Current.Request.ServerVariables[“服务器\端口”] HttpContext.Current.Request.ServerVariables[“服务器\端口\安全”] HttpContext.Current.Request.ServerVariables[“服务器名称”] HttpContext.Current.Request.ApplicationPath 我想通过一个webservice-C#访问这些值,每当我在webservice中调用

HttpContext.Current.Request.ServerVariables[“服务器\端口”] HttpContext.Current.Request.ServerVariables[“服务器\端口\安全”] HttpContext.Current.Request.ServerVariables[“服务器名称”] HttpContext.Current.Request.ApplicationPath


我想通过一个webservice-C#访问这些值,每当我在webservice中调用这些值时,上面所有的值都会为null,而上面的值适用于web页面(aspx)。

您使用的是哪种web服务?asmx还是wcf?它们应该可以与asmx服务配合使用,但如果您使用的是WCF,则需要向该方法添加以下属性:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

如果是WCF web服务,则可以执行以下操作:

[AspNetCompatibilityRequirementsAttribute(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class FooBar : IFooBar
{
   public void DoSomething()
   {
       HttpContext context = HttpContext.Current;
       if (context != null)
       {
             // Should get here now
       }
   }

}

关键是添加
[AspNetCompatibilityRequirementsAttribute(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]

正如其他人提到的,您需要启用ASP.NET兼容性。如果不想通过以下属性限制代码,也可以通过配置启用此功能:

<system.serviceModel>        
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>


这有助于向我解释启用兼容模式的基本功能和权衡。

您是否尝试将EnableSession定义为true

[WebMethod(EnableSession = true)]
    public string your_public_method(your_params)
    { [...] }

能否尝试将[WebMethod(EnableSession:=True)]添加到web方法中。我还没试过!!可能重复的请停止一遍又一遍地问同一个问题。这是为了wcf&我现在为asmx的web.config添加了它,但没有什么不同。