Question

Is it possible to register a Servicestack Service as a property in an MVC controller? I ask because I'm experiencing a similar issue to this question: Timeout expired. - Using Db in ServiceStack Service whereby I receive a timeout when I call this Action in an MVC controller too quickly:

BaseController (All my controllers inherit from this):

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);
}

GoodsInService:

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;
}

Edit

As a work around I've done the following and removed the registering of services in my container, as per @mythz answer below, which seems to have resolved my issue:

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);
    }
}

This way, I can use my services as a property in an MVC Action, like so:

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

Rather than:

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

Solution

Don't re-register ServiceStack Services in the IOC as they are already registered by ServiceStack. If you want to call ServiceStack Services in MVC Controllers just used the published AppHostBase.ResolveService<T> API, which just resolves the service from the IOC and injects the current request context.

See this answer for other ways of sharing logic between ServiceStack and MVC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top