Azure WebJobs SDK 3,尝试添加BlobTrigger和相关的连接字符串 [英] Azure WebJobs SDK 3, trying to add a BlobTrigger and related connection string
问题描述
旧的做事方式看起来像这样:
The old way of doing things looked as so:
var jobConfig = new JobHostConfiguration(cfg.DocumentDatabase.BlobStorageServer)
{
NameResolver = new Support.BlobNameResolver(_env)
};
jobConfig.Queues.MaxPollingInterval = TimeSpan.FromSeconds(_pollSeconds);
_wjHost = new JobHost(jobConfig);
我正在尝试将此转换为3.0中的新方法,这就是我已经走了多远:
I am trying to translate this to the new way in 3.0, and this is how far I have come:
_wjHost = new HostBuilder().ConfigureWebJobs(b =>
{
b.AddAzureStorage(x =>
{
x.MaxPollingInterval = TimeSpan.FromSeconds(_pollSeconds);
});
}).ConfigureServices(s =>
{
s.AddSingleton<INameResolver, Support.BlobNameResolver>(_ => new Support.BlobNameResolver(_env));
s.Configure<QueuesOptions>(o =>
{
o.MaxPollingInterval = TimeSpan.FromSeconds(_pollSeconds);
});
}).Build();
首先,我不知道哪个MaxPollingInterval
是正确的使用方式...所以我将两者都设置了.我认为我不应该使用AddBlobStorage
Firstly, i don't know which MaxPollingInterval
is the right one to use... so i set both. I assume i shouldn't be using the one in AddBlobStorage
第二,更重要的是,我在哪里指定Blob存储连接字符串?在上述情况下,它是存储在cfg.DocumentDatabase.BlobStorageServer
Secondly, and more importantly, where do I specify the blob storage connection string? In the case above, it's the setting stored in cfg.DocumentDatabase.BlobStorageServer
谢谢
推荐答案
因此,在盯着webjob SDK的源代码后,我发现了一个难题.好吧,我认为这是一个矛盾.它可以正常工作,现在我可以使用新的3.0 SDK.
So, after staring at the source code for the webjob SDK, I found a kludge. Well, I think it's a kludge. It works and I can now use the new 3.0 SDK.
我将其发布在这里,主要是因为我担心没有其他方法可以使用自己的配置文件来做到这一点.
I am posting this here, mainly because I fear there is no other way to do this using my own configuration files.
如果这是错误的,请让我知道,我将删除此答案.
If this is wrong, please just let me know and I will delete this answer.
所以我的代码现在看起来像这样:
So my code now looks like this:
_wjHost = new HostBuilder().ConfigureWebJobs(b =>
{
b.AddAzureStorage(x =>
{
x.MaxPollingInterval = TimeSpan.FromSeconds(_pollSeconds);
});
}).ConfigureServices(s =>
{
s.AddSingleton(new StorageAccountProvider(new BlobStorageConfiguration(cfg.DocumentDatabase.BlobStorageServer)));
s.AddSingleton<INameResolver, Support.BlobNameResolver>(_ => new Support.BlobNameResolver(_env));
s.Configure<QueuesOptions>(o =>
{
o.MaxPollingInterval = TimeSpan.FromSeconds(_pollSeconds);
});
}).Build();
我添加的行是s.AddSingleton(new StorageAccountProvider(new BlobStorageConfiguration(cfg.DocumentDatabase.BlobStorageServer)));
webjobs SDK专门在寻找一个名为Storage
的密钥.因此,我必须实现IConfiguration
并按以下步骤进行处理:
The webjobs SDK is specifically looking for a key named Storage
. So I had to implement IConfiguration
and kludge this in as so:
private sealed class BlobStorageConfiguration : IConfiguration
{
private readonly string _bsConnString;
public BlobStorageConfiguration(string connString)
{
_bsConnString = connString;
}
public string this[string key]
{
get => key == "Storage" ? _bsConnString : null;
set { }
}
public IEnumerable<IConfigurationSection> GetChildren() => null;
public IChangeToken GetReloadToken() => null;
public IConfigurationSection GetSection(string key) => null;
}
现在触发器触发得很好.不漂亮.但是关于新的IHost方法,有零文档.
and now the trigger is firing just fine. Not pretty. But there is ZERO documentation on the new IHost methods.
这篇关于Azure WebJobs SDK 3,尝试添加BlobTrigger和相关的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!