我正在尝试从 C# 客户端使用基于 C# 的服务。
当我中断服务方法调用并将鼠标悬停在前面的 username
和 password
属性上以验证它们是否已设置,然后跨过该方法时,它起作用了美好的。当我在服务方法调用处中断,然后在不验证用户名/密码的情况下立即跳过方法调用时,我得到了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();
最佳答案
您没有指定,但假设是 WCF Web 服务,要记住的一件事是在您第一次调用 ws 方法之前不会实际尝试代理连接。它的行为相当奇怪,但根据我的经验,当您调用 .Open() 时,客户端连接实际上并未建立。在您的代码中,在 .FindVillain() 调用中尝试连接。您将需要进入 .FindVillain() 以查看实际为空的内容(或使用堆栈跟踪)。您还可以尝试调用不同的 ws 方法(如果有的话)以查看是否在第一次调用时或特别是在 .FindVillain() 上抛出了 null 异常。
祝你好运
关于c# - 对象引用未设置为对象的实例 #1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13236619/