Running ASP.Net Core Background Tasks after AppPool Recycling - asp.net-core

I have an asp.net core application running on windows server. This is using some background tasks for using external service to get the data. We are facing an issue that after our app pool recycled/restarted, the background task is not running. This is running only after we are accessing the application. We found an article regarding this.
https://weblog.west-wind.com/posts/2013/Oct/02/Use-IIS-Application-Initialization-for-keeping-ASPNET-Apps-alive
Since this article describe asp.net application, below code will substitute the above solution in .net core application
public class Startup
{
public void Configure(IApplicationBuilder app)
{
var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
Is there any other work around to solve this issue?

Related

Communicate with SignalR server application from another Windows process

I have followed the guide from Microsoft for getting started with SignalR. This worked perfectly, and I was able to publish and deploy the application to IIS.
Now I need to communicate with the .NET application from another Windows process (specifically a Delphi program). What I want to do is to tell the .NET application to send SignalR message (i.e. invoke a method on all connected clients).
How can I accomplish this?
I'm not sure how the .NET application is being executed - does it have its own Windows process that I could send Windows messages to? Or would it be easier to send a local HTTP GET/POST request from the Delphi program to localhost? If so, how can I make the SignalR application handle it?
You can create a controller and inject the IHubContext<ChatHub>. Use the hub context to send message to clients.
public class MessageController : Controller
{
private readonly IHubContext<ChatHub> _hubContext;
public MessageController(IHubContext<MessageHub> hubContext)
{
_hubContext = hubContext;
}
[HttpPost]
public async Task<IActionResult> SendMessage([FromForm] string message)
{
await _hubContext.Clients.All.SendAsync("ReceiveMessage", message);
return Ok();
}
}
Then call this endpoint from your delphi app.

Can we Host ASP.NET SignalR v2.4.1 in an ASP.NETCORE App?

I have a situation where my codebase is stuck in .Net 4.7.2 for now but I need to push some notifications on a Website which is built on Asp.Core 2.2.
Across the system we use SignalR 2.4.1 but it is completely re-written in .Net Core.
I tried hosting it in the same app without success. Owin does not seem to be happy.
Has anyone had any success with it or has any suggestion?
There has to be a way for projects migrating from .Net to Core.
Thanks
Ok so after along night I got a solution to this issue.
First just to make my setup clear.
There is an API project targetting .Net 4.7.2 which is broadcasting some messages via a SignalR 2.4.1 Hub.
There are some other Asp.Net 4.7.2 Projects consuming those Hubs which are working fine.
And also there is a new website build in .Net Core but targetting 4.7.2 framework.
The solution I ended up is essentially hosting an OWIN pipeline within the AspCore Pipeline.
First I needed to install the following packages:
Microsoft.Owin
Microsoft.AspNetCore.Owin
I also added a new extension method for the Core IApplicationBuilder interface that sets up OWIN on the same pipeline:
public static class OwinExtensions
{
public static IApplicationBuilder UseOwinApp(this IApplicationBuilder app, Action<IAppBuilder> configuration)
{
return app.UseOwin(setup => setup(next =>
{
IAppBuilder owinApp = new AppBuilder();
var aspNetCoreLifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));
var owinAppProperties = new AppProperties(owinApp.Properties)
{
OnAppDisposing = aspNetCoreLifetime?.ApplicationStopping ?? CancellationToken.None,
DefaultApp = next
};
configuration(owinApp);
return owinApp.Build<Func<IDictionary<string, object>, Task>>();
}));
}
}
Then in the Startup class of the Core project, in the Configure method I was able to use my extension and register SignalR hubs to it like this:
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseOwinApp(owinApp =>
{
owinApp.MapSignalR();
});
...
}
This way we can add more middlewares to the OWIN pipeline if we need to for whatever reasons.
I hope this helps.

How to add global metadata to ASP.NET Core logging?

I'd like to add my app's build number to all logs in an ASP.NET Core 3.1 app that is using Application Insights for log storage. Is this possible without having to use BeginScope and EndScope everywhere? I assumed it would be part of the ConfigureLogging startup hook, but didn't see anything. I've done this in the past with Serilog's enrichers, but am not using that library currently.
You can achieve that with TelemetryInitializer. (https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer)
public class BuildNumberTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
(telemetry as ISupportProperties).Properties.Add("BuildNumber", "ValueForBuildNumber");
}
You need to add this initializer to the config, which is done like below if you are on Asp.Net Core applications.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITelemetryInitializer, BuildNumberTelemetryInitializer >();
}

Service not calling OnShutdown() when windows shuts down

I have .net core console application, which is hosted as windows service.
I want to catch an event if the user logs off/shutdown the computer.
I have found ways to catch this event in .net framework (here & here).
But I cant figure out how to achieve this in .net core.
To create service I am using "ServiceBase" class. Sample code is as given below:
public class MyService : ServiceBase
{
readonly string LogPath = "D:\\TestAppService.txt";
#region Constructors
public MyService()
{
this.CanShutdown = true;
}
#endregion
#region Protected Functions
protected override void OnStart(string[] args)
{
//your code here
// call the base class so it has a chance
// to perform any work it needs to
base.OnStart(args);
}
protected override void OnStop()
{
//your code here
// Call the base class
base.OnStop();
}
protected override void OnShutdown()
{
using (StreamWriter sw = File.AppendText(LogPath))
{
sw.WriteLine("shutdown == true");
}
//your code here
base.OnShutdown();
}
#endregion
}
The OnStop and OnStart methods are being called.
but when I shutdown the computer my OnShutdown method is not called.
According to aspisof.net, you should be able to use the SessionEnding API. This is because it is listed as being exposed in the windows Compatibility Pack - available on NuGet here.
This article on learn.microsoft.com shows how you can include it in a .NET Core application.
tl;dr
Add the NuGet package
Target Windows only
One thing to note: this was originally designed to be a temporary fix for porting Windows specific .NET code over to .NET Core.
The more accepted way to implement Windows only features is to move as much code to .NET Standard libraries as possible, and to use conditional compilation directives to include platform specific code when building for that platform.
By design dotnet core is not "friendly" with platform specific stuff
(like listening to log off event seems to me).
The solution I use in one of Windows-hosted services is described here.
When application domain is forced to close by operating system on shutdown - there is a room for using AppDomain event handlers.

