<?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>asp net core code &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/asp-net-core-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Mon, 13 Mar 2023 07:12:25 +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>asp net core code &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Easy Way to Restart Your ASP.NET Core Apps Progamatically</title>
		<link>https://topreviewhostingasp.net/easy-way-to-restart-your-asp-net-core-apps-progamatically/</link>
					<comments>https://topreviewhostingasp.net/easy-way-to-restart-your-asp-net-core-apps-progamatically/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Mon, 13 Mar 2023 07:09:53 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net core code]]></category>
		<category><![CDATA[asp net core programming]]></category>
		<category><![CDATA[asp net core tips]]></category>
		<category><![CDATA[asp net core tutorial]]></category>
		<category><![CDATA[asp net tips]]></category>
		<category><![CDATA[asp net tutorial]]></category>
		<category><![CDATA[shutdown asp net core]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3473</guid>

					<description><![CDATA[We developers won&#8217;t have access to servers all the time, and without management capabilities, it will be difficut to restart a web application. However, in ASP.NET Core, there is a way for us to programmatically restart our application without requring management capabilities from server admins. IApplicationLifetime There is a IApplicationLifetime interface in ASP.NET Core which can handle [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>We developers won&#8217;t have access to servers all the time, and without management capabilities, it will be difficut to restart a web application. However, in ASP.NET Core, there is a way for us to programmatically restart our application without requring management capabilities from server admins.</p>



<h2 class="wp-block-heading">IApplicationLifetime</h2>



<p>There is a <strong>IApplicationLifetime</strong> interface in ASP.NET Core which can handle events like start up and shut down. It provides 3 Cancellation Token, allowing us to use <strong>Action</strong> delegate to handle the ASP.NET Core website&#8217;s start and stop events:</p>



<pre class="wp-block-code"><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
    appLifetime.ApplicationStarted.Register(() =&gt;
    {
        _logger.LogInformation("Moonglade started.");
    });

    appLifetime.ApplicationStopping.Register(() =&gt;
    {
        _logger.LogInformation("Moonglade is stopping...");
    });

    appLifetime.ApplicationStopped.Register(() =&gt;
    {
        _logger.LogInformation("Moonglade stopped.");
    });

    // ... Other code
}</code></pre>



<p>Per Microsoft document:</p>



<figure class="wp-block-table">
<table>
<thead>
<tr>
<th style="text-align: left;">Cancellation Token</th>
<th style="text-align: left;">Triggered when…</th>
</tr>
</thead>
<tbody>
<tr>
<td>ApplicationStarted</td>
<td>The host has fully started.</td>
</tr>
<tr>
<td>ApplicationStopped</td>
<td>The host is completing a graceful shutdown. All requests should be processed. Shutdown blocks until this event completes.</td>
</tr>
<tr>
<td style="text-align: left;">ApplicationStopping</td>
<td style="text-align: left;">The host is performing a graceful shutdown. Requests may still be processing. Shutdown blocks until this event completes.</td>
</tr>
</tbody>
</table>
</figure>



<h2 class="wp-block-heading">Killing the Website</h2>



<p>Besides these 3 events, <strong>IApplicationLifetime</strong> interface got another method named <strong>StopApplication() </strong>which can stop the current ASP.NET Core application. When the website is being stopped, <strong>ApplicationStopping</strong> and <strong>ApplicationStopped</strong> events will be fired in sequence.</p>



<p>We can restart our website based on this method.</p>



<h2 class="wp-block-heading">Implement Restart Functionality</h2>



<p>The most easy way for us to restart a website is to access a certain URL. Take ASP.NET Core MVC application for example:</p>



<p>Inject an <strong>IApplicationLifetime </strong>class into the Controller:</p>



<pre class="wp-block-code"><code>public class AdminController : Controller
{
    IApplicationLifetime applicationLifetime;

    public AdminController(IApplicationLifetime appLifetime)
    {
        applicationLifetime = appLifetime;
    }

    // ...
}</code></pre>



<p>And use an Action to invoke the <strong>StopApplication() </strong>method:</p>



<pre class="wp-block-code"><code>[HttpGet("blow-me-up")]
public IActionResult BlowMeUp()
{
    applicationLifetime.StopApplication();
    return new EmptyResult();
}</code></pre>



<p>Now, when I access the URL &#8220;<strong>blow-me-up</strong>&#8220;, the website will shut down itself:</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="887" height="399" class="wp-image-3474 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2023/03/cmd.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/03/cmd.png 887w, https://topreviewhostingasp.net/wp-content/uploads/2023/03/cmd-300x135.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/03/cmd-768x345.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/03/cmd-50x22.png 50w" sizes="(max-width: 887px) 100vw, 887px" /></figure>



<p>To restart the application is easy. Under IIS, the next request goes to the website will start it up again. Which basically means, reopen the website in the browser again and it will start up.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/easy-way-to-restart-your-asp-net-core-apps-progamatically/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
