<?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>Quartz With Database &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/quartz-with-database/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:18:27 +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>Quartz With Database &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Asp.Net Core Windows Service Task Quartz.Net With Database</title>
		<link>https://sibeeshpassion.com/asp-net-core-windows-service-task-quartz-net-with-database/</link>
					<comments>https://sibeeshpassion.com/asp-net-core-windows-service-task-quartz-net-with-database/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 29 Aug 2019 12:46:07 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Asp.Net Core]]></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[Quartz SQL Server]]></category>
		<category><![CDATA[Quartz With Database]]></category>
		<category><![CDATA[Windows Service]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13893</guid>

					<description><![CDATA[[toc] Introduction In our previous article we have already seen that how we can use Quartz.Net scheduler in our Asp.Net core console application and then run it as a windows service. The problem with that approach is that, the scheduler information will be lost once the system restarts or something bad happens. To rectify that problem we can actually save all the scheduler information in a database, luckily the Quartz.Net scheduler supports this and we can easily implement it. I hope the introduction is clear and let&#8217;s jump on to the implementation. Source Code The source code can be found [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>[toc]</p>



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



<p>In our previous article we have already seen that how we can <a href="https://medium.com/better-programming/asp-net-core-windows-service-task-scheduler-daily-weekly-monthly-700a569d502a"><strong><em>use Quartz.Net scheduler in our Asp.Net core console application and then run it as a windows service</em></strong></a>. The problem with that approach is that, the scheduler information will be lost once the system restarts or something bad happens. To rectify that problem we can actually save all the scheduler information in a database, luckily the Quartz.Net scheduler supports this and we can easily implement it. I hope the introduction is clear and let&#8217;s jump on to the implementation.</p>



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



<p>The source code can be found <a href="https://github.com/SibeeshVenu/Perfect-Scheduler"><strong><em>in this GitHub repository</em></strong></a>. Please feel free to star, fork or do whatever you wish. You can also consider follow me on GitHub. LOL. </p>



<h2 class="wp-block-heading">Setting Up Database</h2>



<p>Before we start the coding, let us set up our database. You can choose any database as you wish. I am choosing SQL Server Express.</p>



<h3 class="wp-block-heading">Install SQL Server Express</h3>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1024" height="812" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Install-SQl-Express-1024x812.png" alt="" class="wp-image-13894" srcset="/wp-content/uploads/2019/08/Install-SQl-Express-1024x812.png 1024w, /wp-content/uploads/2019/08/Install-SQl-Express-300x238.png 300w, /wp-content/uploads/2019/08/Install-SQl-Express-768x609.png 768w, /wp-content/uploads/2019/08/Install-SQl-Express-425x337.png 425w, /wp-content/uploads/2019/08/Install-SQl-Express.png 1690w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>SQL Server Express</figcaption></figure>



<h3 class="wp-block-heading">Enable sa Login</h3>



<p>We should enable the sa login which we will use in our Quartz configuration. If you try to configure the Quartz with windows authentication, you will get an error as below.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>2019-08-29 11:34:02.3570|INFO|Quartz.Core.QuartzScheduler|JobFactory set to: Backup.Service.Helpers.CustomJobFactory<br> 2019-08-29 11:34:12.6801|INFO|Quartz.Impl.AdoJobStore.JobStoreTX|ConnectionAndTransactionHolder passed to RollbackConnection was null, ignoring<br> 2019-08-29 11:34:12.6892|ERROR|Quartz.Impl.AdoJobStore.JobStoreTX|Failure occurred during job recovery: Failed to obtain DB connection from data source &#8216;default&#8217;: System.Data.SqlClient.SqlException (0x80131904): Cannot open database &#8220;BackupClientSchedules&#8221; requested by the login. The login failed.<br> Login failed for user &#8216;WORKGROUP\DESKTOP-3EF5B65$&#8217;.<br>    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)<br>    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)<br>    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)<br>    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)<br>    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)<br>    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)<br>    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection)<br>    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions)    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource</code>1 retry)<br>    at System.Data.SqlClient.SqlConnection.Open()<br>    at Quartz.Impl.AdoJobStore.JobStoreSupport.GetConnection() in C:\projects\quartznet\src\Quartz\Impl\AdoJobStore\JobStoreSupport.cs:line 323<br> ClientConnectionId:2efd5ab8-71aa-4598-b5ca-63c5aaa6afbe<br> Error Number:4060,State:1,Class:11</p></blockquote>



<p>Yes, that is such a log error, and we don&#8217;t want any errors right. So let us configure sa login first. I have already explained how you can do that , in one of my StackOverflow answer, you can see that <a href="https://stackoverflow.com/questions/3583605/login-failed-for-user-sa-the-user-is-not-associated-with-a-trusted-sql-server/57707682#57707682"><strong><em>here</em></strong></a>. </p>



<h3 class="wp-block-heading">Create Database and Tables Needed</h3>



<p>Now we need to create a database and some tables where we can save the scheduler and trigger information. You can always see the table schema here in <a href="https://github.com/quartznet/quartznet/blob/master/database/tables/tables_sqlServer.sql"><strong><em>this official Quartz GitHub repository</em></strong></a>. You can also find the same file in our source code.</p>



<p>Before you run that query in your SQL Server Management Studio, make sure to create a database first and the use the same.</p>



<pre class="wp-block-code"><code>CREATE database BackupClientSchedules
USE BackupClientSchedules</code></pre>



<p>Now id you see inside the database BackupClientSchedules, you should see some tables with no data inside.</p>



<figure class="wp-block-image"><img decoding="async" width="646" height="1024" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Quartz-Net-Tables-646x1024.png" alt="" class="wp-image-13895" srcset="/wp-content/uploads/2019/08/Quartz-Net-Tables-646x1024.png 646w, /wp-content/uploads/2019/08/Quartz-Net-Tables-189x300.png 189w, /wp-content/uploads/2019/08/Quartz-Net-Tables-347x550.png 347w, /wp-content/uploads/2019/08/Quartz-Net-Tables.png 650w" sizes="(max-width: 646px) 100vw, 646px" /><figcaption>Quartz Net Tables</figcaption></figure>



<h2 class="wp-block-heading">Configure Quartz.Net to Use Database Instead of RAM</h2>



<p>As we have already set up our database, now we can start configuring the service. If you are new Quartz.Net scheduler, I strongly recommend you to read my previous <a href="https://sibeeshpassion.com/asp-net-core-windows-service-task-scheduler-daily-weekly-monthly/"><strong><em>article</em></strong></a>. </p>



<h3 class="wp-block-heading">Edit App.config</h3>



<p>Let&#8217;s change the App.config file as preceding.</p>



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



<p>Note that we have set  quartz.dataSource.default.provider  as Sqlserver and also given the  quartz.dataSource.default.connectionString. You should change these values with your provider and connection string.</p>



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



<p>We should install the Nuget package Quartz.Serialization.Json in our application. </p>



<h3 class="wp-block-heading">Setup the Scheduler</h3>



<p>In our previous article, we have see how we can setup the scheduler and we were using a function called GetScheduler for the same. Now we can edit the code of that function as preceding.</p>



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



<p>Please note the comments in the above code block.</p>



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



<p>Once we finished the above steps, we can run dotnet publish command with release configuration and install the service as preceding.</p>



<figure class="wp-block-image"><img decoding="async" width="1024" height="568" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/SC-Commands-1024x568.png" alt="" class="wp-image-13896" srcset="/wp-content/uploads/2019/08/SC-Commands-1024x568.png 1024w, /wp-content/uploads/2019/08/SC-Commands-300x166.png 300w, /wp-content/uploads/2019/08/SC-Commands-768x426.png 768w, /wp-content/uploads/2019/08/SC-Commands-425x236.png 425w, /wp-content/uploads/2019/08/SC-Commands.png 1838w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>SC Commands</figcaption></figure>



<p>Again, read my previous article, if you need a detailed explanations about this step.</p>



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



<p>Once the service started successfully, you should be able to see two log files backupclientlogfile_backupservice.txt and backupclientlogfile_helperservice.txt in our win7-x64 folder. You can see all the logs in this files.</p>



<p>You should also be able to see some scheduling information in our tables in our database. Some of the table&#8217;s screenshots are given below.</p>



<figure class="wp-block-image"><img decoding="async" width="1024" height="140" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Quartz-Job-Details-1024x140.png" alt="" class="wp-image-13897" srcset="/wp-content/uploads/2019/08/Quartz-Job-Details-1024x140.png 1024w, /wp-content/uploads/2019/08/Quartz-Job-Details-300x41.png 300w, /wp-content/uploads/2019/08/Quartz-Job-Details-768x105.png 768w, /wp-content/uploads/2019/08/Quartz-Job-Details-425x58.png 425w, /wp-content/uploads/2019/08/Quartz-Job-Details.png 1923w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Quartz Job Details</figcaption></figure>



<figure class="wp-block-image"><img decoding="async" width="652" height="164" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Quartz-Sample-Triggers.png" alt="" class="wp-image-13898" srcset="/wp-content/uploads/2019/08/Quartz-Sample-Triggers.png 652w, /wp-content/uploads/2019/08/Quartz-Sample-Triggers-300x75.png 300w, /wp-content/uploads/2019/08/Quartz-Sample-Triggers-425x107.png 425w" sizes="(max-width: 652px) 100vw, 652px" /><figcaption>Quartz Sample Triggers</figcaption></figure>



<figure class="wp-block-image"><img decoding="async" width="649" height="67" src="https://sibeeshpassion.com/wp-content/uploads/2019/08/Quartz-Triggers.png" alt="" class="wp-image-13899" srcset="/wp-content/uploads/2019/08/Quartz-Triggers.png 649w, /wp-content/uploads/2019/08/Quartz-Triggers-300x31.png 300w, /wp-content/uploads/2019/08/Quartz-Triggers-425x44.png 425w" sizes="(max-width: 649px) 100vw, 649px" /><figcaption>Quartz Triggers</figcaption></figure>



<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 with database</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-quartz-net-with-database/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
