2011-03-02 52 views
0

我在构建一个使用WCF客户端从我的服务器检索数据的应用程序。带线程WCF客户端的C#winform应用程序

我希望我对该服务的调用是异步的,因为他们中的很多人都需要更改UI,我不想失去来自我的应用程序的响应。

我尝试使用*Completed*Async

ServiceUserClient client = new ServiceUserClient(); 
client.FindUserCompleted += delegate(object sender, FindUserCompletedEventArgs e) 
{ 
    // here e.Result always fails 
}; 
client.FindUserAsync(text); 

的*完成委托里面我总是得到远程主机关闭的错误(连接:我每次启用日志记录我能找到,但我仍然不明白为什么我收到这些错误)

同步呼叫始终工作。

我有一个类来处理所有的服务调用。

有没有办法在类似于线程类的东西里面有同步调用?

+0

你也可以使用WCF跟踪客户端和服务器上? http://msdn.microsoft.com/en-us/library/ms733025.aspx –

+0

是的,我仍然无法弄清楚为什么它给了我这个错误:尝试建立到net.tcp的连接时,操作被中断:// /。 – Keeper

+0

在得到e.Result之前,我总是检查e.Error。 e.Error说什么? –

回答

0

我结束了使用的BackgroundWorker这种方式创建我自己的异步方法(可能不是最好的方式但它的工作原理):

// this is the click event on my search button 
private void FindUser_Click(object sender, EventArgs e) 
{ 
    this.UserListSearch.Enabled = false; 
    this.UserListSearch.Items.Clear(); 
    Model.FindUser(FindText.Text.ToUpper(), userlist => 
    { 
     foreach (User u in userlist) 
     { 
      ListViewItem item = new ListViewItem(u.UserName); 
      item.Name = u.UserName; 
      item.SubItems.Add(u.Description); 
      this.UserListSearch.Items.Add(item); 
     } 
     this.UserListSearch.Enabled = true; 
    }); 
} 

// this is the function I call when I need async call 
public void FindUser(string text, Action<User[]> callback) 
{ 
    CreateBackgroundWorker<User[]>(() => 
     { 
      ServiceUsersClient client = new ServiceUsersClient(); 
      var results = client.FindUser(text); 
      client.Close(); 
      return results; 
     }, callback); 
} 

// this is my utility function to create a bgworker "on demand" 
private void CreateBackgroundWorker<T>(Func<T> dowork, Action<T> callback) 
{ 
    BackgroundWorker worker = new BackgroundWorker(); 
    worker.DoWork += (sender, args) => 
    { 
     T result = dowork.Invoke(); 
     (callback.Target as Form).Invoke(callback, result); 
    }; 
    worker.RunWorkerAsync(); 
} 
0

您是否设置客户端绑定以匹配服务器接受的内容?

你也应该尝试与WCF测试客户端(通常在%Program Files文件%\微软的Visual Studio 10.0 \ Common7 \ IDE \ WcfTestClient.exe)测试它。如果测试客户端工作,然后检查绑定。

你的电话是否连上服务器?序列化从服务器到客户端的响应时发生了类似的错误,因此您可能需要检查该问题。如果你到达你的服务器,那么绑定不是问题,而是存在序列化问题。对于试图在服务器上进行反序列化的数据模型属性,您有“套件”吗?

我知道这是没有答案,但没有得到我这里足以被允许的意见......我已经身在何处,完全令人沮丧。

+0

他提到,同步调用工作,所以它不会被序列化问题。 –

+0

我现在看到了......谢谢。也许尝试与客户端或WCFStorm? – ale

相关问题