2016-05-11 15 views
2

C#で非同期ASP.NET WEB APIファイルアップローダを作成しようとしています。順番にアップロードを受け取る)。C#.NET WEB API:さまざまな非同期HTTPリクエストによってアクセス可能で変更可能なサイクリングファイル

ファイルのアップロードは、HTTP POSTリクエストに非同期応答、すべて良いと行われているが、私は次のようなコードが必要になります。

public class FileUploadPlaceholder 
{ 
    private string[] uploadPathArray = new string[] { "~/import1", "~/import2", "~/import3" }; 
    private int uploadPathPointer = 0; 
    private int uploadPathPointerMax = 2; 

    public string uploadPath() 
    { 
     return uploadPathArray[uploadPathPointer]; 
    } 

    public void cycleUploadPath() 
    { 
    //the below will be thread safe once I get further along 
     if (uploadPathPointer < uploadPathPointerMax) 
     { 
      uploadPathPointer += 1; 
     } 
     else 
     { 
      uploadPathPointer = 0; 
     } 
    //the above will be thread safe once I get further along 
    } 

} 

...すべての非同期要求によって常にアクセス可能。アプリケーションが初期化されると、常に変化するこのクラスのインスタンスは、すべての要求によってアクセス可能になります。このようなコードが置かれている場所と、私のようなコントローラ、FileUploadControllerによってどのようにアクセスされているのか、ベストプラクティスは何ですか?

また、私はちょうどC#で始まり、私が何か慣習的でないことを知りたいと熱望しているので、自分のコードを批判することは自由ですが、ここで学習者を説明してください!

答えて

0

すべてのクラスメソッドとフィールドを静的にすることで、ソリューションを自分で作成しました。そのため、クラスのインスタンスではなくクラスに関する位置を持つ配列を繰り返し処理します。このクラスは、同じ名前空間内のFileUploaderControllerのすぐ隣に置くことができます。

public class FileUploadPlaceholder 
{ 
    static private string[] uploadPathArray = new string[] { "~/import1", "~/import2", "~/import3" }; 
    static private int uploadPathPointer = 0; 
    static private int uploadPathPointerMax = 2; 

    static public string uploadPath() 
    { 
     return uploadPathArray[uploadPathPointer]; 
    } 

    static public void cycleUploadPath() 
    { 
    //the below will be thread safe once I get further along 
     if (uploadPathPointer < uploadPathPointerMax) 
     { 
      uploadPathPointer += 1; 
     } 
     else 
     { 
      uploadPathPointer = 0; 
     } 
    //the above will be thread safe once I get further along 
    } 

} 

//[Authorize] 
public class FileUploadController : ApiController 
{ 

    public async Task<List<string>> PostAsync() 
    { 
     if (Request.Content.IsMimeMultipartContent()) 
     { 
      string uploadPath = HttpContext.Current.Server.MapPath(FileUploadPlaceholder.uploadPath()); 
[...] 
関連する問題