【问题标题】:Azure WebJobs with Storage File Share具有存储文件共享的 Azure WebJobs
【发布时间】:2017-01-08 15:39:45
【问题描述】:

如何使用 FileTrigger 获取上传的文件?

配置代码:

public class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    public static void Main()
    {

        JobHostConfiguration jobConfiguration = new JobHostConfiguration();
        FilesConfiguration fileConfiguration = new FilesConfiguration();

        jobConfiguration.UseFiles(fileConfiguration);
        jobConfiguration.UseTimers();

        var host = new JobHost(jobConfiguration);

        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

以下代码在运行 WebJob 时出错

    public static void ProcessXml([FileTrigger(@"XML\{name}", "*.xml", autoDelete: true)] Stream file, string name, TextWriter log)
    {
        try
        {
            TextReader reader = new StreamReader(file);
            ProcessFile(reader);
        }
        catch (Exception ex)
        {
            log.WriteLine(string.Format("Ao processar o arquivo '{0}', o seguinte erro ocorreu: {1}", name, ex.Message));
        }
        log.WriteLine(string.Format("Arquivo '{0}' processado!", name));
    }

错误:

[08/31/2016 21:59:39 > 0d02fe: INFO] Found the following functions:
[08/31/2016 21:59:39 > 0d02fe: INFO] XXXX.jobs.Functions.ProcessXml
[08/31/2016 21:59:39 > 0d02fe: ERR ] 
[08/31/2016 21:59:39 > 0d02fe: ERR ] Unhandled Exception: System.InvalidOperationException: Path 'D:\home\data\XML' does not exist.
[08/31/2016 21:59:39 > 0d02fe: ERR ]    at Microsoft.Azure.WebJobs.Files.Listeners.FileListener.CreateFileWatcher()
[08/31/2016 21:59:39 > 0d02fe: ERR ]    at Microsoft.Azure.WebJobs.Files.Listeners.FileListener.<StartAsync>d__6.MoveNext()

如何映射文件路径?我尝试使用网络路径作为 RootPath,但是出现错误,指出文件路径无效。

非常感谢您的帮助。

【问题讨论】:

    标签: c# azure storage fileshare


    【解决方案1】:

    可以看到异常抛出代码here:

    if (!Directory.Exists(_watchPath))
    {
        throw new InvalidOperationException(string.Format("Path '{0}' does not exist.", _watchPath));
    }
    

    它要求该文件夹在作业主机启动之前存在。

    一种快速的解决方法是在开始工作之前创建目录:

    public class Program
    {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        public static void Main()
        {
    
             if(!System.IO.Directory.Exists(@"D:\home\data\XML")) 
             {
                  System.IO.Directory.Create(@"D:\home\data\XML");
             }
    
             JobHostConfiguration jobConfiguration = new JobHostConfiguration();
             FilesConfiguration fileConfiguration = new FilesConfiguration();
    
             jobConfiguration.UseFiles(fileConfiguration);
             jobConfiguration.UseTimers();
    
             var host = new JobHost(jobConfiguration);
             host.RunAndBlock();
        }
    

    【讨论】:

    • 您好安德鲁,感谢您的宝贵时间。抱歉,如果我不清楚这个问题,问题是我需要在我的存储帐户(如 Blob、队列)中使用文件共享。使用环境变量 %HOME% 作为文件夹发布的 WebJob,这不是我需要的。我需要使用我的存储帐户的路径。如何做到这一点?
    • 我也有兴趣将共享安装到网络作业。有人可以分享一些知识吗?
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2017-04-11
    • 2020-09-19
    • 2020-12-09
    • 1970-01-01
    • 2020-10-21
    • 2018-12-05
    • 2018-08-09
    相关资源
    最近更新 更多