aspnet / AspNetCoreModule

ASP.NET Core Module for IIS and IIS Express
Other
115 stars 31 forks source link

ASP.NET Core 2.0 IApplicationLifetime not working on IIS #166

Open Tratcher opened 7 years ago

Tratcher commented 7 years ago

From @thshr on September 22, 2017 15:54

When registering IApplicationLifetime callbacks in ASP.NET Core 2.0 hosted in IIS, they are not triggered i.e. Stopped and Stopping registered delegates are not being fired when the app pool is recycled on IIS.

They are triggered only when ASP.NET Core app is launched as an executable and stopped with CTRL-C

sample app: https://github.com/thshr/AspNetCoreIAppLifetimeIIS output from logger with custom warnings: EXECUTABLE AspNetCoreIAppLifetimeIIS.Controllers.ValuesController:Warning: STARTED! AspNetCoreIAppLifetimeIIS.Controllers.ValuesController:Warning: STOPPING.... AspNetCoreIAppLifetimeIIS.Controllers.ValuesController:Warning: STOPPED!!! IIS AspNetCoreIAppLifetimeIIS.Controllers.ValuesController:Warning: STARTED!

Copied from original issue: aspnet/IISIntegration#421

dvzghr commented 7 years ago

sample app working url: https://github.com/thshr/AspNetCoreIAppLifetimeIIS

shirhatti commented 7 years ago

@pan-wang Can you have a look at this?

pan-wang commented 7 years ago

ANCM never listened on IIS AppPool recycle. It relies on the destruction of configuration object which is triggered during shutdown but is too late for the .Net core App as itself may not have time to do graceful shutdown.

letrungkien211 commented 7 years ago

I am facing the exact same problem. Any workaround for this?

aleha84 commented 6 years ago

is there any progress?

letrungkien211 commented 6 years ago

One vote, please work on this. Since .net core doesn’t support QueueWorkItem... so I need to control OnStopping event to clean up background tasks.

alexsandro-xpt commented 6 years ago

+1

MrAntix commented 6 years ago

I am not seeing this on website stop either. not until I do an iisreset I can created a repo if required, but thought the one above would be enough?

One wrinkle, I am running a websocket server on the site.

I have logging, which shows nothing happening when the site is stopped multiple times, but then when I iisreset, you get all the stop messages in one go

MrAntix commented 6 years ago

I made a repo https://github.com/MrAntix/DotNetCore2.WebAppLIfetime

this is the log, the bits with no timestamp are manual actions by me

2018-02-18T14:20:05.5688793+00:00  [INF] Configure (7be1d423)
2018-02-18T14:20:05.7208929+00:00  [INF] ApplicationStarted (883d59d0)
client connected
2018-02-18T14:20:18.9621727+00:00 0HLBMRAQJ3DNK:00000002 [INF] WebSocket Connected 1 (20f38d18)
WebSite Restarted - no logs
client connected
2018-02-18T14:21:14.0399889+00:00 0HLBMRAQJ3DNL:00000001 [INF] WebSocket Connected 2 (e8409bb3)
WebPool Recycled - no logs
2018-02-18T14:22:13.2438866+00:00  [INF] Configure (7be1d423)
2018-02-18T14:22:13.3973236+00:00  [INF] ApplicationStarted (883d59d0)
two new clients connected - so now 4 clients running all saying connected but the app thinks there is only two because its restarted
2018-02-18T14:22:13.6302114+00:00 0HLBMRC0KPTHM:00000001 [INF] WebSocket Connected 1 (20f38d18)
2018-02-18T14:22:32.4845791+00:00 0HLBMRC0KPTHN:00000001 [INF] WebSocket Connected 2 (e8409bb3)
iisreset
2018-02-18T14:23:59.9744022+00:00 0HLBMRC0KPTHN:00000001 [INF] WebSocket Disconnected 1 (9d7c7fd3)
2018-02-18T14:23:59.9769216+00:00 0HLBMRC0KPTHM:00000001 [INF] WebSocket Disconnected 0 (6b3d9b56)
2018-02-18T14:24:00.9987941+00:00  [INF] ApplicationStopping (dfb8d74c)
2018-02-18T14:24:01.0615675+00:00  [INF] ApplicationStopped (d23c6807)

You can see I loose track of the first 2 clients, which remain connected and I have no lifetime event to deal with this

The apppool recycle happens when I deploy, so I loose track of my clients then

Is there any more you need or can you suggest a work around?

sepehr1014 commented 6 years ago

