我正在将aspnet5 / rc-1网站转换为已发布的aspnet核心。我遇到了涉及开发环境中的用户机密的问题。
在Startup构造函数中对AddUserSecrets的调用会抛出异常:
public Startup( IHostingEnvironment env )
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile( "appsettings.json" )
.AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true );
if( env.IsDevelopment() )
{
// this line blows up
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
错误信息是在bin / Debug / netcoreapp1.0文件夹中找不到project.json文件的结果......这很奇怪,因为我不希望找到project.json首先在可分发的可执行文件夹中输入文件。
这一切在rc1下都运行良好,所以有些事情发生了变化。
其他信息
以下是例外情况:
System.InvalidOperationException未被用户代码
处理 HResult = -2146233079消息=无法找到project.json 'C:\程序\的SpeedView的\ src \的SpeedView \ BIN \调试\ netcoreapp1.0 \'。
Source = Microsoft.Extensions.Configuration.UserSecrets StackTrace: 在Microsoft.Extensions.Configuration.UserSecrets.PathHelper.GetSecretsPath(IFileProvider) 提供商) 在Microsoft.Extensions.Configuration.ConfigurationExtensions.AddUserSecrets(IConfigurationBuilder) 组态) at SpeedView.Startup..ctor(IHostingEnvironment env)在C:\ Programming \ SpeedView \ src \ SpeedView \ Startup.cs:第48行
的InnerException:
这是project.json文件:
{
"userSecretsId": "aspnet5-SpeedView-f526a1b2-f58b-4e04-b189-8442609eff8c",
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Antiforgery": "1.0.0",
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": {
"version": "1.0.0",
"type": "build"
},
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"AutoMapper": "5.0.2",
"Serilog.Extensions.Logging": "1.1.0",
"Serilog.Sinks.ColoredConsole": "2.0.0",
"Serilog.Sinks.Literate": "2.0.0",
"Serilog.Sinks.RollingFile": "2.2.0",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview2-final",
"imports": [
"portable-net45+win8"
]
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp siteprep" ]
}
}
答案 0 :(得分:2)
不要忘记设置您的BasePath:
public Startup( IHostingEnvironment env )
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath) // Add this line <-----
.AddJsonFile( "appsettings.json" )
.AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true );