对象引用未设置为对象#1的实例

时间:2012-11-05 16:50:18

标签: c# service

我正在尝试从C#客户端使用基于C#的服务。

当我在服务方法调用中断并将鼠标悬停在前面的usernamepassword属性上以验证它们是否已设置时,然后跳过该方法,它可以正常工作。当我打破服务方法调用,然后立即跳过方法调用而不验证用户名/密码时,我得到object reference not set to an instance of an object

用户名和密码来自app.config文件,配置值不会更改。

这可能是同步处理问题,用户名和密码没有及时设置?我尝试插入sleep(5000)来电,这没有用。我已多次更新服务参考。

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

1 个答案:

答案 0 :(得分:0)

您没有指定,但假设WCF Web服务需要记住的一件事是,在您第一次调用ws方法之前,实际上并未尝试代理连接。它的相当奇怪的行为,但根据我的经验,当你调用.Open()时,实际上并没有建立客户端连接。在您的代码中,在.FindVillain()调用中尝试连接。您需要步入.FindVillain()以查看实际为null(或使用堆栈跟踪)。你也可以尝试调用一个不同的ws方法(如果有的话)来查看第一次调用或特别是.FindVillain()上是否抛出了null异常。

祝你好运