Object reference not set to an instance of an object.??? in soap?
本问题已经有最佳答案,请猛点这里访问。
尝试调用WebService时出错
"System.NullReferenceException:对象引用未设置为对象的实例。"此行出错
1 2 | if (Authentication.Username =="x" && Authentication.Password =="y") |
这是什么意思?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [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"; } } } |
尝试将类
另外,尝试测试未知的头文件,看看
1 2 3 4 5 6 7 8 9 10 | [WebMethod] [SoapHeader("Authentication")] [SoapHeader("unknownHeaders",Required=false)] public string TestService() { foreach (SoapUnknownHeader header in unknownHeaders) { // Test header Debug.Write(header.Element.Name); } } |
除此之外,我们能看到您调用Web服务的代码吗?< BR>< BR>*编辑*
如果您没有在客户机端做任何事情来创建TestAuthHeader的实例并设置其属性,那么在服务端会有一个空引用。从使用soapheaders(客户机代码)的msdn示例中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | MyHeader mySoapHeader = new MyHeader(); // Populate the values of the SOAP header. mySoapHeader.Username = Username; mySoapHeader.Password = SecurelyStoredPassword; // Create a new instance of the proxy class. MyWebService proxy = new MyWebService(); // Add the MyHeader SOAP header to the SOAP request. proxy.MyHeaderValue = mySoapHeader; // Call the method on the proxy class that communicates with // your Web service method. string results = proxy.MyWebMethod(); |
< BR>你真正需要的是:
1 2 3 4 5 6 7 | public string TestService() { if (Authentication == null) { return"Access is denied"; } // ... the rest of your code |
这将处理客户端不设置身份验证对象的场景。
我只是在猜测,但是在您的