简体   繁体   English

如何在 ASP.NET Core 3 预览版中使用 Lamar 2?

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

I configured Lamar with ASP.NET Core 3 but I got an error我使用 ASP.NET Core 3 配置了 Lamar,但出现错误

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

My config in Program class:我在Program类中的配置:

    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:Startup类:

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.基于文档我换成ConfigureServicesConfigureContainer但我得到了我上面提到的错误。

在此处输入图片说明

Can anyone help me to use Lamar with ASP.NET Core 3 preview?谁能帮我将 Lamar 与 ASP.NET Core 3 预览版一起使用?

UPDATED As #Tom Style wrote, the accepted answer is quite old and depreciated, so he put the new one see his answer.更新正如#Tom Style 所写,已接受的答案已经很旧且已贬值,因此他将新答案放在了他的答案中。

You can try replacing HostBuilder with the old one like this:您可以尝试用旧的 HostBuilder 替换 HostBuilder,如下所示:

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

IWebHostBuilder will definitely work but is now deprecated. IWebHostBuilder 肯定会工作,但现在已弃用。

This code sample shows how to use it with the modern IHostBuilder interface.此代码示例展示了如何将其与现代 IHostBuilder 接口一起使用。

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 官方文档链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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