I am working on an existing C# web app using ASP.NET MVC, targeting .NET 4.5. It uses Unity for IoC, with a custom lifetime manager that appears to be equivalent to per request (I believe it was set up before PerRequestLifetimeManager was added to Unity). The project also uses Entity Framework to talk to a SQL Server database, and a WCF client to talk to a webservice.
The types that are registered are: several custom service interfaces mapped to their classes (none of these are currently implementing IDisposable), an interface mapped to a derived class of the WCF client, and a derived class of the DBContext.
I'm fairly new to IoC; I've done some reading in preparation for adding a logging task and I think there are existing problems in the code base. I'll post a follow-up question about my planned changes later, but I want to make sure the existing code is correct first.
The service classes get their WCF- and DBContext-derived classes injected as properties:
[Dependency]
public IMyWCFClient MyWCFClient { get; set; }
[Dependency]
public MyDBContextDerivedClass MyDBContext { get; set; }
I believe this is okay for the DBContext property since it doesn't actually need to be Dispose
'd, but for the WCF client I think it will never get properly cleaned up - Close()
is never called manually, Dispose()
is never called manually, and the service doesn't implement IDisposable
. Further, it seems from my reading that implementing IDisposable
on the service and having it call Dispose()
on its WCF client property would be wrong, and instead we need to call Close()
, check many exceptions, and call Abort()
if necessary.
Am I correct in thinking the current implementation is a problem?
If so, I've found the ChannelAdam WCF library (https://www.nuget.org/packages/ChannelAdam.Wcf) and it looks like a promising and hopefully-not-too-painful way of fixing this issue - is this a reasonable approach?
edit: Also, am I right that I don't need to worry about cleaning up the DBContext?