<?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>topshelf Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/topshelf/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/topshelf/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sat, 17 Nov 2018 13:21:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>topshelf Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/topshelf/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Building Windows Services using Topshelf</title>
		<link>https://www.codejourney.net/windows-services-using-topshelf/</link>
					<comments>https://www.codejourney.net/windows-services-using-topshelf/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sat, 14 May 2016 18:19:52 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[topshelf]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=26</guid>

					<description><![CDATA[<p>Hello again 🙂 Visibly this is going to be my first technical post here. I hope you&#8217;ll find it useful. Windows Services Recently I came up with a need to create an application, which executes some tasks in the background based on file system&#8217;s events. &#8220;That&#8217;s easy&#8221;, one would say. &#8220;Build Windows Service app&#8221;, he&#8230;</p>
<p>The post <a href="https://www.codejourney.net/windows-services-using-topshelf/">Building Windows Services using Topshelf</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p style="text-align: left;">Hello again 🙂<br />
Visibly this is going to be my first technical post here. I hope you&#8217;ll find it useful.</p>
<p><span id="more-26"></span></p>
<h2>Windows Services</h2>
<p style="text-align: left;">Recently I came up with a need to create an application, which executes some tasks in the background based on file system&#8217;s events. &#8220;That&#8217;s easy&#8221;, one would say. &#8220;Build Windows Service app&#8221;, he would add. Sure, that was also my first idea.</p>
<p style="text-align: left;">I think that&#8217;s very common that we want to create an application without any GUI, which purpose is just to do something in the background. Moreover we&#8217;d like it to be independent from the user running it &#8211; we want it to be running all the time, without having to manually launch it &#8211; especially when the application is running on customer&#8217;s environment, where we can&#8217;t be logged in all the time running the application.</p>
<p style="text-align: left;">Here comes the service &#8211; long-running executable with no user interface, which runs in its own Windows session. Such application can be:</p>
<ul>
<li style="text-align: left;">executed by any user created in the system (e.g. with specific security policies assigned),</li>
<li style="text-align: left;">started automatically when the system launches,</li>
<li style="text-align: left;">automatically restarted in case of errors,</li>
<li style="text-align: left;">at any time started, paused, stopped or restarted.</li>
</ul>
<p style="text-align: left;">Even if you&#8217;ve never developed any service applications, you are using many of them &#8211; check your <em>services.msc</em><code></code> to see the list of services currently running in your system.</p>
<h2>Topshelf</h2>
<p style="text-align: left;">Visual Studio 2015 provides a template to create Windows Service application and probably I could finish my post here. However, I would like to share with you an <strong>even easier</strong> way of building services: to do it with <em>Topshelf</em> &#8211; a very lightweight open-source library, which purpose it to make services development, debugging and deployment painless. <em>Topshelf</em> allows the developer to focus on service&#8217;s functionality, instead of wasting time interacting with .NET Framework&#8217;s build-in services support. There is also no more need to install your service using <em>InstallUtil </em>or learn how to attach your Visual Studio debugger to running service to detect issues.</p>
<p style="text-align: left;">Using <em>Topshelf</em> it&#8217;s enough to create console application, add one service class with public <code>Start</code> and <code>Stop</code> methods, configure service&#8217;s startup parameters (its name, display name, description etc.) and you&#8217;re ready to go. Below we&#8217;ll see how to do it.</p>
<h2 style="text-align: left;">Creating Windows Service application</h2>
<p style="text-align: left;">After creating console application in Visual Studio, add <em>Topshelf</em> to the project by installing it from <em>Nuget</em>:</p>
<p style="text-align: left;"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?ssl=1"><img data-recalc-dims="1" fetchpriority="high" decoding="async" data-attachment-id="51" data-permalink="https://www.codejourney.net/windows-services-using-topshelf/attachment/1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?fit=955%2C91&amp;ssl=1" data-orig-size="955,91" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Topshelf &amp;#8211; Nuget" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?fit=300%2C29&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?fit=955%2C91&amp;ssl=1" class="alignleft size-full wp-image-51" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?resize=955%2C91&#038;ssl=1" alt="Topshelf - Nuget" width="955" height="91" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?w=955&amp;ssl=1 955w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?resize=300%2C29&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/1.png?resize=768%2C73&amp;ssl=1 768w" sizes="(max-width: 955px) 100vw, 955px" /></a></p>
<h2 style="text-align: left;">Adding service class</h2>
<p style="text-align: left;">Then, create service class, which is responsible for executing your service&#8217;s logic:</p>
<pre><pre class="brush: csharp; title: ; notranslate">
class FileWatcherService
{
   public bool Start()
   {
      // Initialize your service application here
      return true;
   }


   public bool Stop()
   {
      // Executed when the service is stopped
      // Dispose your objects here etc.
      return true;
   }
}
</pre>
<p style="text-align: left;">Both of those methods return <code>boolean</code> to indicate that starting/stopping of the service completed successfully.</p>
<h2 style="text-align: left;">Adding service configuration</h2>
<p style="text-align: left;">Now we only need to configure the service&#8217;s parameters. <em>Topshelf</em> provides <code>HostFactory</code> class which contains <code>Run</code> method, to which we can pass fluently-created set of settings:</p>
<ul style="text-align: left;">
<li>service class instance,</li>
<li>methods to be executed while the service is started/stopped/paused/continued,</li>
<li>service&#8217;s name, display name, description,</li>
<li>start/stop timeouts,</li>
<li>startup type (automatic, manual, automatic delayed),</li>
<li>&#8230; <a href="http://docs.topshelf-project.com/en/latest/configuration/config_api.html">and many more</a>.</li>
</ul>
<p style="text-align: left;">The set-up code should be put in the <code>Main</code> method of <code>Program.cs</code>. In our case, the simplest configuration could look as follows:</p>
<pre><pre class="brush: csharp; title: ; notranslate">
static void Main(string&#x5B;] args)
{
    HostFactory.Run(serviceConfig =&amp;amp;amp;amp;amp;gt;
    {
        serviceConfig.Service&amp;amp;amp;amp;amp;lt;FileWatcherService&amp;amp;amp;amp;amp;gt;(serviceInstance =&amp;amp;amp;amp;amp;gt;
        {
            serviceInstance.ConstructUsing(() =&amp;amp;amp;amp;amp;gt; new FileWatcherService());
            serviceInstance.WhenStarted(execute =&amp;amp;amp;amp;amp;gt; execute.Start());
            serviceInstance.WhenStopped(execute =&amp;amp;amp;amp;amp;gt; execute.Stop());
        });

        serviceConfig.SetServiceName(&quot;FileWatcherService&quot;);
        serviceConfig.SetDisplayName(&quot;File Watcher Service&quot;);
        serviceConfig.SetDescription(&quot;Service monitoring local folders for file system events&quot;);
        serviceConfig.StartAutomatically();

    });
}
</pre>
<h2 style="text-align: left;">Debugging the service</h2>
<p style="text-align: left;">Ease of implementation and configuration of the service application isn&#8217;t the only benefit of using <em>Topshelf</em>. Our application has been created as a console app, so it can be simply run and debugged by using F5 button. No more need to attach debugger to the service itself 🙂</p>
<h2 style="text-align: left;">Installing the service</h2>
<p style="text-align: left;"><em>Topshelf</em> also adds some &#8220;shell layer&#8221; to our application. To install executable file (which is the output of our console application) into Windows Service Control Manager (SCM) to be running as a &#8220;real&#8221; Windows Service, it&#8217;s enough to open the command line, navigate to the folder where the <em>.exe</em> file is present and install it passing <code>install</code> as <em>.exe&#8217;s</em> parameter (e.g.: <code>FileWatcherService.exe install)</code>. After executing it, we can see our service created by looking into <i>services.msc</i>:</p>
<p style="text-align: left;"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?ssl=1"><img data-recalc-dims="1" decoding="async" data-attachment-id="72" data-permalink="https://www.codejourney.net/windows-services-using-topshelf/attachment/2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?fit=1283%2C125&amp;ssl=1" data-orig-size="1283,125" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Installed service" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?fit=300%2C29&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?fit=1024%2C100&amp;ssl=1" class="size-full wp-image-72 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?resize=1140%2C111&#038;ssl=1" alt="Installed service" width="1140" height="111" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?w=1283&amp;ssl=1 1283w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?resize=300%2C29&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?resize=768%2C75&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2016/05/2.png?resize=1024%2C100&amp;ssl=1 1024w" sizes="(max-width: 1140px) 100vw, 1140px" /></a></p>
<p>
It&#8217;s similarly easy to uninstall the service.</p>
<h2 style="text-align: left;">Not only Windows</h2>
<p style="text-align: left;">The official <i>Topshelf&#8217;s</i> project&#8217;s page says:</p>
<blockquote>
<p>Topshelf works with Mono, making it possible to deploy services to Linux. The service installation features are currently Windows only, but others are working on creating native host environment support so that installation and management features are available as well.</p>
</blockquote>
<p style="text-align: left;">So it&#8217;s the next great benefit of <i>Topshelf</i> to use applications built with it on Linux.</p>
<h2 style="text-align: left;">More</h2>
<p style="text-align: left;">If you want to check what&#8217;s more that <i>Topshelf</i> can do, you can visit <a href="http://topshelf-project.com/">project&#8217;s official page</a>, check it&#8217;s <a href="https://github.com/topshelf/topshelf">GitHub repository</a> or watch <a href="https://www.pluralsight.com/courses/topshelf-getting-started-building-windows-services">this very good Pluralsight course</a>.</p>
<h2 style="text-align: left;">Summary</h2>
<p style="text-align: left;">Summing up, I found <i>Topshelf</i> very handy in developing service applications. One of the major pros of using it is ease of installation &#8211; it may become very important in the production environment, especially when dealing with any kind of distributed environment. Moreover, debugging the service as a console application makes fixing bugs quick and comfortable. I recommend everyone to use it in case of a need to write any service application.</p>
<p>The post <a href="https://www.codejourney.net/windows-services-using-topshelf/">Building Windows Services using Topshelf</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/windows-services-using-topshelf/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">26</post-id>	</item>
	</channel>
</rss>
