ASP.NET MVCアプリケーションをSTAモードで実行する必要があります。この目的のために私はProgramming ASP.NET MVC 4: Developing Real-World Web Applications with ASP.NET MVCに基づいていくつかのカスタムクラスを開発しました。ここで彼らは、次のとおりです。STA MVCアプリケーションで長時間実行されているリクエストが終了しない
public class StaThreadRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
if (requestContext == null)
throw new ArgumentNullException("requestContext");
return new StaThreadHttpAsyncHandler(requestContext);
}
}
public class StaThreadHttpAsyncHandler : Page, IHttpAsyncHandler, IRequiresSessionState
{
private readonly RequestContext _requestContext;
public StaThreadHttpAsyncHandler(RequestContext requestContext)
{
if (requestContext == null)
throw new ArgumentNullException("requestContext");
_requestContext = requestContext;
}
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
return this.AspCompatBeginProcessRequest(context, cb, extraData);
}
protected override void OnInit(EventArgs e)
{
var controllerName = _requestContext.RouteData.GetRequiredString("controller");
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var controller = controllerFactory.CreateController(_requestContext, controllerName);
if (controller == null)
throw new InvalidOperationException("Could not find controller: " + controllerName);
try
{
controller.Execute(_requestContext);
}
finally
{
controllerFactory.ReleaseController(controller);
}
this.Context.ApplicationInstance.CompleteRequest();
}
public void EndProcessRequest(IAsyncResult result)
{
this.AspCompatEndProcessRequest(result);
}
public override void ProcessRequest(HttpContext httpContext)
{
throw new NotSupportedException("STAThreadRouteHandler does not support ProcessRequest called (only BeginProcessRequest)");
}
}
も調整RouteConfig:
routes.Add(new Route("{controller}/{action}/{id}", new StaThreadRouteHandler()))
すべては私の行動の大多数のため正常に動作しますが、私は終了する10秒と20秒の間のどこかに取る2を持っています。これらの2つのアクションでは、メソッドEndProcessRequest
は実行中に例外がスローされず、this.Context.ApplicationInstance.CompleteRequest();
が問題なしでOnInit
の中で呼び出されても、決して実行されません。その結果、このような要求はIISで状態ExecuteRequestHandler
で終了し、永遠にそこに滞留し、新しい要求が処理されるのをブロックします。
どうしてですか? EndProcessRequest
のブレークポイントは長時間実行されるアクションでは決してヒットしませんが、短いアクション(1〜5秒)ではうまく動作します。