Web services 使用HttpContext的Webmethod返回空引用异常

Web services 使用HttpContext的Webmethod返回空引用异常,web-services,asmx,nullreferenceexception,Web Services,Asmx,Nullreferenceexception,它是通过.asmx页面运行的web方法: 快速总结: [WebMethod] Method Name { string Port=HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; if (Port == null || Port == "80" || Port == "443") { Port = ""; } else {

它是通过.asmx页面运行的web方法:

快速总结:

[WebMethod]

Method Name
{
 string Port=HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
  if (Port == null || Port == "80" || Port == "443")
  {
                    Port = "";
   }
    else
    {
                    Port = ":" + Port;
    }
}
当我测试这个方法时,我得到一个异常

HttpContext.Current.Request.ServerVariables["SERVER_PORT"]    'System.Web.HttpContext.Current' is null.
  • 什么条件httpcontext将为空

  • 为什么它由于空引用异常而使方法失败


  • 这些和IIS有什么关系吗

    我已使HttpContext在web服务中正常工作,请发布更多详细信息。“对象引用未设置为对象的实例。”?ex.Message“对象引用未设置为对象的实例。”?ex.Data{System.Collections.ListDictionaryInternal}[System.Collections.ListDictionaryInternal]:{System.Collections.ListDictionaryInternal}IsFixedSize:false IsReadOnly:false键:{System.Collections.ListDictionaryInternal.NodeKeyValueCollection}值:{System.Collections.ListDictionaryInternal.NodeKeyValueCollection}?例如内部异常null@Brain,您可以使用服务器\端口共享httpcontext请求对象的代码吗?我已使httpcontext在web服务中正常工作,请发布更多详细信息。“对象引用未设置为对象的实例。”?ex.Message“对象引用未设置为对象的实例。”?ex.Data{System.Collections.ListDictionaryInternal}[System.Collections.ListDictionaryInternal]:{System.Collections.ListDictionaryInternal}IsFixedSize:false IsReadOnly:false键:{System.Collections.ListDictionaryInternal.NodeKeyValueCollection}值:{System.Collections.ListDictionaryInternal.NodeKeyValueCollection}?例如内部异常null@Brain,是否可以使用服务器\端口共享httpcontext请求对象的代码?