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

Azure WebJob触发器的Blob路径名提供程序,azure,azure-storage,azure-storage-blobs,azure-webjobs,azure-webjobssdk,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) {

我有一个放在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();
    }
}
问题是:如何摆脱难看的硬编码常量字符串“cBinary/test1/”和“cData/test3/”

硬编码是一个问题,但我需要创建和维护动态创建的两个字符串(blob目录),这取决于支持的类型。此外,我需要在两个位置使用这个字符串值,我不想重复它

例如,我希望将它们放置在某种配置提供程序中,该配置提供程序根据某个枚举构建blob路径字符串


如何执行?

您可以在Meresolver中实现
来动态解析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();
    }
}
public class Functions
{
    public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
    {
        //Do work here
    }
}
最后在
Functions.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();
    }
}
public class Functions
{
    public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
    {
        //Do work here
    }
}
还有更多的信息

希望这有帮助