<?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>host asp net &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/host-asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Wed, 30 Mar 2022 05:32:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>host asp net &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Host ASP.NET Core in Window Service</title>
		<link>https://topreviewhostingasp.net/how-to-host-asp-net-core-in-window-service/</link>
					<comments>https://topreviewhostingasp.net/how-to-host-asp-net-core-in-window-service/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Wed, 30 Mar 2022 05:29:33 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net core]]></category>
		<category><![CDATA[asp net core tips]]></category>
		<category><![CDATA[asp net core tutorial]]></category>
		<category><![CDATA[asp net core windows service]]></category>
		<category><![CDATA[host asp net]]></category>
		<category><![CDATA[host asp net core]]></category>
		<category><![CDATA[windows service]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3025</guid>

					<description><![CDATA[Did you know that we can host ASP.NET service on Windows with Windows Service without using IIS? The benefit of hosting on Windows Service is that the application will automatically restart after the server reboot. This functionality is implemented using a Worker Service Template that becomes the initial point for writing and building long-running service [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Did you know that we can host ASP.NET service on Windows with Windows Service without using IIS? The benefit of hosting on Windows Service is that the application will automatically restart after the server reboot.</p>



<p>This functionality is implemented using a Worker Service Template that becomes the initial point for writing and building long-running service applications. We are focusing on a solution without the IIS because the same may not be available at all times.</p>



<p>Even in the situations where you have IIS already set up, it may not host .NET Core applications, let alone ASP.NET Core. Hence, to host them, you need to understand the implementation of the .NET Core web application in Visual Studio.</p>



<h2 class="wp-block-heading"><strong>Why Use a Web App as a Windows Service?</strong></h2>



<p>In situations where you want to host .NET Core applications without IIS, we can work with self-contained deployment. Here, the application basically runs as a .exe file.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.asphostportal.com" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" width="300" height="271" class="wp-image-2584 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01-50x45.png 50w" sizes="(max-width: 300px) 100vw, 300px" /></a></figure>
</div>



<p>Follow these steps to set up a hosting environment for ASP.NET 3.1 in Windows Service.</p>



<h3 class="wp-block-heading"><strong>1. Create new ASP.NET Core 3.1 Application in Visual Studio</strong></h3>



<p>Create a new project in VS2019 .NetCore web application. Specify version 3.1 as you create a new project in the folder (follow the screenshot).</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="543" height="307" class="wp-image-3026 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/aspnetcore-1.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/aspnetcore-1.png 543w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/aspnetcore-1-300x170.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/aspnetcore-1-50x28.png 50w" sizes="(max-width: 543px) 100vw, 543px" /></figure>
</div>



<h3 class="wp-block-heading"><strong>2. Running an ASP.Net Core application as a Windows Service</strong></h3>



<p>Following the first step, specify a runtime for the application. ASP.NET Core also supports operating systems where Windows Services don’t run.</p>



<p>For this, we must modify the project file. To modify the project file, double click on the Project name and add the following two lines as per the screenshot.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="779" height="316" class="wp-image-3027 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio.png 779w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-300x122.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-768x312.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-50x20.png 50w" sizes="(max-width: 779px) 100vw, 779px" /></figure>
</div>



<p>Add a reference using NuGet package <strong>Microsoft.ASPNetCore.Hosting.WindowsServices</strong> and <strong>Newtonsoft.json</strong>.</p>



<p>This Package has everything needed to run an ASP.Net Core application as a Window service.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="709" height="249" class="wp-image-3028 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-2.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-2.png 709w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-2-300x105.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-2-50x18.png 50w" sizes="(max-width: 709px) 100vw, 709px" /></figure>
</div>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="718" height="243" class="wp-image-3029 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-3.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-3.png 718w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-3-300x102.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-3-50x17.png 50w" sizes="(max-width: 718px) 100vw, 718px" /></figure>
</div>



<p>Now, run the application as Windows Service and configure it in the Program.cs file. In case you want to run an application as IIS Express, then there is no need to change any configuration setting, and we can directly run an application as IIS Express.</p>



<p>We can specify a different port in the <strong>appsettings.json</strong> and <strong>appsettings.Devlopment.json</strong> file to run an application on this port directly using the Windows service.</p>



<p>Specify Port no 5009.</p>



<p><strong><u>appsettings.json File:</u></strong></p>



<pre class="wp-block-code"><code>{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
	}
  },
"AllowedHosts": "*",
"ServicePort" :  5009
}
 appsettings.Devlopment.json file:
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
	}
  },
