StructureMap和.Net Core

JCi*_*cio 0 c# structuremap .net-core asp.net-core

我正在创建一个准系统.NET Core web api项目(从下面的空白模板开始)https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-网核心/

下面的代码工作正常,直到我添加了StructureMap.现在,我收到了这个错误.

StructureMap.StructureMapConfigurationException:没有注册默认实例,无法自动确定类型'System.IServiceProvider'

没有为System.IServiceProvider指定配置

1.)Container.GetInstance()

在StructureMap.SessionCache.GetDefault(Type pluginType,IPipelineGraph pipelineGraph),位于WebApplication4.Startup.ConfigureServices(IServiceCollection服务)上的StructureMap.Container.GetInstanceT ---从抛出异常的上一个位置开始的堆栈跟踪---在System.Runtime Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()中的Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()中的Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)上的.ExceptionServices.ExceptionDispatchInfo.Throw()

有任何想法吗?

请注意:我们没有使用,builder.AppMvc()因为我们正试图尽可能减少这个api.

这是相关的代码.

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Set*_*Set 5

您忘记使用服务集合填充容器,这就是容器不知道如何解决的原因IServiceProvider.

添加x.Populate(services);行,其中services是一个实例IServiceCollection,你有ConfigureServices方法.这样的事情应该有效:

 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;
}
Run Code Online (Sandbox Code Playgroud)