Skip to content

UseUnityServiceProvider does not work on WebApplicationBuilder - .NET 6.0 (Asp.Net Core) #94

@VHeddoori

Description

@VHeddoori

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions