microsoft-dependency-injection
microsoft-dependency-injection copied to clipboard
UseUnityServiceProvider does not work on WebApplicationBuilder - .NET 6.0 (Asp.Net Core)
I've migrated from .NET Core 3.1 to .NET6.0 WebAPI using the guidelines provided by Microsoft. I'm using UnityContainer as DependencyInjection framework. I observe that the Unity dependencies are loaded in the componentManager.SharedUnityContainer but are not passed on the WebApplication builder API's.
Below is the migrated code snippet.
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
var config = new ConfigurationBuilder()
.SetBasePath(pathToContentRoot)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var path = config["AppFolder:DeviceLogs"];
StartupLog.Intialise(path);
StartupLog.Log(string.Format("Base Path {0}", pathToContentRoot));
string basicConfigFile = Path.Combine(pathToContentRoot, "Configuration", "unity.basic.config");
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
IConfiguration configuration = configurationBuilder.Build();
componentManager.Configure(basicConfigFile, null);
componentManager.RegisterType<IInfrastructureServices, InfrastructureServices>(ComponentLifetime.Singleton);
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
builder.WebHost.UseUnityServiceProvider(componentManager.SharedUnityContainer);
builder.WebHost.UseKestrel();
builder.Services.AddConfiguration(configuration);
builder.Services.AddMvc();
builder.Services.AddControllers();
builder.Configuration.AddConfiguration(configuration);
var app = builder.Build();
var infraServices = componentManager.Resolve<IInfrastructureServices>();
infraServices.Initialize(componentManager);
infraServices.UpdateDefaultValues(componentManager.Resolve<IDataManagerFactory>());
_ = new CodeSignatureVerification(infraServices.Logger);
app.UseHsts();
app.UseRouting();
app.UseHttpsRedirection();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();