我正在使用新的MS框架处理新的MVC应用程序,并且在尝试将IServiceDescriptor转换为LightInject IServiceContainer时遇到了麻烦...
我之所以这样做是因为在新的MS MVC框架中我们有:
public IServiceProvider ConfigureServices(IServiceCollection services)
在初创公司。我希望能够遍历它并构建一个新的IServiceProvider,其中包含我希望应用程序使用的相关服务管理器。
我看了一下这里的消息来源: https://github.com/aspnet/DependencyInjection/tree/dev/src
但我似乎无法弄明白!
这是我到目前为止所做的:
public void RegisterMvcDescriptorInstance(IServiceDescriptor service)
{
var name = service.ServiceType.Name;
if (name.Contains("Hosting"))
{
}
ILifetime lifetime;
switch (service.Lifecycle)
{
case LifecycleKind.Singleton:
lifetime = new PerContainerLifetime();
break;
case LifecycleKind.Scoped:
lifetime = new PerScopeLifetime();
break;
case LifecycleKind.Transient:
lifetime = null;
break;
default:
lifetime = null;
break;
}
if (service.ImplementationType != null)
{
if (lifetime == null)
{
MsMvcServiceContainer.Register(service.ServiceType,
service.ImplementationType,
MsMvcServiceName);
return;
}
MsMvcServiceContainer.Register(service.ServiceType,
service.ImplementationType,
MsMvcServiceName,
lifetime);
return;
}
if (service.ImplementationFactory != null)
{
if (lifetime == null)
{
MsMvcServiceContainer.Register(factory =>
service.ImplementationFactory,
MsMvcServiceName);
return;
}
MsMvcServiceContainer.Register(factory =>
service.ImplementationFactory,
MsMvcServiceName,
lifetime);
return;
}
MsMvcServiceContainer.RegisterInstance(service.ServiceType,
service.ImplementationInstance,
MsMvcServiceName);
}
到目前为止,我怀疑当我尝试注册ImplementationFactory时,我可能做错了...