and*_*fox 3 azure azure-storage azure-storage-blobs azure-webjobs azure-webjobssdk
我有一个放在WebJob项目中的以下测试代码.在"cBinary/test1 /"存储帐户中创建(或更改)任何blob后触发.
代码有效.
public class Triggers
{
public void OnBlobCreated(
[BlobTrigger("cBinary/test1/{name}")] Stream blob,
[Blob("cData/test3/{name}.txt")] out string output)
{
output = DateTime.Now.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:如何摆脱丑陋的硬编码const字符串"cBinary/test1 /"和""cData/test3 /"?
硬编码是一个问题,但我需要创建和维护几个动态创建的字符串(blob目录) - 取决于支持的类型.更重要的是 - 我需要在几个地方使用这个字符串值,我不想复制它.
我希望将它们放在某种配置提供程序中,例如,根据某些枚举构建blob路径字符串.
怎么做?
您可以实现INameResolver动态解析QueueNames和BlobNames.您可以添加逻辑来解析那里的名称.下面是一些示例代码.
public class BlobNameResolver : INameResolver
{
public string Resolve(string name)
{
if (name == "blobNameKey")
{
//Do whatever you want to do to get the dynamic name
return "the name of the blob container";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你需要把它连接起来 Program.cs
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
//Configure JobHost
var storageConnectionString = "your connection string";
//Hook up the NameResolver
var config = new JobHostConfiguration(storageConnectionString) { NameResolver = new BlobNameResolver() };
config.Queues.BatchSize = 32;
//Pass configuration to JobJost
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
Run Code Online (Sandbox Code Playgroud)
最后在 Functions.cs
public class Functions
{
public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
{
//Do work here
}
}
Run Code Online (Sandbox Code Playgroud)
这里有更多信息.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
577 次 |
| 最近记录: |