【问题标题】:Deploying .NET Core console app as web job not picking up connection string from my config.json将 .NET Core 控制台应用程序部署为 Web 作业,而不是从我的 config.json 中获取连接字符串
【发布时间】:2018-08-12 12:42:29
【问题描述】:

我有一个已部署为 Web 作业的 .NET Core 应用程序,该 Web 作业部署成功,但未执行任何任务。我认为它无法从 config.json 获取连接字符串。

.Net Core 控制台应用程序默认没有配置文件,所以我手动添加了一个并使用以下代码读取其配置:

            var builder = new ConfigurationBuilder();
        string directory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
        var builder2 = builder.SetBasePath(directory);
        var builder3 = builder2.AddJsonFile("config.json", true, true);
        _configuration = builder3.Build();

                var optionsBuilder = new DbContextOptionsBuilder<UserContext>();
            optionsBuilder.UseSqlServer(_configuration.GetConnectionString("ROSCAConnection"));
            UserContext userContext = new UserContext(optionsBuilder.Options);

这在我的机器上运行良好。当我发布 .NET Core 应用程序时,它不会将我的 config.json 文件提取到我的发布输出中。

这是我的 config.json 文件:

{
"ConnectionStrings": {
    "ROSCAConnection": "Data Source=***.database.windows.net;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***"
},
"Ripple": {
    "baseURL": "https://s.altnet.rippletest.net:51234",
    "account": "***",
    "secret": "***"
},
"Logging": {
    "IncludeScopes": false,
    "Debug": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "Console": {
        "LogLevel": {
            "Default": "Warning"
        }
    }
}

}

如何发布此 .NET Core 控制台应用程序,以便它读取我的 config.json?

在 azure 中检查应用服务编辑器确认 config.json 确实存在

【问题讨论】:

  • i think it has failed to get connection string frommy config.json你是怎么得出这个结论的?
  • @mjwills 我添加了有问题的图像...... azure 应用服务编辑器确实确认 config.json 存在......仍然网络作业没有执行其任务
  • 将 Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;让 config.json 在 azure 中工作?

标签: c# .net-core console-application config azure-webjobs


【解决方案1】:

我已经测试过了,你可以参考以下步骤:

1.appsettings.json 文件的Copy to Output Directory 属性也应设置为Copy if newer,以便应用程序在发布时能够访问它

2.设置是在main方法中注入的,而不是像web应用程序那样在启动方法中注入,但代码基本相同。我使用你提供的appsetting.json

static void Main(string[] args)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    IConfigurationRoot configuration = builder.Build();
    Console.WriteLine("hello");
    Console.WriteLine(configuration.GetConnectionString("ROSCAConnection"));
}

3.要解析SetBasePath,将依赖项添加到Microsoft.Extensions.Configuration.FileExtensions 包。 要解析AddJsonFile,请将依赖项添加到Microsoft.Extensions.Configuration.Json 包。

4.webjobs 输出。

【讨论】:

  • 如果我的配置文件被命名为 config.json 而不是 appsettings.json 会有区别吗? @JoyeCai
  • 其实没有什么不同。您需要确保文件格式为 json 文件,并通过 AddJsonFile 加载此 config.json。并将 json 文件属性设置为copy if newer。然后它会正常工作。
  • 嗨!同样,Console 应用程序是否可以读取 Azure 应用服务的配置区域中定义的设置,而不是从 *.json 文件中读取?
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 2021-01-09
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2017-03-06
相关资源
最近更新 更多