MVC 6 install as a Windows Service (ASP.NET Core 1.0.0)

UPDATE - 26th July 2016
I have added the solution to this in ASP.NET Core 1.0.0 in the answers below.
I have created a simple MVC 6 app and have included the Microsoft.AspNet.WebListener library so I can host outside of IIS.
From project.json:
"dependencies": {
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Microsoft.AspNet.Mvc": "6.0.0-beta4"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}
When I publish this I can run the web.cmd file and get the site running in a console window. Great!
But in OWIN you can use TopShelf to launch your web app from a Console Application. This can then be built as an executable and installed as a Windows Service.
Is there a way to do this with an ASP.NET 5 MVC 6 web app?
You can run a DNX app as a Windows service, however, you can't run the CMD file directly. You will get an error saying the following: 'The service did not respond to the start or control request in a timely fashion.' You can point directly to dnx.exe and pass the project folder and command as arguments.
Read this post for a lot more detail: http://taskmatics.com/blog/run-dnx-applications-windows-service/
Once you have your app set up. You can bootstrap ASP.NET from the OnStart method of the service. To do this you can use WebHostBuilder from Microsoft.AspNet.Hosting.
Lastly, you can ensure the app is still runnable in VS by passing an argument (such as 'non-service') to the Main method and check that before calling ServiceBase.Run, and if present, you can call OnStart directly instead. The project's properties gives you the option to pass arguments when running in VS.
UPDATE:
There is a follow up post which builds upon the one above. It shows how to run ASP.NET 5 with static files and MVC 6 in a Windows service. The link is here: http://taskmatics.com/blog/host-asp-net-in-a-windows-service/
As of the latest ASP.NET Core Version 1.0.0 libraries this is now somewhat simplified.
There is an open discussion on this topic on the ASP.NET GitHub page.
All ASP.NET Core applications are now Console Applications and there is a new library to host as a Windows Service that runs on the full .NET framework (which makes sense as this whole problem assumes a Windows web server).
We need to create a new ASP.NET Core Web Application (.NET Framework)
Check the project.json file to ensure that the "frameworks" section is as below:
"frameworks": {
"net461": {}
},
We need to then add the service hosting library Microsoft.AspNetCore.Hosting.WindowsServices and save the project.json to restore the package.
We then need to edit the program.cs file and add paths for running in debug and running as a service, the code for this is as follows:
public static void Main(string[] args)
{
var isDebug = Debugger.IsAttached || ((IList)args).Contains("--debug");
string runPath;
if (isDebug)
runPath = Directory.GetCurrentDirectory();
else
{
var exePath = Process.GetCurrentProcess().MainModule.FileName;
runPath = Path.GetDirectoryName(exePath);
}
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(runPath)
.UseStartup<Startup>()
.Build();
if (isDebug)
host.Run();
else
host.RunAsService();
}
The .RunAsService() method is an extension method provided by the Microsoft.AspNetCore.Hosting.WindowsServices lib.
To install as a service you just need to run the following command from an Administrator command prompt:
SC Create <service-name> binPath= "[PublishOutputPath]\mvc6-example.exe"
Please clone and view the working version on my GitHub repository.
I hope this helps :)
UPDATE: It seems like there is going to be a Windows Service hosting option coming in with RC2. See this GitHub comment for more info and this answer.
I am afraid the answer is no for this. I have been looking into this as well and the best way to do this is to deploy your project into a known location on disk and have a Windows Service to spin up the process which calls the cmd file. This way, the Windows Service will only act as a watchdog.
I am hoping to get some blog posts and samples on this as I have been looking into this in terms of deployment. There is also an open discussion here: https://github.com/aspnet/Home/issues/465
It is worth looking at https://github.com/aspnet/Hosting/tree/dev/src/Microsoft.AspNet.Hosting.WindowsServices
It seems that ASP.NET team is working on native support for hosting ASP.NET MVC 6 applications within Windows Services.
Here is a simple ServiceBase hosting an ASP.NET MVC 6 app:
/// <summary>
/// Provides an implementation of a Windows service that hosts ASP.NET.
/// </summary>
public class WebApplicationService : ServiceBase
{
private IWebApplication _application;
private IDisposable _applicationShutdown;
private bool _stopRequestedByWindows;
/// <summary>
/// Creates an instance of <c>WebApplicationService</c> which hosts the specified web application.
/// </summary>
/// <param name="application">The web application to host in the Windows service.</param>
public WebApplicationService(IWebApplication application)
{
_application = application;
}
protected sealed override void OnStart(string[] args)
{
OnStarting(args);
_application
.Services
.GetRequiredService<IApplicationLifetime>()
.ApplicationStopped
.Register(() =>
{
if (!_stopRequestedByWindows)
{
Stop();
}
});
_applicationShutdown = _application.Start();
OnStarted();
}
protected sealed override void OnStop()
{
_stopRequestedByWindows = true;
OnStopping();
_applicationShutdown?.Dispose();
OnStopped();
}
}