<?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>Windows Service to Upload to Azure &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/windows-service-to-upload-to-azure/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:13:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Windows Service to Upload to Azure &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Asp.Net Core Windows Service Task Scheduler Daily, Weekly, Monthly</title>
		<link>https://sibeeshpassion.com/asp-net-core-windows-service-task-scheduler-daily-weekly-monthly/</link>
					<comments>https://sibeeshpassion.com/asp-net-core-windows-service-task-scheduler-daily-weekly-monthly/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 12 Aug 2019 19:15:45 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Asp Net Core Windows Service]]></category>
		<category><![CDATA[Azure Blob Storage]]></category>
		<category><![CDATA[Azure Storage]]></category>
		<category><![CDATA[NLog]]></category>
		<category><![CDATA[NLog in Windows Service]]></category>
		<category><![CDATA[Quartz Scheduler]]></category>
		<category><![CDATA[Quartz Scheduler with Dependency Injection]]></category>
		<category><![CDATA[Windows Service]]></category>
		<category><![CDATA[Windows Service to Upload to Azure]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13876</guid>

					<description><![CDATA[[toc] Introduction Windows services are a good way for reducing some manual jobs that we have to do in our system. In this article, we are going to write a windows service using Asp.Net core, the jobs of this windows service is given below. Zip the folder and save the file to a particular directory Upload the zipped folder to the Azure blob storage The above mentioned tasks will be running daily, weekly, monthly. We are using Quartz scheduler with Dependency Injection to do this amazing tasks. We are using Nlog for logging the details of our tasks. If you [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>[toc]</p>



<h2 class="wp-block-heading">Introduction</h2>



<p>Windows services are a good way for reducing some manual jobs that we have to do in our system. In this article, we are going to write a windows service using Asp.Net core, the jobs of this windows service is given below.</p>



<ol class="wp-block-list"><li> Zip the folder and save the file to a particular directory </li><li>Upload the zipped folder to the Azure blob storage</li></ol>



<p>The above mentioned tasks will be running daily, weekly, monthly. We are using Quartz scheduler with Dependency Injection to do this amazing tasks. We are using Nlog for logging the details of our tasks. If you think this is something that you are interested at, then you are at the right place, let us develop our PerfectScheduler. </p>



<h2 class="wp-block-heading">Source Code</h2>



<p>The source code of this project is been uploaded to the GitHub, please feel free to fork, star, create pull requests etc. The repository can be found <a href="https://github.com/SibeeshVenu/Perfect-Scheduler"><strong><em>here</em></strong></a>.</p>



<h2 class="wp-block-heading">Creating Perfect Scheduler</h2>



<p>As I mentioned earlier, we are creating a windows service with Asp.Net core, technically there is no straight way to do this, as the Windows Service with Asp.Net core template is not available in Visual Studio.</p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="649" height="311" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/No-Windows-Service-Project-Template.png" alt="" class="wp-image-13879" srcset="/wp-content/uploads/2019/08/No-Windows-Service-Project-Template.png 649w, /wp-content/uploads/2019/08/No-Windows-Service-Project-Template-300x144.png 300w, /wp-content/uploads/2019/08/No-Windows-Service-Project-Template-425x204.png 425w" sizes="(max-width: 649px) 100vw, 649px" /><figcaption>No Windows Service Project Template</figcaption></figure>



<p> so we will be creating an Asp.Net console application and then install executable file generated as a windows service. </p>



<figure class="wp-block-image"><img decoding="async" width="649" height="212" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Net-Core-Console-Application.png" alt="" class="wp-image-13880" srcset="/wp-content/uploads/2019/08/Net-Core-Console-Application.png 649w, /wp-content/uploads/2019/08/Net-Core-Console-Application-300x98.png 300w, /wp-content/uploads/2019/08/Net-Core-Console-Application-425x139.png 425w" sizes="(max-width: 649px) 100vw, 649px" /><figcaption>.Net Core Console Application</figcaption></figure>



<h3 class="wp-block-heading">Creating an Asp.Net Console Application</h3>



<p>Open Visual Studio and search for the project template Console App(.Net Core) and name the solution as per your convenience, I am naming this project as Perfect Scheduler, as I am thinking that we, the community people can make this windows service perfect by creating many pull requests.</p>



<p> Once you had created the application, let us install all of our dependencies at once, so that we don&#8217;t need to worry about them later. </p>



<figure class="wp-block-image"><img decoding="async" width="650" height="640" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Dependencies.png" alt="" class="wp-image-13881" srcset="/wp-content/uploads/2019/08/Dependencies.png 650w, /wp-content/uploads/2019/08/Dependencies-300x295.png 300w, /wp-content/uploads/2019/08/Dependencies-425x418.png 425w" sizes="(max-width: 650px) 100vw, 650px" /><figcaption>Asp.Net Core Windows Service Dependencies</figcaption></figure>



<h3 class="wp-block-heading">Write the Service</h3>



<p>As we have installed all of our dependencies, let us create our service now. Add a new class with the name BackupService and inherit the same from IHostedService, which is part of Microsoft.Extensions.Hosting namespace.</p>



<p>The IHostedService has two methods in it as follows, so we should implement them in our service class.</p>



<script src="https://gist.github.com/SibeeshVenu/2a9cb76d58645085c3704d533a0a6a73.js"></script>



<h4 class="wp-block-heading">StartAsync</h4>



<p>The StartAsync method can be implemented as preceding.</p>



<script src="https://gist.github.com/SibeeshVenu/1e39b9749d12aac8acc1af98a2da4469.js"></script>



<p>As you can see that in the first line of the above code, we are getting the scheduler of Quartz. Let us create a method and return a scheduler then.</p>



<script src="https://gist.github.com/SibeeshVenu/afd74a512ddb7c8bd6d35868c263bd30.js"></script>



<p>The next step is to build a service provider so that we can inject our dependencies using Constructor Dependency Injection. By default the Quartz is not doing this, so we have to build the configuration our own.</p>



<script src="https://gist.github.com/SibeeshVenu/914f9ee0934d63234f81f16c5ffb2fcc.js"></script>



<p>As you can see that we have configured the services for DailyJob, WeeklyJob, MonthlyJob and HelperService. No worries, we will be creating these Classes and Interfaces very soon. </p>



<p>Once we get the Service Provider, we can pass this to our Custom Job Factory, which we will be implementing soon. Now, we can start the scheduler and schedule our jobs, please make sure that you are using different Identity names for both Triggers and Jobs. The samples are given below.</p>



<script src="https://gist.github.com/SibeeshVenu/2b6fa57cb5edca68431f59133ada6de2.js"></script>



<h4 class="wp-block-heading">StopAsync</h4>



<p>The StopAsync method can be implemented as follows</p>



<script src="https://gist.github.com/SibeeshVenu/36a4edb6866bbd9394a2eeb4a4abba5f.js"></script>



<h3 class="wp-block-heading">Creating the JobBuilders and TriggerBuilders</h3>



<p>Now we can create the Interfaces and Classes for our Jobs, which are Daily, Weekly, Monthly. To do so, let us create a new folder called Helpers and then create a folder called Interfaces inside.  Below are the Interfaces you need to create then.</p>



<h4 class="wp-block-heading">IDailyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/2a06425a40aa642c420616a9dc438ea4.js"></script>



<h4 class="wp-block-heading">IWeeklyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/745be5f74479f91a24d454c4b4ee113d.js"></script>



<h4 class="wp-block-heading">IMonthlyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/43de235532540cd5cd6ff136ad07e9e6.js"></script>



<p>Let us also create an Interface for our HelperService.</p>



<h4 class="wp-block-heading">IHelperService</h4>



<script src="https://gist.github.com/SibeeshVenu/4aa11ef3c39e323a40de2b06abfbccfd.js"></script>



<p>The parameter schedule is for determining which job it is, the value of this parameter can be daily, weekly, monthly.</p>



<script src="https://gist.github.com/SibeeshVenu/308a0cc4b620f6c65f3f47738863db75.js"></script>



<h3 class="wp-block-heading">Create a Custom Job Factory</h3>



<p>To implement the Dependency Injection, we need to create our own custom job factory. Create a class inside the Helper folder as preceding.</p>



<script src="https://gist.github.com/SibeeshVenu/a2e10e1228e65ace98e45397d7c9d849.js"></script>



<h3 class="wp-block-heading">Implement the Job Builder Interfaces</h3>



<p>Now it is time to implement our Daily, Weekly, Monthly job builders. </p>



<h4 class="wp-block-heading">DailyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/d34af66c20232409c4bb15852c88aabf.js"></script>



<h4 class="wp-block-heading">WeeklyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/d021c1dc0be1c03cff9147ac3d4d231c.js"></script>



<h4 class="wp-block-heading">MonthlyJob</h4>



<script src="https://gist.github.com/SibeeshVenu/3b72c9acb9ad0e00887ecbee532d0438.js"></script>



<p>As you can see that we have configured separate classes for our each jobs with the dependency IHelperService injected via constructor. In the future we should be able to write custom logic for each jobs here as it is in separate classes.</p>



<h3 class="wp-block-heading">Implement HelperService</h3>



<p>Before we start implementing this service let us configure the NLog now as we will be writing logs from this class. </p>



<h4 class="wp-block-heading">Configure NLog</h4>



<p>To configure, NLog, create a new configuration file NLog.config and edit the content as preceding.</p>



<script src="https://gist.github.com/SibeeshVenu/7eaab92256f71a5c660a83564635b0dd.js"></script>



<p>Now, create a method SetUpNLog() and add the codes as below.</p>



<script src="https://gist.github.com/SibeeshVenu/82dbe1dd2ea19f5eda4f6fc9342880fc.js"></script>



<p>Please make sure that you have already added a property ILogger _logger. </p>



<p>We can implement the HelperService as preceding with all the necessary private and public methods.</p>



<script src="https://gist.github.com/SibeeshVenu/bc02d3f5621826c2a085970c6e47b004.js"></script>



<p>The method PerformService(string schedule) will be called for every schedule and it will make sure that the below tasks are performed.</p>



<ol class="wp-block-list"><li>Zip the folder and save the file to a particular directory</li><li>Upload the zipped file to the Azure blob storage</li></ol>



<p>Here, the values of the the variable and the blob storage container names are same, that is, daily, weekly, monthly. If you are not sure how you can upload a blob to Azure storage account, I strongly recommend you to read my article <a href="https://sibeeshpassion.com/timertrigger-azure-function-to-upload-to-azure-blob-daily/#azure-function-and-azure-blob"><strong><em>here</em></strong></a>.</p>



<p>Remember to set the values for the FolderToZipLocation(To which location the file should be saved), FolderFromZipLocation (From where the files should be taken), StorageConnectionString in the App.config file.</p>



<script src="https://gist.github.com/SibeeshVenu/1a2a3f26bf5e7a73e3b0af8aa3824172.js"></script>



<h3 class="wp-block-heading">Setting Up The Program</h3>



<p>As you all know that Program class it the start of our console application, not it is time to call our BackupService from the Program class. Let us edit the code of the Program class as below.</p>



<script src="https://gist.github.com/SibeeshVenu/55d3dea19acdf8f3416269f7911c5916.js"></script>



<p>If we are running the application locally/debug, we are calling the extension method RunConsoleAsync() or else we call our own custom extension method RunTheServiceAsync(). The line services.AddHostedService() is very important as this is where we register our IHostedService, which is BackupService. Below is the code for our extension method.</p>



<script src="https://gist.github.com/SibeeshVenu/b4950ef1a36a91800fd9bf9f5a9f02a3.js"></script>



<p>The ServiceLifetime class is where we override the methods from ServiceBase class. You can find the implementation as follows.</p>



<script src="https://gist.github.com/SibeeshVenu/9c1e42e7f697b60375d1a5975c5b0f53.js"></script>



<h2 class="wp-block-heading">Creating the Windows Service</h2>



<p>As we have already created the Asp.Net Core console application, now it is time to create a windows service from it. Make sure that you had set the RuntimeIdentifier to win7-x64 and SelfContained to true in the properties of your project, this will make sure that all of your dependencies are being added to your executable file, so that you don&#8217;t need to worry about handling your dependencies manually. You can read more about it <a href="https://docs.microsoft.com/en-us/dotnet/core/deploying/#self-contained-deployments-scd"><strong><em>here</em></strong></a>. To do so, right click on the project and click on Edit Project File. At the end, your csproj file should be similar as preceding.</p>



<script src="https://gist.github.com/SibeeshVenu/4772a4e34662dc09e7421a12064bcc28.js"></script>



<h3 class="wp-block-heading">Create the Release Configuration</h3>



<p>You should also run the dotnet publish command with the release configuration before you try to install the service, because you need this executable file to install the service.</p>



<p>Open the command prompt with administrator privilege and go to the project root folder using the cd command. Run the command &#8220;dotnet publish &#8211;configuration=release&#8221; .</p>



<p>This will generate everything you wanted. If you run the command correctly, you should see an output as preceding.</p>



<figure class="wp-block-image"><img decoding="async" width="651" height="400" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Dotnet-Publish-Release.png" alt="" class="wp-image-13883" srcset="/wp-content/uploads/2019/08/Dotnet-Publish-Release.png 651w, /wp-content/uploads/2019/08/Dotnet-Publish-Release-300x184.png 300w, /wp-content/uploads/2019/08/Dotnet-Publish-Release-425x261.png 425w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>Dotnet Publish Release</figcaption></figure>



<p>Now go to the bin folder and then release folder, you should see a folder with the name netcoreapp2.1, inside this folder, there will be a folder win7-x64, this is the folder where your exe file, log file and other items are residing. </p>



<h3 class="wp-block-heading">Install the Service</h3>



<p>To install our Asp.Net console application as a windows service, you can use the sc command. Open the command prompt with administrator privilege and run the preceding command.</p>



<script src="https://gist.github.com/SibeeshVenu/cb8761b926f41000658ec14b9dcf1799.js"></script>



<p>And then you can start the service by running the command as &#8220;sc start BackupService&#8221;. This should start your service.</p>



<h3 class="wp-block-heading">Giving Permission to the Folders</h3>



<p>Sometimes you may get some permission issue in your service as it doesn&#8217;t have enough permission to read the files form the C drive. To overcome this, you should give enough permission to the user. You can do this by editing the security properties of those folders.</p>



<figure class="wp-block-image"><img decoding="async" width="649" height="412" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Setting-Permission-for-the-Users.png" alt="" class="wp-image-13886" srcset="/wp-content/uploads/2019/08/Setting-Permission-for-the-Users.png 649w, /wp-content/uploads/2019/08/Setting-Permission-for-the-Users-300x190.png 300w, /wp-content/uploads/2019/08/Setting-Permission-for-the-Users-425x270.png 425w" sizes="(max-width: 649px) 100vw, 649px" /><figcaption>Setting Permission for the Users</figcaption></figure>



<p>Do the same for the BackupZip folder as well.</p>



<h2 class="wp-block-heading">Output</h2>



<p>If you have run the service correctly, you should see a log file with the name  backupclientlogfile.txt inside your  win7-x64  folder. Once the service is run,  the logs will be written as follows.</p>



<script src="https://gist.github.com/SibeeshVenu/8173f2129e1f79fa4689e7d25d761807.js"></script>



<p>You can also check your Azure Storage account to check whether the files have uploaded correctly or not.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Wow!. Now we have learned,</p>



<ul class="wp-block-list"><li>about Windows Service and Asp.Net Console Application </li><li>about How to create a Windows Service using Asp.Net Core</li><li>about How to use Quartz scheduler</li><li>about How to use NLog in Windows Service</li><li>about How to configure Quartz scheduler to use Dependency Injection</li><li>about How to Zip a folder and save the file</li><li>about How to upload a file to the Azure blob storage</li></ul>



<h2 class="wp-block-heading">Your turn. What do you think?</h2>



<p>Thanks a lot for reading. Did I miss anything that you may think which is needed in this article? Could you find this post as useful? Kindly do not forget to share me your feedback.</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sibeeshpassion.com/asp-net-core-windows-service-task-scheduler-daily-weekly-monthly/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
