开发者

WPF application locking up when sending/receiving messages to multiple WCF services (MVVM)

I'm struggling a bit with the right architecture for my app. Particularly having problems with the UI locking up and I'm not sure why. I have two apps both running services that need to talk to each other.

When sending messages either way I create a new channel factory each time.

The Clients send messages every 15s to the Server. On command, the Server sends messages to the Clients (one at a time). Theoretically the Server could be receiving messages from multiple Clients constantly, so needs to scale like a webserver while still maintaining UI respo开发者_Python百科nsiveness.

The problem is after a while my UI locks up, sometimes on the Server, sometimes on the Client. I'm pretty sure it's due to WCF (there's also a lot of other stuff going on).

If I try to send a message by creating the channel (via channelfactory), sending the message and then closing the channel factory it takes a minute for the message to be sent (meantime the UI on both apps are locked). If I leave off channelFactory.close() after I send the message then messages get sent right away.

The service I create has [OperationContract(IsOneWay = true)] for the SendMessage method. The implementation of my service has [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)].

Why does it take a minute for my message to be sent when using channelFactory.close() at the end?


In most cases, you should not send a message over the network on the UI thread. This is what is causing your app to lock up. I can't see why your web service calls are taking 1 minute - it may just be that the UI thread is getting queued up with messages. Let's fix the threading issue first and see if that improves the request time.

You don't need to worry about receiving messages, because WCF will spin up a thread to receive and process the message, but when you send a message, the thread will block until the message has been sent and acknowledged.

To send your message on a non-UI thread, use the Task Parallel Library:

Task t = Task.Factory.StartNew(() =>
{
    //Do web service call here
});

If you need to update the UI after the message has been sent, follow the above code with something like this:

Task UITask= t.ContinueWith(() =>
{
 this.TextBlock1.Text = "Complete"; 
}, TaskScheduler.FromCurrentSynchronizationContext());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