2017-02-07 6 views
0

Webjobsを使用しているときにファイルを読むにはどうすればよいですか?webjobファイルをローカルでプロダクションで読む

using (StreamReader sr = new StreamReader(VirtualPathProvider.OpenFile("~/content/file/file.txt"))) 
      { 
       template = sr.ReadToEnd(); 
      } 

しかし、ローカルで実行するには、ローカルのために、あなたの説明によると

答えて

3

に失敗:

我々はWebJobプロジェクトルートパスを取得するには、次のコードを使用することができ、これをやろうとしている

。 Azureのために

rootPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())); 

D:\homeは、私たちのために共有されていると、私たちは、このパスにファイルを読み書きできます。ホームディレクトリアクセスの詳細については、documentを参照してください。 Azureのファイル構造はdocumentを参照してください。また、Kudu(http://yourwebsite.scm.azurewebsites.net/)ツールから参照することもできます。

サンドボックスは、d:\ homeを顧客のホームディレクトリにマッピングする動的モードのシンボリックリンクをカーネルモードで実装しています。これは、サイトにアクセスするときに顧客が自分のネットワーク共有パスを参照し続ける必要性を取り除くために行われます。何の環境変数「ホーム」我々はそれを行うには、次のコードを使用することができますがない場合は、サイトの実行、またはどのように多くのサイトがVM上で実行に関係なく、それぞれが

rootPath = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" 

を使って自分のホームディレクトリにアクセスすることができます。続き

string path; 
if (Environment.GetEnvironmentVariable("HOME")!=null) 
{ 
    path = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\testfilename.txt"; 
} 
else 
{ 
    path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\testfilename.txt"; 
    } 

は、詳細テストの手順です:私はタイマーを使用するプロジェクトで

1.Create WebJobプロジェクトとtest.textファイルとフォルダのテスト

enter image description here

2.As WebJobでトリガするので、program.csにconfig.UseTimers()を追加する必要があります。

enter image description here

3。 Function.csファイルに次のコードを追加します。

public static void ProcessQueueMessage([TimerTrigger("00:00:03")] TimerInfo timerInfo, TextWriter log) 
     { 
      string instance = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"); 
      string newMsg = $"WEBSITE_INSTANCE_ID:{instance}, timestamp:{DateTime.Now}"; 
      string path; 
      if (Environment.GetEnvironmentVariable("HOME")!=null) 
      { 
       path = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\test.txt"; 
      } 
      else 
      { 
       path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\test.txt"; 
      } 
      string template = File.ReadAllText(path); 
      log.WriteLine($"NewMsge: {newMsg},file Content:{template}"); 
      Console.WriteLine($"NewMsge: {newMsg},file Content:{template}"); 
     } 

4.ローカルマシンでテストします。

enter image description here

5.Afterはアズールに展開し、AzureのWebJobダッシュボードからログを取得します。

enter image description here

6.Afterはアズールに展開し、AzureのWebJobダッシュボードからログを取得します。

enter image description here

関連する問題