简体   繁体   中英

Object reference not set to an instance of an object #1

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM