简体   繁体   中英

How to use Lamar 2 with ASP.NET Core 3 preview?

I configured Lamar with ASP.NET Core 3 but I got an error

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Lamar.ServiceRegistry'.'

My config in Program class:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseLamar();
                    webBuilder.UseStartup<Startup>();
                });
    }

and Startup class:

public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        //public void ConfigureServices(IServiceCollection services)
        //{
        //    services.Configure<CookiePolicyOptions>(options =>
        //    {
        //        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        //        options.CheckConsentNeeded = context => true;
        //        options.MinimumSameSitePolicy = SameSiteMode.None;
        //    });
        //    services.AddMvc()
        //        .AddNewtonsoftJson();
        //}

        public void ConfigureContainer(ServiceRegistry services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            // Supports ASP.Net Core DI abstractions
            services.AddMvc().AddNewtonsoftJson();            
            services.AddLogging();

            // Also exposes Lamar specific registrations
            // and functionality
            services.Scan(s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            });
        }

Based on the documentation I replaced ConfigureServices with ConfigureContainer but I got the error that I mentioned above.

在此处输入图片说明

Can anyone help me to use Lamar with ASP.NET Core 3 preview?

UPDATED As #Tom Style wrote, the accepted answer is quite old and depreciated, so he put the new one see his answer.

You can try replacing HostBuilder with the old one like this:

 public static IWebHostBuilder CreateHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseLamar()
            .UseUrls($"http://*:80")
            .UseStartup<Startup>();

IWebHostBuilder will definitely work but is now deprecated.

This code sample shows how to use it with the modern IHostBuilder interface.

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

private static IHostBuilder CreateHostBuilder(string[] args) =>
   Host.CreateDefaultBuilder(args)
      .UseLamar();
      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder.UseStartup<Startup>();
      })

Official docs link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM