Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Created July 23, 2022 04:28
Show Gist options
  • Save mirmostafa/d4e8fc1f99664c445e5b9c133a64d3d2 to your computer and use it in GitHub Desktop.
Save mirmostafa/d4e8fc1f99664c445e5b9c133a64d3d2 to your computer and use it in GitHub Desktop.
Use Autofac in Blazor server-side
using Autofac;
using Autofac.Extensions.DependencyInjection;
using HanyCo.Infra;
using Library.Cqrs;
using Library.Mapping;
namespace BlazorApp;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var autofacServiceProviderFactory = new AutofacServiceProviderFactory();
_ = builder.Host.UseServiceProviderFactory(autofacServiceProviderFactory);
_ = builder.Services.AddRazorPages();
_ = builder.Services.AddServerSideBlazor();
_ = builder.Services.AddSingleton<IMapper, Mapper>()
.AddMesInfraServices<Program>("connection string", Library.Logging.ILogger.Empty);
_ = builder.Host.ConfigureContainer<ContainerBuilder>(builder => builder.AddCqrs(typeof(Program).Assembly));
var app = builder.Build();
//! Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
_ = app.UseExceptionHandler("/Error");
}
_ = app.UseStaticFiles();
_ = app.UseRouting();
_ = app.MapBlazorHub();
_ = app.MapFallbackToPage("/_Host");
app.Run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment