1

I'm working on a new MVC application using the new MS frameworks and am having trouble trying to convert an IServiceDescriptor to a LightInject IServiceContainer...

The reason why I'm going about this is because in the new MS MVC framework we have:

public IServiceProvider ConfigureServices(IServiceCollection services)

In Startup. I want to be able to iterate through that and build a new IServiceProvider from which would contain the related service manager I want the application to use.

I took a look at sources here: https://github.com/aspnet/DependencyInjection/tree/dev/src

But I can't seem to figure it out!

This is what I have so far:

    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);
    }

So far I suspect that I might be doing something wrong when I try to register the ImplementationFactory when it is available...

Anderson Matos
  • 3,132
  • 1
  • 23
  • 33
Casey
  • 11
  • 3

0 Answers0