Hosting in a windows service has a few gotcha’s to it as I found out on my first WF project and I wanted to share those here to hopefully help others to get things running much quicker than I did.
Introduction
The code to host in a Windows service is the same as in any other windows application and you can find those details here. However, hosting in a Windows service needs some special handling and this is because you need to call WorkflowRuntime.StartRuntime(), WorkflowRuntime.StopRuntime(), and WorkflowRuntime.Dispose() on the same thread and the SCM (Service Control Manager) which is the Windows service that will call your Windows Services OnStart() and OnStop() methods will call these methods on different threads which is illustrated below.
You will want to put WorkflowRuntime.StartRuntime() in your OnStart() method and you will want to put your calls to WorkflowRuntime.StopRuntime() and WorkflowRuntime.Dispose() in the OnStop() method and you will need a way to make sure that all of these calls are preformed on the same thread or you will exprience issues like server hangs on shutdown.
SyncronizationContext
A custom SynchronizationContext is the tool that I used to preform calls on the same thread. My code is based on the threading articles by mikeperetz and I won’t be covering the threading details in this article so please see Mike’s articles for those details. I’ve taken the code in that article series and created my own custom SynchronizationContext called MainThreadSynchronizationContext which is included in the sample code and allows for you to easily marshal calls to the same thread by using the Send method shown below.
public partial class WorkflowServer : ServiceBase
{
private MainThreadSynchronizationContext _syncContext = null;
WorkflowRuntime _runtime = null;
public WorkflowServer()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// Create new thread using MainThreadSynchronizationContext and then
// send message to call Start() on the new "Main WF Thread" thread
_syncContext = new MainThreadSynchronizationContext("Main WF Thread");
_syncContext.Send(Start, null);
}
// Will be called on "Main WF Thread"
protected void Start(object data)
{
_runtime = new WorkflowRuntime();
_runtime.StartRuntime();
// Do other WF related processing
}
protected override void OnStop()
{
// Call Stop() on "Main WF Thread"
_syncContext.Send(Stop, null);
}
// Will be called on "Main WF Thread"
protected void Stop(object data)
{
_runtime.StopRuntime();
_runtime.Dispose();
}
}
If you use this technique your server will start and stop nicely and you won’t experience any hangs on shutdown which was something we were seeing a lot on my current project.
Tags: Hosting, SynchronizationContext, Threading, Windows Service, WPF 3.5
Hello,
Can i get a one small photo from your blog?
Thanks
[url=http://www.noteshamps.com/]Nicolas[/url]
Sure, what were you going to use it for?
Thank you,
very interesting article
Nicely article
Hello! Please e-mail me your contacts. I have a question webmaster@spottovo.ru” rel=”nofollow”>……
Thank you!!!…
Buy:Retin-A.Valtrex.Accutane.Petcam (Metacam) Oral Suspension.Synthroid.Lumigan.Prednisolone.Nexium.Zovirax.Mega Hoodia.Zyban.Arimidex.Human Growth Hormone.100% Pure Okinawan Coral Calcium.Actos.Prevacid….
Tapes http://dburtonml8xlf.ACEHARDWAREE.INFO/tag/Mini+Tapes+micro+tapes/ : tapes…
micro…
Buy:VPXL.Viagra.Levitra.Viagra Professional.Viagra Soft Tabs.Cialis Soft Tabs.Cialis Professional.Maxaman.Soma.Propecia.Cialis.Viagra Super Force.Cialis Super Active+.Super Active ED Pack.Viagra Super Active+.Zithromax.Tramadol….
Buy:Benicar.Ventolin.Acomplia.Female Pink Viagra.Prozac.Buspar.Lipothin.Nymphomax.Cozaar.Lipitor.Zetia.Amoxicillin.Aricept.Advair.Seroquel.Female Cialis.Wellbutrin SR.Lasix.Zocor.SleepWell….