seesharper/LightInject

Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'LightInject.IServiceContainer

sgf opened this issue · 2 comments

sgf commented

this is my code in asp.net core 3.0 Web-Api project ,by https://www.lightinject.net/microsoft.aspnetcore.hosting/

public class Program {
        public static void Main(string[] args) {

            config = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json", optional: true)
                   .Build();
            CreateHostBuilder(args).Build().Run();
        }
        private static IConfigurationRoot config;
        public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)
             .ConfigureWebHostDefaults(webBuilder => {
                 //Ioc use LightInject
                 webBuilder.UseLightInject();
                 var isProduction = false;
                 webBuilder.ConfigureKestrel((context, options) => {
                     options.Limits.MaxRequestBodySize = Setting.FileUploadLimit;
                     isProduction = context.HostingEnvironment.IsProduction();
                 });
                 webBuilder.UseStartup<Startup>();
                 if (isProduction) {
                     var url = config.GetSection("Url").Value;
                     webBuilder.UseUrls(url);
                 }

             });
    }
// added in Startup
     public void ConfigureContainer(IServiceContainer container) {
            //TODO:container.RegisterFrom<CompositionRoot>();

        }

Hi, I've had similar issue with .NET Core 3.1

You need to add

.UseServiceProviderFactory(new LightInjectServiceProviderFactory())

to your HostBuilder chain, before .ConfigureWebHostDefaults(..)
LightInjectServiceProviderFactory is from LightInject.Microsoft.DependencyInjection nuget package.

sgf commented

Hi, I've had similar issue with .NET Core 3.1

You need to add

.UseServiceProviderFactory(new LightInjectServiceProviderFactory())

to your HostBuilder chain, before .ConfigureWebHostDefaults(..)
LightInjectServiceProviderFactory is from LightInject.Microsoft.DependencyInjection nuget package.

thanks