【发布时间】:2017-06-08 19:37:08
【问题描述】:
我正在创建一个准系统 .NET Core Web api 项目(从下面的空白模板开始)https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/
在我添加 StructureMap 之前,下面的代码运行良好。现在,我收到了这个错误。
StructureMap.StructureMapConfigurationException:没有默认实例 已注册,无法自动确定类型 'System.IServiceProvider'
没有为 System.IServiceProvider 指定配置
1.) Container.GetInstance()
在 StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)在 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;
}
}
【问题讨论】:
标签: c# asp.net-core .net-core structuremap