I am implementing context in my service as per code below. I need to setup IDisposable
correctly as it is not being called.
public class MyWidgetService : WidgetService, IDisposable
{
private bool _disposed;
private readonly DBContext _context;
public MyWidgetService()
{
_context = new DBContext();
}
public List GetWidgets()
{
return _context.stuff().ToList();
}
// however dipose is never being called
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_context.Dispose();
}
}
_disposed = true;
}
}
I am then using Ninject to serve instances of my widget service
public SomeClass(IWidgetService widgetService)
{
_widgetService = widgetService;
}
I register the widget service with ninject here
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Bind<IWidgtetService>().To<WidgetService>();
kernel.Bind<IWidgetService>().To<WidgetService>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}