WebJob 触发器的 Blob 路径名称提供程序

Blob path name provider for WebJob trigger

我在 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();
    }
}

问题是:如何摆脱丑陋的硬编码 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";
        }
    }
}

然后你需要把它连接到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();
    }
}

终于在 Functions.cs

public class Functions
{
    public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
    {
        //Do work here
    }
}

还有更多信息here.

希望这对您有所帮助。