=========================== ASP.NET Service Integration =========================== Integration with ASP.NET Core allows you to use IoC.Configuration to manage the lifecycle and resolution of your web services, including API controllers. This is achieved via the ``IoC.Configuration.AspNet`` Nuget library (source code available at `IoC.Configuration.AspNet `_). Implementation Details ---------------------- To integrate with ASP.NET Core, you use the ``IoC.Configuration.AspNet`` Nuget library (source code available at `IoC.Configuration.AspNet `_). This allows the ``IHostBuilder`` (or ``WebApplicationBuilder``) to delegate service resolution to the underlying container configured in your IoC.Configuration XML file (e.g., Autofac). Key steps include: - Creating a ``WebApplicationHostBuilder`` wrapper for your ``IHostBuilder`` (provided in the `IoC.Configuration.AspNet `_ package). - Calling ``WithHostBuilder`` during the IoC.Configuration startup sequence. Example ------- For a complete implementation, refer to the example project ``WebApiDemo.csproj`` and related projects in `ApplicationIntegrationDemos/ASP.NET.WebApi `_: In this project, you can see how the ``Program.cs`` or ``Startup.cs`` is modified to initialize ``DiContainerBuilder`` and connect it to the web host. The code snippet below shows how the methods ``WithHostBuilder`` and ``RegisterServiceProviderAndBuildApp`` are used in `WebApiDemo.Program `_ to register the DI container configured in IoC.Configuration file `IoCConfiguration.xml `_ with the ASP.NET dependency injection setup. .. note:: The helper method ``ControllerRegistrationHelpers.RegisterControllers(loadedConfiguration, controllerBuilder)`` available in the `IoC.Configuration.AspNet `_ nuget package ensures that controllers specified in the assemblies within ``iocConfiguration/webApi``` and ``iocConfiguration/pluginsSetup/pluginSetup/webApi`` elements are correctly loaded. .. note:: All classes and interfaces injected into controllers will be resolved by IoC.Configuration container loaded from the ``IoC.Configuration.xml`` configuration file. **Program.cs**: .. sourcecode:: csharp using IoC.Configuration; using IoC.Configuration.AspNet; using IoC.Configuration.AspNet.HostBuilder; using IoC.Configuration.DiContainerBuilder; using IoC.Configuration.DiContainerBuilder.FileBased; using OROptimizer; using OROptimizer.Diagnostics.Log; using WebApiDemo.Startup; LogHelper.RegisterContext(new LogHelperContextLogToConsole()); var builder = WebApplication.CreateBuilder(args); var appData = CreateContainer(builder, () => builder.Services.AddControllers(), (addWebApiServices) => { // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle addWebApiServices.AddEndpointsApiExplorer(); addWebApiServices.AddSwaggerGen(); }); var app = appData.webApplication; // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseAuthorization(); app.MapControllers(); app.Run(); static (WebApplication webApplication, IContainerInfo containerInfo) CreateContainer(WebApplicationBuilder webApplicationBuilder, Func createControllerBuilder, Action addWebApiServices) { var diContainerBuilder = new DiContainerBuilder(); var fileBasedConfigurationParameters = new FileBasedConfigurationParameters( new FileBasedConfigurationFileContentsProvider("IoCConfiguration.xml"), AppContext.BaseDirectory, new AllLoadedAssemblies()) { AttributeValueTransformers = [new FileFolderPathAttributeValueTransformer()] }; var hostIntegratedContainerInfo = diContainerBuilder.StartFileBasedDi(fileBasedConfigurationParameters, loadedConfiguration => { var controllerBuilder = createControllerBuilder(); ControllerRegistrationHelpers.RegisterControllers(loadedConfiguration, controllerBuilder); }) .WithoutPresetDiContainer() // Add additional modules using AddAdditionalDiModules() one or multiple times as necessary // to register modules in addition to DI specified in "IoCConfiguration.xml" // If the method is not called, only the //.AddAdditionalDiModules(new MyModule()) // Use WithHostBuilder(hostBuilder) to make sure IoC.Configuration will register DI with the host builder // Do not call hostBuilder.Build() since this will be done by IoC.Configuration. .WithHostBuilder(new WebApplicationHostBuilder(webApplicationBuilder)) .RegisterServiceProviderAndBuildApp(() => { addWebApiServices(webApplicationBuilder.Services); }); return (hostIntegratedContainerInfo.Host, hostIntegratedContainerInfo.ContainerInfo); } **File ``IoCConfiguration.xml``** located in the same directory as ``WebApiDemo.csproj``. .. code-block:: xml