I'm writing a Windows service that will self-host an OWIN WebApi web service. To start the web service, the location is pretty obvious; in the OnStart
method of the ServiceBase
-extending class:
private IDisposable _webApiDataConnectionHost;
protected override void OnStart(string[] args) {
_webApiDataConnectionHost = WebApp.Start<OwinWebStartup>("...");
}
However, I'm not sure where to dispose of the web app. In this example project, they dispose of it in the OnStop
method:
protected override void OnStop()
{
if(_server != null)
{
_server.Dispose();
}
base.OnStop();
}
However given that this is an IDisposable
, wouldn't it be correct to dispose of it in the service's override of the Dispose
method? Something like the following:
protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) { components.Dispose(); }
// Dispose of our web app if it exists...
if (_webApiDataConnectionHost != null) {
_webApiDataConnectionHost.Dispose();
}
}
base.Dispose(disposing);
}
Which is the proper place to dispose of the web app?