【发布时间】:2010-10-19 17:19:11
【问题描述】:
尝试调用网络服务时出错
“System.NullReferenceException:对象引用未设置为对象的实例。” 此行出错
if (Authentication.Username == "x" &&
Authentication.Password == "y")
这是什么意思?
[WebService(Namespace = "https://domain.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Testing : System.Web.Services.WebService
{
public TestAuthHeader Authentication;
public class TestAuthHeader : SoapHeader
{
public string Username;
public string Password;
}
[WebMethod]
[SoapHeader("Authentication")]
public string TestService()
{
if (Authentication.Username == "x" &&
Authentication.Password == "y")
{
return "OK";
}
else
{
return "Access Denided";
}
}
}
【问题讨论】:
-
可能
Authentication字段为空。但是 C# 中异常的好处是它们会告诉您失败的准确行号。这意味着什么? -
您是如何调用该服务的?你能发布那个代码吗?
-
@kirk,这一行出错“if (Authentication.Username == "x" && Authentication.Password == "y")"
-
你能完成这项工作吗?如果是这样,您能否将答案标记为正确,或者发布您自己的答案并将其标记为正确,以便其他人知道什么有助于解决这个问题?谢谢!
-
NullReferenceException的几乎所有情况都是相同的。请参阅“What is a NullReferenceException and how do I fix it?”获取一些提示。
标签: c# web-services soap asmx nullreferenceexception