我需要在Ubuntu 16.04 LTS服务器上的dotnet核心中设置一个项目,并连接一个数据库。我按照本教程进行操作,并将其设置为在某个域名/ ip上联机。
我使用的教程: https://www.hanselman.com/blog/PublishingAnASPNETCoreWebsiteToACheapLinuxVMHost.aspx
此时,我的简洁,干净的mvc项目正在运行。但是,我需要与数据库建立连接。我有一个在不同的服务器上运行(我有正确的凭据)。但我有托管.json文件的问题。它似乎与常规appsettings.json文件冲突。
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"correctdb": "Server=correctip;Database=correctdb;Uid=SA;Pwd=correctpass",
"providerName": "Sql.Data.SqlClient"
}
}
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.AddJsonFile("appsettings.json", optional: true)
.Build();
这是我的错误消息:error 我的主要问题是,我觉得它与dotnet核心v2中的Kestrel设置相冲突,使用了像本教程所说的hosting.json。
每当我使用Program.cs时,v2只能通过数据库连接脱机运行。每当我使用Program.cs v1时,它都会在线运行,但没有连接。
是否有其他方法可以在不使用hosting.json的情况下配置Kestrel? 非常感谢帮助。
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at webapp.Program.Main(String[] args) in /home/ubuntu/webapp/Program.cs:line 30
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<WebappContext>(options => options.UseSqlServer(Configuration.GetConnectionString("correctdb")));
services.AddMvc();
}
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddCommandLine(args)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}
[21:20:49.129] [InstallWorkflow] Pre-requisite failed: Docker for Windows requires Windows 10 Pro or Enterprise version 14393, or Windows server 2016 RTM to run
答案 0 :(得分:0)
这听起来像是一个愚蠢的问题,但你确定你上传了appsettings.json吗?
此外,v1未加载appsettings.json文件,CreateDefaultBuilder
所做的是加载标准内容,因此它会在appsettings.json存在时加载。