我有使用MSMQ结合其在IIS 7.0使用Windows激活服务激活的单程WCF服务。

我在NInject,所以我一直在使用NInject扩展WCF,这对于一个典型的HTTP WCF服务将工作的伟大的大风扇。

然而,在WAS激活服务没有HTTP管道,所以我的绑定类型时,因为System.Web.HttpContext.Current为空我不能使用InRequestScope。我挣扎使用的是,给我我想要的东西时,找到一种替代。 AspCompatibility模式属性不在此模式下工作,要么。

我想InThreadScope可能会奏效,但比它在执行一个单独的线程创建的服务。

因此,基本上我需要的HttpContext对WCF等效+是范围我在请求级别的对象。有没有在这个世界上的一些静态对象,将相同的方式工作或没有任何人有什么我可以一起砍什么想法吗?

有帮助吗?

解决方案

我实现我自己的WCF扩展Ninject 2.0之前,我知道有一个这个向上在github。我的实现略有不同,但我没有拿出一个解决方案为范围对象:

using System;
using Ninject.Activation;

namespace Ninject.Contrib.Wcf {
  /// <summary>
  /// Defines Scope Callbacks for WCF Context.
  /// </summary>
  public class NinjectWcfScopeCallbacks {
    /// <summary>
    /// Defines WCF Context scope.
    /// </summary>
    public static readonly Func<IContext, object> WcfContext =
      ctx => (System.ServiceModel.OperationContext.Current != null
                ? System.ServiceModel.OperationContext.Current.
                    InstanceContext.
                    Extensions.Find<NinjectInstanceContext>()
                : null);

    /// <summary>
    /// Defines WCF Web Context scope.
    /// </summary>
    public static readonly Func<IContext, object> WcfWebContext = 
               ctx => System.ServiceModel.Web.WebOperationContext.Current;
  }
}

有关完整性,这是我如何使用上面所定义的回调:

Bind<IHelloWorldService>()
        .To<HelloWorldService>()
        .InScope(NinjectWcfScopeCallbacks.WcfWebContext);

在没有托管WCF服务WAS,所以不知道你是否会使用上面定义的WcfWebContextWcfContext,但你可以尝试“时间出去看看。如果WebOperationContext工作,那么你所有的设置。否则,我发现事情有点复杂。你会注意到上面的代码段使用的是连接到一个NinjectInstanceContextOperationContext。这是一类我写了使用Ninject 2.0的“缓存,并收集”机制,允许确定性配置对象。基本上,类是工具IExtension<InstanceContext>其是WCF构造用于附着几乎任何给OperationContext。此类还实现Ninject的INotifyWhenDisposed接口这正是提供了确定性的处置支持。下面是类的定义是什么样子:

  /// <summary>
  /// Defines a custom WCF InstanceContext extension that resolves service instances
  /// using Ninject.  
  /// <remarks>
  /// The custom InstanceContext extension provides support for deterministic disposal
  /// of injected dependencies and service instances themselves by being hook into 
  /// Ninject's "cache and collect" mechanism (new in Ninject 2.0) for object life cycle 
  /// management.  This allows binding object instances to the lifetime of a WCF context
  /// and having them deterministically deactivated and disposed.
  /// </remarks>
  /// </summary>
  public class NinjectInstanceContext : 
                IExtension<InstanceContext>, INotifyWhenDisposed {
  }

我的WCF扩展Ninject的其余部分一样的一个在GitHub上。这些操作的基本是创建一个实例提供其插入到WCF“激活”链 - 我不使用他们的专用术语,只是我明白的事情。所以,这个想法是,你的实例提供应该提供的被请求的WCF服务类实例。所以,这里我们使用Ninject生产服务实例。通过这样做,我们也可以激活并注入的依赖。什么情况下提供程序在我的实现是收官Ninject内核的实例,如果NinjectInstanceContext并将其连接到OperationContext。那么服务的创建委托给此WCF扩展。当实例提供商被告知以释放的服务,这是连接到的OperationContext的NinjectInstanceContext设置其通过实施INotifyWhenDisposed的方式导致确定性处置服务的(和潜在的依赖关系)。

希望此讨论有助于。我去看看,如果我可以在这里发布,如果你有兴趣一些更具体的代码。

其他提示

我敢肯定OperationContext是你在找什么

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top