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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With