dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.49k stars 10.03k forks source link

Unable to use ConfigureTestServices with StructureMap #5949

Closed rossdargan closed 5 years ago

rossdargan commented 5 years ago

I'm using structure map which means I have to configure my ConfigureServices method to return a IServiceProvider as per the example here: https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection#example-1.

ConfigureTestServices registers a IStartupConfigureServicesFilter which results in the following exception:

A ConfigureServices method that returns an IServiceProvider is not compatible with the use of one or more IStartupConfigureServicesFilter. Use a void returning ConfigureServices method instead or a ConfigureContainer method.

This is thrown here: https://github.com/aspnet/Hosting/blob/cfe9b26a34af15c4943240d790ee5d1a168290c9/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs#L185

I'm trying to register a test authentication schema which as far as I can tell should be done via ConfigureTestServices, but I'm using structuremap so I'm not sure how else I can go about this. My customer app factory looks like this:

public class CustomWebApplicationFactory<TStartup>: WebApplicationFactory<TStartup> where TStartup : class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services => {
            services.AddScheme<TestAuthenticationOptions, TestAuthentication>("Test", null);
        });
    }

}

Is there any way to achieve this without putting some testing code in my primary website (the SUT)?

davidfowl commented 5 years ago

You need to use the ConfigureContainer approach instead of returning the IServiceProvider from ConfigureServices. That style of doing custom DI is going away in 3.0. Do this instead https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection#example

muratg commented 5 years ago

@javiercn can we mark this as deprecated in 3.0?

muratg commented 5 years ago

No more changes planned here.