2

I'm trying to consume a C# based service, from a C# client.

When I break at the service method call and hover over the preceding username and password properties to verify that they are being set, then step over the method, it works fine. When I break at the service method call, and then immediately step over the method call without verifying the username/password, I get object reference not set to an instance of an object.

The username and password are coming from an app.config file, and the config values are not changing.

Is this possibly an synchronous processing issue, and the username and password are not getting set in time? I've tried inserting a sleep(5000) call, which doesn't help. I've updated the service reference many times.

MyService.ServiceClient sc = new MyService.ServiceClient();

sc.ClientCredentials.UserName.UserName ="pinkpanther"; // comes from app.config file
sc.ClientCredentials.UserName.Password = "clouseau"; // comes from app.config file

//open service client/proxy
sc.Open();

Dictionary<int, string> result = new Dictionary<int, string>();

// Sleep(5000); this doesn't seem to help

result = sc.FindVillain("Paris", "Train", "Car3", 4);

success = result.ContainsValue("The villain has been detained.");

sc.Close();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    Looking at the properties in the debugger is executing code to get those property values--its executing the getter of the property. Presumably, executing that code is somehow lazily filling in the values. – Matt Smith Nov 05 '12 at 16:56
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 05 '12 at 16:56
  • 1
    Can you step into the generated FindVillain method (or look at the call stack) and see what exactly is null? – Jan Kratochvil Nov 05 '12 at 16:58
  • So maybe you should try and assign one of those ClientCredentials to a temporary variable after opening the service. E.g. var tmp = sc.ClientCredentials.UserName.UserName – Jan Hansen Nov 05 '12 at 17:00

1 Answers1

0

You dont specify, but assuming a WCF web service one thing to keep in mind is that the proxy connection is not actually attempted until you make the first call to a ws method. Its rather odd behavior but from my experience the client connection is not actually established when you call .Open(). In your code the connection is attempted at the .FindVillain() call. You will need to step into .FindVillain() to see what's actually null (or use the stack trace). You could also try calling a different ws method (if one is available) to see if the null exception is thrown on the first call or specifically on .FindVillain().

Good luck

tHand
  • 301
  • 1
  • 5