开发者

C# winform application with threaded WCF client

开发者 https://www.devze.com 2023-02-14 20:28 出处:网络
I\'m building an application that uses a WCF client to retrieve data from my server. 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 l

I'm building an application that uses a WCF client to retrieve data from my server.

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.

I tried using *Completed and *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)

Synchronous calls always work.

I have a class that hand开发者_开发百科les 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). 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):

// 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

精彩评论

暂无评论...
验证码 换一张
取 消