"ServicePort": 5009</code></pre>



<p>Moving on, to specify our <strong>appsettings.json</strong> file as Configuration path in <strong>Program.cs</strong> file to let the application read that path and run on that port. See the screenshots below.</p>



<p><strong>Program.csFile:</strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="944" height="590" class="wp-image-3030 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-4.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-4.png 944w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-4-300x188.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-4-768x480.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-4-50x31.png 50w" sizes="(max-width: 944px) 100vw, 944px" /></figure>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="944" height="484" class="wp-image-3031 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-5.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-5.png 944w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-5-300x154.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-5-768x394.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-5-50x26.png 50w" sizes="(max-width: 944px) 100vw, 944px" /></figure>
</div>



<h3 class="wp-block-heading"><strong>3.</strong> <strong>Build the application and Publish the code</strong></h3>



<p>With the above-given configuration completed in the <strong>Program.cs</strong> file start building the application, followed by publishing the code in the folder.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="944" height="489" class="wp-image-3032 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-6.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-6.png 944w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-6-300x155.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-6-768x398.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/visual-studio-6-50x26.png 50w" sizes="(max-width: 944px) 100vw, 944px" /></figure>



<h3 class="wp-block-heading"><strong>4. Making the application Run as WindowsService</strong></h3>



<p>To make the application run as a Windows service follow these steps;</p>



<ul>
<li>Open the Command line with administrative Permission.</li>
<li>Register the application as a Windows service using this command (<strong>space after “binPath=” is mandatory</strong>)</li>
</ul>



<p><strong>Create windows service using the following command</strong>:</p>



<p><strong>sc create AspNetCoreWindowsServicebinPath = “path to my publish folder application exe –service”</strong></p>



<p>In the example above, we have taken <strong>AspNetCoreWindowsService</strong> as the name; you can use any name you want to keep. Also, <strong>–service</strong> is an argument name that we pass in <strong>Program.cs</strong> file.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="862" height="466" class="wp-image-3033 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/cmd.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/cmd.png 862w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/cmd-300x162.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/cmd-768x415.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/cmd-50x27.png 50w" sizes="(max-width: 862px) 100vw, 862px" /></figure>
</div>



<p>Nest, open the <strong>services.msc</strong> and see your hosted Windows service with the name <strong>AspNetCoreWindowsService</strong> to start the service.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="879" height="490" class="wp-image-3034 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/service.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/service.png 879w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/service-300x167.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/service-768x428.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/service-50x28.png 50w" sizes="(max-width: 879px) 100vw, 879px" /></figure>
</div>



<p>When the service starts, open the browser and navigate to <a href="http://localhost:50009/"><strong>http://localhost:50009</strong></a> to see the web application is running. If you would remember, we had set port 5009 in the earlier steps of ASP.NET core web application development. Specifically, look for the part where we had specified it in the <strong>appsetting.json</strong> file as mentioned above.</p>



<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="840" height="494" class="wp-image-3035 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2022/03/localhost.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/03/localhost.png 840w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/localhost-300x176.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/localhost-768x452.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/03/localhost-50x29.png 50w" sizes="(max-width: 840px) 100vw, 840px" /></figure>
</div>



<p>Delete windows service using the following command:</p>



<p>Stop the existing running instance before releasing the new version of the service. Once done, write the following command.</p>



<p><strong>scdeleteAspNetCoreWindowsService</strong></p>



<p>Again, <strong>AspNetCoreWindowsService i</strong>s the windows service name that we have set to give you an example.</p>



<h4 class="wp-block-heading"><strong>Conclusion</strong></h4>



<p>This blog takes you through the steps to set up a hosting environment for <strong>ASP.NET Core 3.1 web application development in Windows Service</strong>. Although the process is simple enough to understand, we at <strong>DEV IT</strong> have understood the nuances of the same along with knowing everything about <strong>agile development practices</strong>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-host-asp-net-core-in-window-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
