<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ryan Vice&#039;s Blog &#187; Windows Service</title>
	<atom:link href="http://www.ryanvice.net/tag/windows-service/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryanvice.net</link>
	<description>Implementation notes and development techniques</description>
	<lastBuildDate>Thu, 11 Feb 2010 13:43:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hosting WorkflowRuntime 3.5 as a Windows Service</title>
		<link>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/</link>
		<comments>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 20:50:16 +0000</pubDate>
		<dc:creator>Ryan Vice</dc:creator>
				<category><![CDATA[WF 3.5]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[SynchronizationContext]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Windows Service]]></category>
		<category><![CDATA[WPF 3.5]]></category>

		<guid isPermaLink="false">http://www.ryanvice.net/?p=128</guid>
		<description><![CDATA[ Download Source
Hosting in a windows service has a few gotcha&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p> <a href="http://www.ryanvice.net/wp-content/uploads/2009/11/WorkflowHostService.zip">Download Source</a></p>
<p>Hosting in a windows service has a few gotcha&#8217;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.</p>
<p><span id="more-128"></span></p>
<h2>Introduction</h2>
<p>The code to host in a Windows service is the same as in any other windows application and you can find those details <a href="http://odetocode.com/Articles/457.aspx" target="_blank">here</a>. However, hosting in a Windows service needs some special handling and this is because you need to call <em>WorkflowRuntime.StartRuntime()</em>, <em>WorkflowRuntime.StopRuntime()</em>, and <em>WorkflowRuntime.Dispose()</em> on the same thread and the SCM (<a href="http://msdn.microsoft.com/en-us/library/ms685150(VS.85).aspx" target="_blank">Service Control Manager</a>) which is the Windows service that will call your Windows Services <em>OnStart()</em> and <em>OnStop</em>()<em> </em>methods will call these methods on different threads which is illustrated below. </p>
<p><a href="http://www.ryanvice.net/wp-content/uploads/2009/11/SCMThreading.JPG"><img class="alignnone size-full wp-image-134" title="SCMThreading" src="http://www.ryanvice.net/wp-content/uploads/2009/11/SCMThreading.JPG" alt="SCMThreading" width="837" height="469" /></a></p>
<p>You will want to put <em>WorkflowRuntime.StartRuntime() </em>in your <em>OnStart()</em> method and you will want to put your calls to <em>WorkflowRuntime.StopRuntime()</em> and <em>WorkflowRuntime.Dispose()</em> in the <em>OnStop()</em> 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.</p>
<h2>SyncronizationContext</h2>
<p>A custom SynchronizationContext is the tool that I used to preform calls on the same thread. My code is based on the threading <a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=313446" target="_blank">articles by mikeperetz</a> and I won&#8217;t be covering the threading details in this article so please see Mike&#8217;s articles for those details. I&#8217;ve taken the code in that article series and created my own custom SynchronizationContext called <em>MainThreadSynchronizationContext</em> which is included in the sample code and allows for you to easily marshal calls to the same thread by using the <em>Send</em> method shown below.</p>
<pre class="brush: csharp;">

    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 &quot;Main WF Thread&quot; thread
            _syncContext = new MainThreadSynchronizationContext(&quot;Main WF Thread&quot;);
            _syncContext.Send(Start, null);
        }

        // Will be called on &quot;Main WF Thread&quot;
        protected void Start(object data)
        {
            _runtime = new WorkflowRuntime();
            _runtime.StartRuntime();

            // Do other WF related processing
        }

        protected override void OnStop()
        {
            // Call Stop() on &quot;Main WF Thread&quot;
            _syncContext.Send(Stop, null);
        }

        // Will be called on &quot;Main WF Thread&quot;
        protected void Stop(object data)
        {
            _runtime.StopRuntime();
            _runtime.Dispose();
        }
    }</pre>
<p>If you use this technique your server will start and stop nicely and you won&#8217;t experience any hangs on shutdown which was something we were seeing a lot on my current project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
