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.