servicestack - 解析 MVC Controller 中的 ServiceStack 服务

标签 servicestack funq

是否可以将 Servicestack 服务注册为 MVC Controller 中的属性?我问这个问题是因为我遇到了与此问题类似的问题:Timeout expired. - Using Db in ServiceStack Service当我在 MVC Controller 中过快调用此操作时,我会收到超时:

BaseController(我的所有 Controller 都继承于此):

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

服务中的 cargo :

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

最佳答案

不要在 IOC 中重新注册 ServiceStack 服务,因为它们已由 ServiceStack 注册。如果您想在 MVC Controller 中调用 ServiceStack 服务,只需使用已发布的 AppHostBase.ResolveService<T> API,它只是解析来自 IOC 的服务并注入(inject)当前的请求上下文。

请参阅此答案以了解 sharing logic between ServiceStack and MVC 的其他方式.

关于servicestack - 解析 MVC Controller 中的 ServiceStack 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20121991/

相关文章:

servicestack - Chrome JavaScript 错误 : Refused to get unsafe header X-Response-Time with servicestack

authentication - 如何禁用 redis 将自动 session 存储到数据库?

ServiceStack 的 Funq.Container 没有更新属性

ServiceStack Funq RequestScope

httpwebrequest - 使用 ServiceStack.Client 超时

redis - 如何使用 Redis 防止竞争条件?

c# - 如何解决 Funq IoC 中的循环依赖?

servicestack - ServiceStack 中的 Funq 范围(最佳实践)

.net - 如何使用 ServiceStack 将多个读取命令管道传输到 Redis

asp.net-mvc - ServiceStack Funq MVC html 帮助程序扩展