Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

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.

like image 832
HamedFathi Avatar asked Mar 05 '23 08:03

HamedFathi


1 Answers

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>();
like image 67
Luka Avatar answered Mar 15 '23 07:03

Luka