I have recently moved a .Net Framework 4.7.2 MVC project from Unity to Microsoft.Extensions.DependencyInjection due to Unity being deprecated. The switch-over seemed simple enough with the main change being the need to create a custom DependencyResolver, as this was previously handled by Unity.
Now these changes are on Production and I am starting to notice some severe memory issues. Getting a dump of the memory usage shows that largest item in memory is the ServiceProvider from Microsoft.Extensions.DependencyInjection which contains thousands of Controllers that have not been disposed of.
The DependencyResolver looks like:
public class MicrosoftDefaultDependencyResolver
: System.Web.Mvc.IDependencyResolver
, System.Web.Http.Dependencies.IDependencyResolver
{
protected IServiceProvider serviceProvider;
public MicrosoftDefaultDependencyResolver(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public IDependencyScope BeginScope()
{
return new MicrosoftDefaultDependencyResolver(
this.serviceProvider.CreateScope().ServiceProvider);
}
public void Dispose()
{
}
public object GetService(Type serviceType)
{
return this.serviceProvider.GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.serviceProvider.GetServices(serviceType);
}
}
I have implemented this based off of a stackoverflow article that I read: How do I inject dependency in webapi in .net framework using Microsoft.Extensions.DependencyInjection?
The Startup class looks like:
public void Configuration(IAppBuilder app)
{
// Set MVC Resolver
MicrosoftDefaultDependencyResolver resolver = GetDependencyResolver();
DependencyResolver.SetResolver(resolver);
// Any connection or hub wire up and configuration should go here
app.MapAzureSignalR(GetType().FullName);
// Turn tracing on programmatically
GlobalHost.TraceManager.Switch.Level = SourceLevels.Error;
}
As we are still using .Net framework, it seemed like the Controllers were not automatically being registered, so I instead had to register them explicitly as Transient.
My question is, am I completely missing something? My hope for moving towards the Microsoft DI package was that it would function in the same way that it does in the newer version of .Net, but I am at the point where it feels easier to move to a completely different IoC framework, such as Autofaq, to resolve these memory issues.