2

Servicestack サービスを MVC コントローラーのプロパティとして登録することは可能ですか? この質問と同様の問題が発生しているため、質問します:タイムアウトが期限切れになりました。- ServiceStack サービスで Db を使用すると、MVC コントローラーでこのアクションを呼び出すのが速すぎるとタイムアウトが発生します。

BaseController (すべてのコントローラーはこれから継承します):

public class BaseController : Controller
{
    public GoodsInService GoodsInService { get; set; }
    public GoodsInProductService GoodsInProductService { get; set; }
    public ReturnTypeService ReturnTypeService { get; set; }
}

GoodsInController:

public ActionResult Details(int id)
{
    var goodsIn = GoodsInService.Get(new GoodsIn
    {
        Id = id
    });

    return View(goodsIn);
}

サービス中の商品:

public GoodsIn Get(GoodsIn request)
{
    var goodsIn = Db.Id<GoodsIn>(request.Id);

    using (var goodsInProductSvc = ResolveService<GoodsInProductService>())
    using (var returnTypeSvc = ResolveService<ReturnTypeService>())
    {
        goodsIn.GoodsInProducts = goodsInProductSvc.Get(new GoodsInProducts
        {
            GoodsInId = goodsIn.Id
        });
        goodsIn.ReturnType = returnTypeSvc.Get(new ReturnType
        {
            Id = goodsIn.ReturnTypeId
        });
    }

    return goodsIn;
}

編集

回避策として、以下の@mythzの回答に従って、次のことを行い、コンテナー内のサービスの登録を削除しました。これにより、問題が解決したようです。

public class BaseController : ServiceStackController
{
    public GoodsInService GoodsInService { get; set; }
    public GoodsInProductService GoodsInProductService { get; set; }
    public ReturnTypeService ReturnTypeService { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        GoodsInService = AppHostBase.ResolveService<GoodsInService>(System.Web.HttpContext.Current);
        GoodsInProductService = AppHostBase.ResolveService<GoodsInProductService>(System.Web.HttpContext.Current);
        ReturnTypeService = AppHostBase.ResolveService<ReturnTypeService>(System.Web.HttpContext.Current);

        base.OnActionExecuting(filterContext);
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        GoodsInService.Dispose();
        GoodsInProductService.Dispose();
        ReturnTypeService.Dispose();

        base.OnActionExecuted(filterContext);
    }
}

このようにして、次のように、サービスを MVC アクションのプロパティとして使用できます。

goodsIn = GoodsInService.Get(new GoodsIn
{
    Id = id
});

それよりも:

using (var goodsInSvc = AppHostBase.ResolveService<GoodsInService>
          (System.Web.HttpContext.Current))
{
    goodsIn = goodsInSvc.Get(new GoodsIn
    {
        Id = id
    });
}
4

1 に答える 1