简体   繁体   English

具有线程WCF客户端的C#Winform应用程序

[英]C# winform application with threaded WCF client

I'm building an application that uses a WCF client to retrieve data from my server. 我正在构建一个使用WCF客户端从服务器检索数据的应用程序。

I want my call to the service to be asynchronous because many of them need to change the UI and I don't want to lose responsiveness from my app. 我希望对服务的调用是异步的,因为其中许多需要更改UI,并且我不想失去应用程序的响应能力。

I tried using *Completed and *Async : 我尝试使用*Completed*Async

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

Inside the *Completed delegate I always get an error (Connection closed by remote host: I enabled every logging I could find but I still don't understand why I get these errors) 在* Completed委托中,我总是收到一个错误(远程主机关闭了连接:我启用了所有可以找到的日志记录,但是我仍然不明白为什么会收到这些错误)

Synchronous calls always work. 同步调用始终有效。

I have a class that handles all the calls to the service. 我有一个处理所有对该服务的调用的类。

Is there a way to have syncronous calls inside something like a threaded class? 有没有办法在线程类之类的内部进行同步调用?

Are you setting the client side bindings to match what the server accepts? 您是否正在设置客户端绑定以匹配服务器接受的内容?

You should also try testing it with the WCF test client (normally under %Program Files%\\Microsoft Visual Studio 10.0\\Common7\\IDE\\WcfTestClient.exe). 您还应该尝试使用WCF测试客户端进行测试(通常在%Program Files%\\ Microsoft Visual Studio 10.0 \\ Common7 \\ IDE \\ WcfTestClient.exe下)。 If the test client works then check the bindings. 如果测试客户端正常工作,则检查绑定。

Is your call even getting to the server? 您的电话甚至到达服务器了吗? I've had similar errors happen when serializing the response from the server to the client, so you might want to check for that. 从服务器到客户端的响应序列化时,我也遇到过类似的错误,因此您可能需要检查一下。 If you get to your server then the bindings are not the problem but rather there is a serialization problem. 如果您使用服务器,则绑定不是问题,而是序列化问题。 Do you have "sets" on the data model properties that are trying to get deserialized on the server? 您是否在试图在服务器上反序列化的数据模型属性上具有“设置”?

I know this is no answer but I haven't been here enough to be allowed comments...and I've been where you are, totally frustrating. 我知道这是没有答案,但是我来这儿的地方还不够多,不能发表评论……而我去过你所在的地方,完全感到沮丧。

I ended up creating my own async methods using BackgroundWorker this way (probably not the best way but it works): 我最终以这种方式使用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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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