[英]Running a published version of an asp.net core from the cli
從 cli 運行 asp.net 核心應用程序的發布配置版本時出現問題。
該應用程序非常簡單,我使用 Api 模板創建了一個新的 Asp.Net,因此我得到了天氣預報應用程序。
我添加了一個 SqlServer 項目,使用 Entity Framework 核心,並使用從 appsettings.Production.json 文件中獲取的 connectionString 對其進行配置
"ConnectionString": {
"SqlServer": "Server = .; Database = test_Prod; Trusted_Connection = True; "
},
我的 Startup class 構造函數
public Startup(IWebHostEnvironment environment)
{
_configurationRoot = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{ environment.EnvironmentName }.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
_environment = environment;
}
和配置方法
public void Configure(IApplicationBuilder app, ILogger<Startup> logger)
{
if (_environment.IsProduction())
logger.LogInformation($" { Environment.NewLine } Environment { _environment.EnvironmentName } launched { Environment.NewLine }");
logger.LogInformation($" ConnectionString : { _configurationRoot.GetSection("ConnectionString").GetValue<string>("SqlServer") }"); {
AppContext dbContext = app.ApplicationServices.GetRequiredService<AppContext>();
}
我收到以下異常
crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
Application startup exception
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuil
檢查我的日志我得到 Environment is Production 並且 ConnectionString 是空的
以下是我正在執行的命令
dotnet 發布 -c 發布 -o 出
dotnet.\out\myApp.dll
我究竟做錯了什么?
如果從 Visual Studio 運行並且如果使用運行命令從 dotnet cli 運行,該應用程序可以工作
dotnet run -p.\myApp\myApp.csproj
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.