我使用 ASP.NET Core 3 配置了 Lamar,但出现错误
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Lamar.ServiceRegistry'.'
我在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>();
});
}
和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();
});
}
根据我替换的文档ConfigureServices,ConfigureContainer但我得到了上面提到的错误。
谁能帮我在 ASP.NET Core 3 预览版中使用 Lamar?
已更新正如#Tom Style 所写,接受的答案已经过时且已贬值,因此他将新答案放在了他的答案中。
