2016-12-16 11 views
0

Webアプリケーションに.NET 4.5.2を使用していますが、処理されたイメージを返すHTTPハンドラがあります。私は、非同期はjQueryのを使用して処理ハンドラに呼び出し、私は次のエラーを取得し始めている作っています:これは何を意味するのか任意のアイデアHTTPハンドラ非同期問題.NET 4.5.2

 public void ProcessRequest(HttpContext context) 
    { 
     string CaseID = context.Request.QueryString["CaseID"].ToString(); 
     int RotationAngle = Convert.ToInt16(context.Request.QueryString["RotationAngle"].ToString()); 
     string ImagePath = context.Request.QueryString["ImagePath"].ToString(); 

     applyAngle = RotationAngle; 

     string ImageServer = ConfigurationManager.AppSettings["ImageServerURL"].ToString(); 
     string FullImagePath = string.Format("{0}{1}", ImageServer, ImagePath); 

     WebClient wc = new WebClient(); 
     wc.DownloadDataCompleted += wc_DownloadDataCompleted; 
     wc.DownloadDataAsync(new Uri(FullImagePath)); 
    } 

    private void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) 
    { 
     Stream BitmapStream = new MemoryStream(e.Result); 

     Bitmap b = new Bitmap(BitmapStream); 
     ImageFormat ImageFormat = b.RawFormat; 

     b = RotateImage(b, applyAngle, true); 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      if (ImageFormat.Equals(ImageFormat.Png)) 
      { 
       HttpContext.Current.Response.ContentType = "image/png"; 
       b.Save(ms, ImageFormat.Png); 
      } 

      if (ImageFormat.Equals(ImageFormat.Jpeg)) 
      { 
       HttpContext.Current.Response.ContentType = "image/jpg"; 
       b.Save(ms, ImageFormat.Jpeg); 
      } 
      ms.WriteTo(HttpContext.Current.Response.OutputStream); 
     } 
    } 

と私はに何ができる:

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

これは、ハンドラのコードですこれを克服する?

ありがとうございます。

+0

は何を克服しますか?コードはどこですか?これを引き起こしたのは何をしようとしましたか? –

+0

*ハンドラ*コード。 jQueryはHTTPハンドラとは何の関係もなく、ブラウザ上でのみ動作します。 –

+0

コードを追加しました。レビューしてください。 – user1144596

答えて

0

WebClientをProcessRequestメソッド内に作成しても終了するのを待たずに、コードをそのまま使用することはできません。その結果、メソッドが終了するとすぐにクライアントが孤立します。応答が到着するまでに、要求自体は終了しています。応答を書き込むことができるコンテキストまたは出力ストリームはありません。あなたはHttpTaskAsyncHandlerクラスから派生してProcessRequestAsyncメソッドを実装する必要があり、非同期HTTPハンドラを作成するには

public class MyImageAsyncHandler : HttpTaskAsyncHandler 
{ 

    public override async Task ProcessRequestAsync(HttpContext context) 
    { 
     //... 
     using(WebClient wc = new WebClient()) 
     { 
      var data=await wc.DownloadDataTaskAsync(new Uri(FullImagePath));   
      using(var BitmapStream = new MemoryStream(data)) 
      { 
       //... 
       ms.WriteTo(context.Response.OutputStream); 
      //... 
      } 
     } 
    } 
} 
+0

Thanks Panagiotis Kanavos – user1144596