C# 但是,通过dotnet run运行终端的ion似乎可以在生产环境中运行应用程序。将终端中使用的环境更改为开发(export-ASPNETCORE\u-environment=development)后,一切正常。您是从与项目相同的目录中使用dotnet r
C# 但是,通过dotnet run运行终端的ion似乎可以在生产环境中运行应用程序。将终端中使用的环境更改为开发(export-ASPNETCORE\u-environment=development)后,一切正常。您是从与项目相同的目录中使用dotnet r,c#,postgresql,angular,.net-core,C#,Postgresql,Angular,.net Core,但是,通过dotnet run运行终端的ion似乎可以在生产环境中运行应用程序。将终端中使用的环境更改为开发(export-ASPNETCORE\u-environment=development)后,一切正常。您是从与项目相同的目录中使用dotnet run,还是从不同的目录中使用它?(例如,dotnet运行-p path/to.csproj)是的,我使用的dotnet运行来自与项目源相同的目录。如果我从代码中删除DbSeeder,它就可以正常工作。因为对UseNpgsql()的调用抛出后,连
但是,通过
dotnet run
运行终端的ion似乎可以在生产环境中运行应用程序。将终端中使用的环境更改为开发(export-ASPNETCORE\u-environment=development
)后,一切正常。您是从与项目相同的目录中使用dotnet run
,还是从不同的目录中使用它?(例如,dotnet运行-p path/to.csproj
)是的,我使用的dotnet运行来自与项目源相同的目录。如果我从代码中删除DbSeeder,它就可以正常工作。因为对UseNpgsql()
的调用抛出后,连接字符串似乎无法从配置中读取(这是在解析DbSeeder
时传递执行的,因为它可能取决于上下文)
Unhandled Exception: System.Exception: Could not resolve a service of type 'OpenGameListApp.Data.DbSeeder' for the parameter 'dbSeeder' of method 'Configure' on type 'OpenGameListApp.Startup'. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: connectionString
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions.UseNpgsql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 NpgsqlOptionsAction)
at OpenGameListApp.Startup.<ConfigureServices>b__4_0(DbContextOptionsBuilder options) in /home/noam/projects/dotnet/OpenGameListApp/src/Startup.cs:line 43
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at OpenGameListApp.Program.Main(String[] args) in /home/noam/projects/dotnet/OpenGameListApp/src/Program.cs:line 15
public void ConfigureServices(IServiceCollection services)
{
// Add framework services
services.AddMvc();
// Add entity framework
services.AddEntityFrameworkNpgsql()
.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add database seeder
services.AddSingleton<DbSeeder>();
}
// 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, DbSeeder dbSeeder)
{
// Add logger
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
// Rewrite all routing urls including the root one to the index.html file by adding the following rules
var rewriteOptions = new RewriteOptions().AddIISUrlRewrite(env.ContentRootFileProvider, "rewrite-rules.xml");
// Add iis url rewrite features to kestrel
// Note: the app.UseRewriter method must be called before app.UseDefaultFiles and app.UseStaticFiles
app.UseRewriter(rewriteOptions);
// In order for your Web app to serve a default page without the user having to fully qualify the URI,
app.UseDefaultFiles();
// Enable service of static files in wwwroot folder
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
// Disable caching for all static files
context.Context.Response.Headers["Cache-Control"] = Configuration["StaticFiles:Headers:Cache-Control"];
context.Context.Response.Headers["Pragma"] = Configuration["StaticFiles:Headers:Pragma"];
context.Context.Response.Headers["Expires"] = Configuration["StaticFiles:Headers:Expires"];
}
});
// Enable the use of asp.net mvc framework
app.UseMvc();
// Initialize auto mapper
Mapper.Initialize(cfg => cfg.CreateMap<Item, ItemViewModel>());
// Seed database
dbSeeder.SeedIt();
}