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";
}
}
}
Try moving the definition of class
TestAuthHeaderoutside of the web service itself…Also, try testing unknown headers to see if
Authenticationis 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.