Any progress on this issue?

alexsandro-xpt commented 6 years ago

@sepehr1014 are you already test it with aspnet core 2.1 preview?

sepehr1014 commented 6 years ago

@alexsandro-xpt I did on Preview2 and it still doesn't fire.

pan-wang commented 6 years ago

The new version aspnet core does listen on IIS global stop event and then graceful shutdown back-end process. I'm not sure preview 2 ship it or not. Windows event log would tell whether graceful shutdown happened or not. Please also check whether you have aspnetcorerh.dll at intersection folder. I will also check with the repro app once in office.

pan-wang commented 6 years ago

I checked with the latest build (you can also build yourself from https://github.com/aspnet/IISIntegration/ ). ANCM does send graceful shutdown message to the backend when recycling IIS worker process. Windows event log does show Event ID 1006 and Event ID 1030. I also see the following staff from Kestrel log Application started. Press Ctrl+C to shut down. info: DotNetCore2.WebAppLIfetime.Startup[0] ApplicationStopping info: DotNetCore2.WebAppLIfetime.Startup[0] ApplicationStopped Application is shutting down... The feature works as expected.

Note, please file issues in IISIntegration instead of AspNetCoreModule as we will not use the later anymore.

n9 commented 6 years ago

@pan-wang IISIntegration is replacement for AspNetCoreModule? Or how these two projects relate? Where can I find more information?

Tratcher commented 6 years ago

The two code bases have been merged in IISIntegration.

sepehr1014 commented 6 years ago

I can confirm this event is not fired on ASP.NET Core 2.1 RC1. We've set IIS to recycle the app pool once a day in an exact time and it does not trigger the event. It's fired when we manually stop the site but not if IIS recycles the app pool ...

sepehr1014 commented 6 years ago

It seems it didn't have a graceful shutdown:

A worker process with process id of '38740' serving application pool 'PasajApp' has requested a recycle because it reached its scheduled recycle time.

A process serving application pool 'PasajApp' exceeded time limits during shut down. The process id was '38740'.

Any idea what might cause this issue and how to debug it? BTW can connected web sockets prevent the app from graceful shutdown?

pan-wang commented 6 years ago

@sepehr1014 This is a known issue in ancm V1 as it relies on IIS module's application shutdown notification. By design (enforced by IIS instead of ANCM), this notification will be hold until all requests were done. this is why you so some websocket request held the shutdown/recycle. This issue has been solved in ANCM v2 which has not shipped yet. V2 code is available at https://github.com/aspnet/IISIntegration/tree/dev/src/AspNetCoreModuleV2 V1 code is available at https://github.com/aspnet/IISIntegration/tree/dev/src/AspNetCoreModuleV1 @n9 ANCM V2 has more lots improvement and new features (the most important one is inprocess support) you can find some discussion at https://github.com/aspnet/AspNetCoreModule/issues/265

sepehr1014 commented 6 years ago

@pan-wang It good to hear that it's a known issue. But I expected ANCM v2 to be released with ASP.NET Core 2.1 RC1 ... Will it be released alongside 2.1 RTM? Any planned date?

shirhatti commented 6 years ago

@sepehr1014 ANCMv2 won't be released alongside 2.1 RTM. We don't have a committed ship date yet. Watch for an announcement on the aspnet/announcements repo as we finalize plans.

TermanEmil commented 6 years ago

it still doesn't work for me

yidane commented 6 years ago

me too

sepehr1014 commented 6 years ago

@TermanEmil @yidane I think it won't work until ANCMv2 is released ...

yidane commented 6 years ago

@sepehr1014 I will confirm that. I have changed my project to windows service of netcore,so that I can manage all exit events.

coleman-c commented 5 years ago

I'm using net core 2.2 & ANCMv2 hosting InProcess.

This appears to mostly work in IIS, however I don't receive the Stopping & Stopped events unless a request has arrived via http(s). This is an issues becuase, with the exception of the health checks there are no http endpoints and IIS is simply the process host for a worker style service.

I'm using the Application Initialization module with AlwayRunning & preloadEnabled="true" set. If I start the application then IApplicationLifetime.ApplicationStarted fires. If I then stop the application The Stopping and Stopped events fail to fire.

If however I make a single request to the API then stop the application pool the shutdown process follows the expected path.

Could it be related to this issue? https://github.com/aspnet/AspNetCore/issues/6075 since the request which is fired by the WarmUp module doesn't complete successfully as described in the linked item and maybe there is some initialization that is not complete unless a full request has been through the pipeline?