Description
There are lots of changes in ASP.NET Core 3.0 and breaks StructureMap.Microsoft.DependencyInjection. The new framework dropped the support for configuring third-party DI containers via ConfigureServices, which, returns an IServiceProvider class. Now, developers who use StructureMap.MicrosoftDependencyInjection must use the other method of wiring up the DI container. That is, by using the StructureMap.AspNetCore extensions. However, the current version is still using the IWebHostBuilder and it needs to be migrated to IHostBuilder to support ASP.NET 3.0. The code below shows the current implementation of the ServiceCollectionExtensions and WebHostBuilderExtensions. The former extension class need not to return an IServiceCollection since it's not supported in ASP.NET Core 3 and instead, return a void. Meanwhile, the WebHostBuildExtensions class must call the UseServiceProviderFactory before calling the ConfigureServices method.
Current solution:
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddStructureMap(this IServiceCollection services)
{
return AddStructureMap(services, registry: null);
}
public static IServiceCollection AddStructureMap(this IServiceCollection services, Registry registry)
{
return services.AddSingleton<IServiceProviderFactory<Registry>>(new StructureMapServiceProviderFactory(registry));
}
}
public static class WebHostBuilderExtensions
{
public static IWebHostBuilder UseStructureMap(this IWebHostBuilder builder)
{
return UseStructureMap(builder, registry: null);
}
public static IWebHostBuilder UseStructureMap(this IWebHostBuilder builder, Registry registry)
{
return builder.ConfigureServices(services => services.AddStructureMap(registry));
}
}
Proposed solution:
public static class ServiceCollectionExtensions
{
public static void AddStructureMap(this IServiceCollection services)
{
AddStructureMap(services, registry: null);
}
public static void AddStructureMap(this IServiceCollection services, Registry registry)
{
services.AddSingleton<IServiceProviderFactory<Registry>>(new StructureMapServiceProviderFactory(registry));
}
}
public static class HostBuilderExtensions
{
public static IHostBuilder UseStructureMap(this IHostBuilder builder)
{
return UseStructureMap(builder, registry: null);
}
public static IHostBuilder UseStructureMap(this IHostBuilder builder, Registry registry)
{
return builder
.UseServiceProviderFactory<Registry>(new StructureMapServiceProviderFactory(registry))
.ConfigureServices(services => services.AddStructureMap(registry));
}
}