3

How can I use a FileTrigger to get my uploaded file?

Configuration code:

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

The following code gives error when running 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));
    }

Error:

[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()

How can I map the file path? I tried to use the network path as RootPath, however, an error occurs stating that the file path is invalid.

Thank you very much any help.

1 Answers1

0

You can see the exception throwing code here:

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

It requires the folder to exist before the job host starts.

A quick workaround is to create the directory before you kick off the job:

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();
    }
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
  • 2
    Hi Andrew, thanks for your time. Sorry if I was not clear on the question, the problem is I need to use the File Share in my Storage Account (like Blob, Queue). The WebJob published using the environment variable %HOME% as folder and this is not what I need. I need to use the path of my Storage Account. How to do this? – Carlos Saraiva Jr. Sep 01 '16 at 11:25
  • I am also interested in mounting a share to a web job. Can someone share some knowledge ? – sebastian.roibu Mar 13 '17 at 12:57