Azure WebJobs与存储文件共享

时间:2016-08-31 22:23:52

标签: c# azure storage fileshare

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

配置代码:

-- questions and answers
CREATE TABLE posts
(
  id bigserial PRIMARY KEY,
  created_at timestamp(3) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,

  title character varying(100),
  text text,
  post_url character varying(100),

  score integer DEFAULT 0,
  author_id integer NOT NULL REFERENCES users (id),
  parent_post_id integer REFERENCES posts (id),
  post_type varchar(30),

  is_accepted boolean DEFAULT FALSE,
  acceptor_id integer REFERENCES users (id) DEFAULT NULL
  --seen_by_parent_post_author boolean DEFAULT false

  --view_count
  --accepted_answer_id
  --answer_count
);

CREATE TABLE tags
(
    id bigserial PRIMARY KEY,
    created_at timestamp(3) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    label character varying(30) NOT NULL,
    description character varying(200),
    category character varying(50)
);

以下代码在运行WebJob

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

错误:

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

如何映射文件路径?我尝试将网络路径用作RootPath,但是,发生错误表明文件路径无效。

非常感谢你的任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以看到异常抛出代码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();
    }