2016-08-31 16 views
0

(Visual Studio 2015のMVC 5.2.3およびNet Framework 4.5.2を使用して作成された空のアプリケーション)で有効になっているときに同期として動作します。MVC非同期アクションは、Session_StartがGlobal.asax

public class HomeController : Controller 
{ 
public static int Progress = 0; 

public async Task<ActionResult> GetProgressAsync() 
{ 
    Task<ActionResult> task = Task.Run<ActionResult>(() => 
    { 
     var result = new ContentResult(); 
     result.Content = string.Format("Progreso: {0}", Progress); 
     return result; 
    }); 

    return await task; 
} 

public async Task<ActionResult> CommitAsync() 
{ 
    Task<ActionResult> task = Task.Run<ActionResult>(() => 
    { 
     Progress = 0; 

     while (Progress < 100) 
     { 
      Progress++; 
      System.Threading.Thread.Sleep(100); 
     } 

     var result = new ContentResult(); 
     result.Content = string.Format("{0} : Commit completado", DateTime.Now.ToShortTimeString()); 
     return result; 
    }); 

    return await task; 
} 
} 

方法SESSION_START /のSession_EndはGlobal.asax.cs、コントローラで有効になっていない場合は、次のサンプルでは、​​サーバーを想定インジケータ(GetProgressAsync)の進展を示す非同期メソッドを持つコントローラは、一方(CommitAsync)のためにビジーであることを示しタスクが非同期で動作するようになり、タスクの進行状況を(つまりブラウザから)監視することができます。

public class MvcApplication : System.Web.HttpApplication 
{ 
protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
} 

//WARNING: habilitar estos métodos hace que no funcionen las llamadas Async/Await 
//WARNING: enabling this methods hang Async/Await support 
//protected void Session_Start(object sender, EventArgs e) 
//{ 
// // TODO 
//} 

//protected void Session_End(object sender, EventArgs e) 
//{ 
// // TODO 
//} 
} 

セッションメソッドSession_Start/Session_Endが有効になっていると、バグが識別されます。コントローラは、最初の要求に対してのみ非同期として動作します。その後のCommitAsyncリクエストはアプリケーションをハングし、GetProgressAsyncへのリクエストはCommitAsyncが終了するまで処理されません。

答えて

0

それは、コントローラ上でこの属性を使用して動作します:

[SessionState(SessionStateBehavior.ReadOnly)] 
関連する問題