简答:
检查this project。但是,它依赖于仍处于测试版的 Ninject 4.0.0,它似乎离最终版本还很远(source)。对于 Ninject 3.3.x 如下所示。
长答案:
感谢@Steven,我设法创建了 ASP.NET Core 2.0 和 Ninject(3.3.x 和 4.0)的工作解决方案。代码主要来自Missing-Core-DI-ExtensionsGit repo,非常感谢dotnetjunkie。
无论引用的 Ninject 版本如何,都必须执行以下操作:
1) 在您的项目中包含AspNetCoreExtensions.cs 和AspNetCoreMvcExtensions.cs。
2) 创建一个非常简单的服务来测试 DI:TestService,它实现了ITestService:
public class TestService : ITestService
{
public int Data { get; private set; }
public TestService()
{
Data = 42;
}
}
public interface ITestService
{
int Data { get; }
}
如下所示更改Startup.cs:
1) 添加这些成员
private readonly AsyncLocal<Scope> scopeProvider = new AsyncLocal<Scope>();
private IKernel Kernel { get; set; }
private object Resolve(Type type) => Kernel.Get(type);
private object RequestScope(IContext context) => scopeProvider.Value;
2) 添加到ConfigureServices(IServiceCollection services)(最后)
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRequestScopingMiddleware(() => scopeProvider.Value = new Scope());
services.AddCustomControllerActivation(Resolve);
services.AddCustomViewComponentActivation(Resolve);
3) 添加到Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)(开头)
Kernel = RegisterApplicationComponents(app, loggerFactory);
4) 添加如下方法和内部类:
private IKernel RegisterApplicationComponents(
IApplicationBuilder app, ILoggerFactory loggerFactory)
{
// IKernelConfiguration config = new KernelConfiguration();
Kernel = new StandardKernel();
// Register application services
foreach (var ctrlType in app.GetControllerTypes())
{
Kernel.Bind(ctrlType).ToSelf().InScope(RequestScope);
}
Kernel.Bind<ITestService>().To<TestService>().InScope(RequestScope);
// Cross-wire required framework services
Kernel.BindToMethod(app.GetRequestService<IViewBufferScope>);
Kernel.Bind<ILoggerFactory>().ToConstant(loggerFactory);
return Kernel;
}
private sealed class Scope : DisposableObject { }
5) 创建BindToMethod扩展方法
public static class BindingHelpers
{
public static void BindToMethod<T>(this IKernel config, Func<T> method) =>
config.Bind<T>().ToMethod(c => method());
}
6) 通过将自定义服务注入控制器、在测试服务构造函数中设置断点并检查调用堆栈来测试 DI。除了提供一个实例外,调用堆栈应该命中自定义代码以集成 Ninject(例如ConfigureRequestScoping 方法)
IKernel 在版本 4 中已被弃用,因此应该使用 IReadOnlyKernel 和 IKernelConfiguration 类(尽管上面的代码应该可以工作)。
1) 使用新类 (IReadOnlyKernel)
private readonly AsyncLocal<Scope> scopeProvider = new AsyncLocal<Scope>();
private IReadOnlyKernel Kernel { get; set; }
private object Resolve(Type type) => Kernel.Get(type);
private object RequestScope(IContext context) => scopeProvider.Value;
2) 3) 是一样的
4) 方法略有不同:
private IReadOnlyKernel RegisterApplicationComponents(
IApplicationBuilder app, ILoggerFactory loggerFactory)
{
IKernelConfiguration config = new KernelConfiguration();
// Register application services
foreach (var ctrlType in app.GetControllerTypes())
{
config.Bind(ctrlType).ToSelf().InScope(RequestScope);
}
config.Bind<ITestService>().To<TestService>().InScope(RequestScope);
// Cross-wire required framework services
config.BindToMethod(app.GetRequestService<IViewBufferScope>);
config.Bind<ILoggerFactory>().ToConstant(loggerFactory);
return config.BuildReadonlyKernel();
}
5) 扩展必须使用IKernelConfiguration
public static class BindingHelpers
{
public static void BindToMethod<T>(
this IKernelConfiguration config, Func<T> method) =>
config.Bind<T>().ToMethod(c => method());
}