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-net-core/
在我添加 StructureMap 之前,下面的代码运行良好。现在,我收到了这个错误。
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()
有什么想法吗?
请注意:我们没有使用 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;
}
我正在创建一个准系统 .NET Core web api 项目(从下面的空白模板开始)https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/
在我添加 StructureMap 之前,下面的代码运行良好。现在,我收到了这个错误。
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()
有什么想法吗?
请注意:我们没有使用 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;
}