2017-06-08 206 views
0

我创建一个准系统.NET核心Web API项目(从下面空白模板开始)https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/StructureMap和.Net核心

下面的代码工作正常,直到我说StructureMap。现在,我收到这个错误。

StructureMap.StructureMapConfigurationException:没有默认实例 被登记,并且可以不是类型来自动确定 “System.IServiceProvider”

有没有对System.IServiceProvider

1)容器指定的配置.GetInstance()

at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)at StructureMap.Container.GetInstanceT在 WebApplication4.Startup.ConfigureServices(IServiceCollection服务) ---从先前的位置在那里引发异常堆栈跟踪的结尾---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

有任何想法吗?

请注意:我们没有使用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; 
    } 
} 

回答

3

你忘了使用该服务集合来填充的容器,这就是为什么容器不知道如何解决IServiceProvider

添加​​行其中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; 
} 
+0

谢谢..这就像你说的那样。欣赏它。 – JCircio