C# Web服务的HTTPContext
HttpContext.Current.Request.ServerVariables[“服务器\端口”] HttpContext.Current.Request.ServerVariables[“服务器\端口\安全”] HttpContext.Current.Request.ServerVariables[“服务器名称”] HttpContext.Current.Request.ApplicationPathC# 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中调用
我想通过一个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添加了它,但没有什么不同。