gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:28:43 33 4
gpt4 key购买 nike

我使用 ASP.NET Core 3 配置了 Lamar,但出现错误

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

我在 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>();
});
}

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

根据我替换的文档 ConfigureServicesConfigureContainer但我得到了我上面提到的错误。

enter image description here

谁能帮我将 Lamar 与 ASP.NET Core 3 预览版一起使用?

更新
正如#Tom Style 所写,已接受的答案已经过时且已贬值,因此他将新答案放在了他的答案中。

最佳答案

您可以尝试用旧的 HostBuilder 替换 HostBuilder,如下所示:

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

关于c# - 如何在 ASP.NET Core 3 预览版中使用 Lamar 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188121/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com