WCF restful服务注入WebOperationContext与autofacc

本文关键字:autofacc WebOperationContext 注入 restful 服务 WCF | 更新日期: 2023-09-27 18:06:01

我一直在做。net 4.0 webapi restful service项目。在该项目中的一个对象中,我们想要注入WebOberationContext。当前的动态。是否有任何方法来实现这一点,使用Autofac。我在谷歌上搜索了很多,但还没有找到解决方案。目前,我使用以下代码:

public class SomeObject : ISomeObject
{
     private readonly ISomeService _someService;
     public SomeObject(ISomeService someService)
     {
        _someService = someService;
     }
     public WebOperationContext Context { get; set; }
     public void SomeOperation()
     {
          var incomeRequtests = Context.Current.IncomingRequests;
     }
}

比起将WebOperationContext设置为属性,我想知道,如果我们可以使用autofac自动注入它。

任何信息都是非常感谢的。

谢谢

WCF restful服务注入WebOperationContext与autofacc

不,你不能改变上下文。这是WCF经常被抱怨的一个问题,缺乏"单元可测试性"(特别是WebOperationContext或OperationContext)。新的WCF Web api(来自http://wcf.codeplex.com)是解决这个问题的良好开端。