如何在 ASP.net Core 2 中使用 Windsor IoC
How to use Windsor IoC in ASP.net Core 2
如何使用 Castle Windsor 作为 IOC 而不是默认的 .net 核心 IOC 容器?
我构建了一个服务解析器,它依赖于 WindsorContainer
来解析服务。
类似于:
public class ServiceResolver
{
private static WindsorContainer container;
public ServiceResolver()
{
container = new WindsorContainer();
// a method to register components in container
RegisterComponents(container);
}
public IList<T> ResolveAll<T>()
{
return container.ResolveAll<T>().ToList();
}
}
无法弄清楚如何让我的 .net core 2 web API 使用此解析器替代 IServiceCollection。
对于以 IServiceProvider
为中心的 .net 核心,您需要创建自己的包装器
参考:Introduction to Dependency Injection in ASP.NET Core: Replacing the default services container
public class ServiceResolver : IServiceProvider {
private static WindsorContainer container;
public ServiceResolver(IServiceCollection services) {
container = new WindsorContainer();
// a method to register components in container
RegisterComponents(container, services);
}
public object GetService(Type serviceType) {
return container.Resolve(serviceType);
}
//...
}
然后在ConfigureServices
和return中配置容器IServiceProvider
:
When using a third-party DI container, you must change ConfigureServices
so that it returns IServiceProvider
instead of void
.
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
// Add other framework services
// Add custom provider
var container = new ServiceResolver(services);
return container;
}
在运行时,您的容器将用于解析类型和注入依赖项。
供其他参考 除了Nkosi提供的解决方案。
有一个名为 Castle.Windsor.MsDependencyInjection 的 nuget 包,它将为您提供以下方法:
WindsorRegistrationHelper.CreateServiceProvider(WindsorContainer,IServiceCollection);
返回的类型是 IServiceProvider
,您不需要创建自己的包装器。
所以解决方案如下:
public class ServiceResolver{
private static WindsorContainer container;
private static IServiceProvider serviceProvider;
public ServiceResolver(IServiceCollection services) {
container = new WindsorContainer();
//Register your components in container
//then
serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(container, services);
}
public IServiceProvider GetServiceProvider() {
return serviceProvider;
}
}
并在启动...
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
// Add other framework services
// Add custom provider
var container = new ServiceResolver(services).GetServiceProvider();
return container;
}
ASP.NET Core 有官方的 Castle Windsor 支持,已作为版本 5 发布(从 nuget Castle.Windsor, Castle.Facilities.AspNetCore). The documentation how to use it is here.
获取)
我想补充 Yahya Hussein 为我提供的答案,但是我必须将我注册的每个依赖项的范围调整为 LifestyleCustome<MsScopedLifestyleManager>()
。
这对某些人来说可能是第二天性,但我花了一分钟时间才弄明白。
如何使用 Castle Windsor 作为 IOC 而不是默认的 .net 核心 IOC 容器?
我构建了一个服务解析器,它依赖于 WindsorContainer
来解析服务。
类似于:
public class ServiceResolver
{
private static WindsorContainer container;
public ServiceResolver()
{
container = new WindsorContainer();
// a method to register components in container
RegisterComponents(container);
}
public IList<T> ResolveAll<T>()
{
return container.ResolveAll<T>().ToList();
}
}
无法弄清楚如何让我的 .net core 2 web API 使用此解析器替代 IServiceCollection。
对于以 IServiceProvider
为中心的 .net 核心,您需要创建自己的包装器
参考:Introduction to Dependency Injection in ASP.NET Core: Replacing the default services container
public class ServiceResolver : IServiceProvider {
private static WindsorContainer container;
public ServiceResolver(IServiceCollection services) {
container = new WindsorContainer();
// a method to register components in container
RegisterComponents(container, services);
}
public object GetService(Type serviceType) {
return container.Resolve(serviceType);
}
//...
}
然后在ConfigureServices
和return中配置容器IServiceProvider
:
When using a third-party DI container, you must change
ConfigureServices
so that it returnsIServiceProvider
instead ofvoid
.
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
// Add other framework services
// Add custom provider
var container = new ServiceResolver(services);
return container;
}
在运行时,您的容器将用于解析类型和注入依赖项。
供其他参考 除了Nkosi提供的解决方案。
有一个名为 Castle.Windsor.MsDependencyInjection 的 nuget 包,它将为您提供以下方法:
WindsorRegistrationHelper.CreateServiceProvider(WindsorContainer,IServiceCollection);
返回的类型是 IServiceProvider
,您不需要创建自己的包装器。
所以解决方案如下:
public class ServiceResolver{
private static WindsorContainer container;
private static IServiceProvider serviceProvider;
public ServiceResolver(IServiceCollection services) {
container = new WindsorContainer();
//Register your components in container
//then
serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(container, services);
}
public IServiceProvider GetServiceProvider() {
return serviceProvider;
}
}
并在启动...
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
// Add other framework services
// Add custom provider
var container = new ServiceResolver(services).GetServiceProvider();
return container;
}
ASP.NET Core 有官方的 Castle Windsor 支持,已作为版本 5 发布(从 nuget Castle.Windsor, Castle.Facilities.AspNetCore). The documentation how to use it is here.
获取)我想补充 Yahya Hussein 为我提供的答案,但是我必须将我注册的每个依赖项的范围调整为 LifestyleCustome<MsScopedLifestyleManager>()
。
这对某些人来说可能是第二天性,但我花了一分钟时间才弄明白。