你调用的对象是空的。???在肥皂中?
时出现错误
尝试调用 Web 服务“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";
}
}
}
Got an error while trying to invoke the webservice
"System.NullReferenceException: Object reference not set to an instance of an object."
Error on this line
if (Authentication.Username == "x" &&
Authentication.Password == "y")
what does this mean?
[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";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将类
TestAuthHeader
的定义移到 Web 服务本身之外...另外,尝试测试未知标头以查看
Authentication
是否最终在那里...除此之外,我们可以看到您调用 Web 服务的代码吗?
*编辑*
如果您没有在客户端执行任何操作来创建 TestAuthHeader 实例并设置其属性,那么您将在服务端获得空引用。来自使用 SoapHeaders 的 MSDN 示例(客户端代码):
您真正需要的是:
这将处理客户端未设置身份验证对象的情况。
Try moving the definition of class
TestAuthHeader
outside of the web service itself...Also, try testing unknown headers to see if
Authentication
is ending up there...Other than that, can we see the code where you're calling into the web service?
*Edit *
If you're not doing anything on the client side to create an instance of TestAuthHeader and set its properties, you're going to have a null reference on the service side. From the MSDN example of using SoapHeaders (client code):
All you really need is:
This will handle the scenario of where the client doesn't set an authentication object.
我只是猜测,但在您的
TestService
方法中,Authentication
实例可能为 null,从而给您带来异常。也许您没有为该网站启用任何身份验证?I'm just guessing, but in your
TestService
method, theAuthentication
instance is probably null, giving you that exception. Maybe you don't have any authentication turned on for the site?