1

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";
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
001
  • 62,807
  • 94
  • 230
  • 350
  • Probaly `Authentication` field is null. But the nice thing about exceptions in C# is that they tell you the precise line number of the failure. What line does it implicate? – Kirk Woll Oct 19 '10 at 17:22
  • How are you calling in to the service? Can you post that code? – James King Oct 19 '10 at 19:01
  • @kirk, error on this line "if (Authentication.Username == "x" && Authentication.Password == "y")" – 001 Oct 20 '10 at 01:01
  • Were you able to make this work? If so, can you mark an answer as correct, or post your own answer and mark that as correct so that others will know what was helpful to resolve this? Thanks! – James King Nov 08 '10 at 18:01
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 27 '15 at 20:02

2 Answers2

0

I'm just guessing, but in your TestService method, the Authentication instance is probably null, giving you that exception. Maybe you don't have any authentication turned on for the site?

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • Sorry but what does that mean? I have to turn on authentication ? Setup web.config to turn it on? Otherwise it wouldn't work? – 001 Oct 20 '10 at 00:16
0

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...

[WebMethod]
[SoapHeader("Authentication")]
[SoapHeader("unknownHeaders",Required=false)]
public string TestService()
{
   foreach (SoapUnknownHeader header in unknownHeaders) {
      // Test header
      Debug.Write(header.Element.Name);
   }
}

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):

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();


All you really need is:

public string TestService()
{
    if (Authentication == null) {
        return "Access is denied";
    }

    // ... the rest of your code

This will handle the scenario of where the client doesn't set an authentication object.

James King
  • 6,233
  • 5
  • 42
  • 63
  • just using the default asm page to do involk, I was expecting the response to be "Access Denied" – 001 Oct 20 '10 at 01:04
  • please hv a look at http://stackoverflow.com/questions/22781969/adding-parameters-to-soap-request-using-ksoap2-android – Braj Apr 02 '14 at 08:44