c# - Azure webjobs JobHostConfiguration 缺失,现在无法进行设置
问题描述
我想将我当前的 webjobs 配置重写为 3.0 版,但我无法让它与文档一起使用,因为我不知道如何设置dashboardconnectionstring
或storageconnectionstring
不设置配置文件。
JobHostConfiguration config = new JobHostConfiguration
{
NameResolver = new WebJobsNameResolver()
};
string defaultStorageConnectionString = string.Format( "Some dynamically generation string" );
config.DashboardConnectionString = defaultStorageConnectionString;
config.StorageConnectionString = defaultStorageConnectionString;
using(JobHost host = new JobHost(config))
{
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
我想让它使用正确的存储和仪表板连接字符串连续运行,而不使用配置文件。
解决方案
3.0.0 NuGet 包更新(非测试版)带来了重大变化。它基于类似于 asp.net 主机的通用主机。您可以参考以下步骤:
1.在你的program.cs中添加这行代码。
.ConfigureAppConfiguration((context, config) => {
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
Program.cs 中的整个代码。
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebJob1template
{
class Program
{
static void Main()
{
var builder = new HostBuilder()
.UseEnvironment("Development")
.ConfigureAppConfiguration((context, config) => {
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureWebJobs(
b =>
{
b.AddAzureStorageCoreServices()
.AddAzureStorage()
.AddTimers()
.AddFiles();
//.AddDashboardLogging();
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
})
.UseConsoleLifetime();
var host = builder.Build();
using (host)
{
host.Run();
}
}
}
}
2.Set appsettings.json
(注意设置它的属性Copy to Output Directory
或 Copy always
):
{
"ConnectionStrings": {
"AzureWebJobsDashboard": "xxxx",
"AzureWebJobsStorage": "xxxx"
}
}
3.Functions.cs:
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace WebJob1template
{
public class Functions
{
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger log)
{
//log.WriteLine(message);
log.LogInformation(message);
}
}
}
有关更多详细信息,您可以参考本教程。
更新:
正如乔伊所说,我们可以使用
config.AddInMemoryCollection(settings);
和
public static Dictionary<string, string> settings = new Dictionary<string, string>
{
{"ConnectionStrings:AzureWebJobsDashboard:0", "xxxxxxx"},
{"ConnectionStrings:AzureWebJobsStorage:1", "xxxxxx"},
};
这样它就不会使用配置文件。这是有关如何使用的文章AddInMemoryCollection
推荐阅读
- python - 如何使用带有 python 的正则表达式从 XML 文件中删除多个空格和字符?
- python - 当我更改我的 css 代码时如何重新加载烧瓶服务器?
- git - Git克隆错误消息“永久添加到已知主机列表”
- android - 我将使用 React Native 开发一个简单的帐户处理移动应用程序,为此我应该使用什么数据库?
- angular - 如何在 Angular 7 中使用反应式表单设置“至少应填写一个字段”验证?
- javascript - 有没有办法在 Discord.js 中制作频道表情?
- docker - docker-compose chrome-alpine 使用 nginx
- joomla - 创建仅包含文本的页面
- google-chrome - 为什么 instagram 不时在浏览器上抛出“Expected Dict got Mapping”错误?
- python - 有没有办法用另一个变量命名一个变量