簡體   English   中英

StructureMap和.Net Core

[英]StructureMap and .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()

在WebApplication4.Startup.ConfigureServices(IServiceCollection服務)的StructureMap.Container.GetInstanceT處的StructureMap.SessionCache.GetDefault(Type pluginType,IPipelineGraphpipelineGraph)處-從上一個引發異常的位置開始的堆棧跟蹤-在System.Runtime處Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection服務)的.ExceptionServices.ExceptionDispatchInfo.Throw()Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()的Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()的Microsoft.AspNetCore.Hosting。

有任何想法嗎?

請注意:我們沒有使用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;
    }
}

您忘記了使用服務集合填充容器,這就是為什么容器不知道如何解析IServiceProvider

添加x.Populate(services); 行,其中services是您在ConfigureServices方法中擁有的IServiceCollection的實例。 這樣的事情應該起作用:

 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM