简体   繁体   中英

StructureMap and .Net Core

I am creating a barebones .NET Core web api project (Started from blank template below) https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/

The code below was working fine, until I added StructureMap. Now, I am getting this error.

StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'System.IServiceProvider'

There is no configuration specified for System.IServiceProvider

1.) Container.GetInstance()

at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) at StructureMap.Container.GetInstanceT at WebApplication4.Startup.ConfigureServices(IServiceCollection services) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Any ideas?

Please note: We are not using builder.AppMvc() as we are trying to slim this api down as much as possible.

Here is the relevant code.

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvcCore();
        builder.AddApiExplorer();

        builder.AddAuthorization();
        builder.AddFormatterMappings();
        builder.AddJsonFormatters();
        builder.AddCors();

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });

        var container = ContainerConfigurator.Configure();

        return container.GetInstance<IServiceProvider>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();

        app.UseMvc();

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
    }

public class ContainerConfigurator
{
    public static Container Configure()
    {
        var container = new Container(
            x => x.Scan
            (
                s =>
                {
                    s.TheCallingAssembly();
                    s.WithDefaultConventions();
                    s.AddAllTypesOf<IStartupTask>();
                    s.LookForRegistries();
                    s.AssembliesAndExecutablesFromApplicationBaseDirectory();

                }
            )
        );


        return container;
    }
}

You forgot to populate the container using the service collection, that's why container doesn't know how to resolve IServiceProvider .

Add x.Populate(services); line where services is an instance of IServiceCollection that you have in ConfigureServices method. Something like this should work:

 public static Container Configure(IServiceCollection services)
 {
    var container = new Container( config  => 
      {
          config.Scan(s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
                s.AddAllTypesOf<IStartupTask>();
                s.LookForRegistries();
                s.AssembliesAndExecutablesFromApplicationBaseDirectory();
            });
          config.Populate(services);  
       });

    return container;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM