WCF threading - non-responsive UI
Asked Answered
H

3

6

I'm trying to configure some WCF stuff. Currently, I have a server which allows remote users to download files, and client. In the server, I use a ServiceHost class. I assume it should be running on a separate thread, however, the server UI (WinForms) becomes locked when someone downloads a file. Is there a way to manage the WCF threading model?

Thank you!

Hayman answered 1/6, 2010 at 11:53 Comment(2)
How are you hosting the service? Is it a window service or a custom hosting using some exe?Cowell
The WinForms executable hosts WCF service library.Hayman
H
1

"From the same Windows Form application if you were to construct the ServiceHost instance before starting the UI thread, it will run on its own thread. That means worker threads allocated from the thread pool process messages instead of the message loop. Thus, services can truly process multiple concurrent requests. "

Hayman answered 1/6, 2010 at 12:51 Comment(1)
This will result in functionality equivalent to the answer I gave below. Since you are creating the ServiceHost before the SycnhronizationContext.Current property gets set to an instance of WindowsFormsSynchronizationContext (e.g. before any controls have been created), your ServiceHost will not attempt to process service requests on the UI thread. The advantage of my solution is that it makes this behavior a matter of policy, so that it doesn't matter at what point you decide to create your ServiceHost.Albuminous
A
11

You should add a ServiceBehaviorAtttribute to the class implementing your service and set its UseSynchronizationContext property to false. This will cause calls to your service to be processed on their own thread.

Example:

[ServiceBehavior(UseSynchronizationContext=false)]
class YourService : IYourService
{
  // Service Methods
}

Just remember that if you are going to update any Controls from within your service methods, you must bear in mind the cross-thread programming model of Windows Forms.

Albuminous answered 3/6, 2010 at 0:37 Comment(2)
wOW + 1 didn't know that feature!Distributee
This is the solution to ~2 days bug search of mine, why a WCF service does not work on a non-UI app (app has both UI and non UI mode). Thanks.Monotype
H
1

"From the same Windows Form application if you were to construct the ServiceHost instance before starting the UI thread, it will run on its own thread. That means worker threads allocated from the thread pool process messages instead of the message loop. Thus, services can truly process multiple concurrent requests. "

Hayman answered 1/6, 2010 at 12:51 Comment(1)
This will result in functionality equivalent to the answer I gave below. Since you are creating the ServiceHost before the SycnhronizationContext.Current property gets set to an instance of WindowsFormsSynchronizationContext (e.g. before any controls have been created), your ServiceHost will not attempt to process service requests on the UI thread. The advantage of my solution is that it makes this behavior a matter of policy, so that it doesn't matter at what point you decide to create your ServiceHost.Albuminous
R
0

You shouldn't host a WCF Service inside a UI program. WCF has a very specific threading model, that will prevent your UI from running nicely.

Basically, the WCF threading scheme changes depending on the instance management behavior selected, and also when there are no requests reaching the service during a time period WCF might suspend the running thread. All of this will cause trouble with the UI.

What I do in this situations, is to create a Windows Service hosting WCF, and create a ServiceContract to expose the needed data to the monitoring UI. This UI will run independtly in it's own exe, being another client of the service, fetching the data from the service as needed.

I hope makes sense for you.

Rhinencephalon answered 1/6, 2010 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.