5

I have a legacy application where HostingEnivronment.RegisterObject is used.

I have been tasked with converting this to asp.net core 2.0. however I am unable to find a way to either implement this or find an alternative to this in asp.net core 2.0.

the namespace Microsoft.AspNetCore.Hosting.Internal does not contain the registerobject method nor does it have the IRegisteredObject interface. I am at a loss on how to get this implemented.

Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47

1 Answers1

3

The way to achieve similar goal in asp.net core is to use IApplicationLifetime interface. It has two properties two CancellationTokens,

ApplicationStopping:

The host is performing a graceful shutdown. Requests may still be processing. Shutdown blocks until this event completes.

And ApplicationStopped:

The host is completing a graceful shutdown. All requests should be completely processed. Shutdown blocks until this event completes.

This interface is by default registered in container, so you can just inject it wherever you need. Where you previously called RegisterObject, you instead call

// or ApplicationStopped
var token = lifeTime.ApplicationStopping.Register(OnApplicationStopping);

private void OnApplicationStopping() {
     // will be executed on host shutting down
}

And your OnApplicationStopping callback will be invoked by runtime on host shutdown. Where you previously would call UnregisterObject, you just dispose token returned from CancellationToken.Register:

token.Dispose();

You can also pass these cancellation tokens to operations that expect cancellation tokens and which should not be accidentally interrupted by shutdown.

Evk
  • 98,527
  • 8
  • 141
  • 191
  • 1
    This answer is not clear, could you please give more details, what namespace we need to using (or nuget install)? – Bashar Abu Shamaa Sep 23 '19 at 16:21
  • https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iapplicationlifetime?view=aspnetcore-6.0 This was the OLDER method (now deprecated). To use the older method, add a using statement for: Microsoft.AspNetCore.Hosting. The NEW method is: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationlifetime?view=dotnet-plat-ext-6.0 Add a using statement for this namespace: Microsoft.Extensions.Hosting You might need to install Nuget packages for these if VisualStudio, Mono, etc can't find the namespace. – Richard Duerr Jun 27 '22 at 20:48
  • Could you please update with clear code examples? Where does `lifeTime` object comes from? – Hary Jan 31 '23 at 15:08
  • 1
    @Hary it's been 5 years so things has changed a bit. Here is fresh link: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationlifetime?view=dotnet-plat-ext-7.0. As mentioned - it's registered by default in asp.net DI so you can inject it wherever you need to use it. For example here they inject to hosted service: https://stackoverflow.com/q/59650230/5311735. – Evk Jan 31 '23 at 16:19