问题
I'm using Ninject.MVC3 with WebAPI.
Originally, I was using the implementation of NinjectResolver and NinjectScope as outlined here,i.e. using _kernel.BeginBlock()
,
I noticed that BeginBlock() gets invoked on each call to the Controller. On load testing the controller (over several hundred invocations) I noticed that the memory consumption of w3wp increased significantly (upwards of 1.4 gigs on high load) and the GC would never reclaim any memory.
Per this SO post, the kernel should not be disposed and BeginBlock() should not be used. Following which I updated the Resolver and Scope like so:
public class NinjectScope : IDependencyScope
{
protected IResolutionRoot resolutionRoot;
public NinjectScope(IResolutionRoot kernel)
{
resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).SingleOrDefault();
}
public IEnumerable<object> GetServices(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).ToList();
}
public void Dispose()
{
//Don't dispose the kernel
//IDisposable disposable = (IDisposable)resolutionRoot;
//if (disposable != null) disposable.Dispose();
//resolutionRoot = null;
}
}
public class NinjectResolver : NinjectScope, IDependencyResolver
{
private IKernel _kernel;
public NinjectResolver(IKernel kernel): base(kernel)
{
_kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectScope(_kernel);
//what's the difference between using just _kernel vs _kernel.BeginBlock()
//return new NinjectScope(_kernel.BeginBlock());
}
}
The memory consumption lowered significantly following this change(i.e. using the above implementation). I would like to understand why this is. What is it that BeginBlock() really does and when should one use it.
Is the above implementation accurate?
回答1:
Remo Gloor says that the ActivationBlock
concept is broken and that you should not use them. Furthermore, issues regarding ActivationBlock
will most likely not going to be fixed, as the feature is to be removed from future Ninject versions.
The idea of Activation Blocks (current implementation) is:
- Create exactly one instance of every type requested in the
ActivationBlock
- dispose of all instances created by the
ActivationBlock
when the block itself is disposed.
Also see:
- https://github.com/ninject/ninject/issues/106
- Future of Activation Blocks
Furthermore, you've tagged & named your question with "MVC3" but the System.Web.Http.Dependencies.IDependencyResolver interface you are using is related to asp.net-web-api. MVC's System.Web.Mvc.IDependencyResolver is different.
Ninject already features nuget packages which do the integration into asp.net-mvc-3 (4,5,...) and asp.net web api:
- Ninject.MVC3
- WebApi
Now if you really want to implement the dependency resolver and scope yourself we can compare it to ninject's web-api implementation:
- NinjectDependencyResolver.cs
- which inherits from NinjectDependencyScope.cs
The only real difference to your code is that they have one class implementing both, IDependencyResolver
and IDependencyScope
and that they are consistently using the IResolutionRoot
instead of the IKernel
interface.
(and for comparison: MVC3 NinjectDependencyResolver.cs)
来源:https://stackoverflow.com/questions/26708979/when-should-one-use-kernel-beginblock-in-ninject-mvc3