English 中文(简体)
利用Ninject WCF延伸到WCF网络服务
原标题:Using Ninject WCF Extension with WCF Web Service

我有一个WCF网络服务,我想利用我想要依赖的我的存放和服务,向我的WCF网络服务注入,然而,Ninject WCF推广范例相当多,它有一位导师,正在对每个依赖性进行即时调查,我不想这样做,我想说的是纯粹依赖性注射。

谷歌在利用Ninject和WCF方面取得了任何成功,似乎对我所研究的专题来说,没有什么相关结果。

最佳回答
问题回答

我目前使用Ninject(v3)和我的WCF的方法是以Ninject WCF延伸和

简言之,这里是我做的事情:

1) Via NuGet, I ve supplemented a reference to Ninject. 延期。 WCF项目。 由此产生了NinjectWebCommon.cs的“Start”双倍,后者负责启动Ninject。

2) 典型的情况是,你在NinjectWebCommon.cs的创建Kernel方法中建立了你的Ninject绘图。 然而,由于我有一个MVC3站点,处于同一解决方案之中,并且希望该场址有相同的Ninject绘图,我的创建者认为:

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        InfrastructureSetup.RegisterServices(kernel);
        return kernel;
    }

3) 基础设施。 登记册服务,我有我的Ninject绘图:

public static class InfrastructureSetup
{
    public static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IRepositoryContext>().To<MyEntityFrameworkContext>().InRequestScope();
        kernel.Bind<IFooRepository>().To<FooRepository>().InRequestScope();
        kernel.Bind<IBarRepository>().To<BarRepository>().InRequestScope();

        // ... and so on. I want InRequestScope() for the EF context, since
        // otherwise my repositories (which take IRepositoryContext in their 
        // constructors) end up getting different EF contexts, messing things up
    }
}

4) 我也要向我的工联建筑商注入 st(IFooservice等),因此,我编辑了WCF项目的会议,听取Pieter De Rycke的建议:

    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
                <!-- Add the Ninject behavior to the WCF service. This is needed to support dependency injection to the WCF constructors -->
                <ninject />
            </behavior>
        </serviceBehaviors>
    </behaviors>
  <extensions>
    <behaviorExtensions>
      <!-- Add the Ninject behavior extension -->
      <add name="ninject"
        type="MyWCFProject.Infrastructure.NinjectBehaviorExtensionElement, MyWCFProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
  </extensions>

5) 在MyWCF项目中。 基础设施名称空间,我有3个基本上由Pieter复制的档案:

NinjectBehaviorAttribute.cs:

using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Ninject.Web.Common;

namespace MyWCFProject.Infrastructure
{
public class NinjectBehaviorAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
        Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {   
        Type serviceType = serviceDescription.ServiceType;

        // Set up Ninject to support injecting to WCF constructors
        var kernel = new Bootstrapper().Kernel;
        IInstanceProvider instanceProvider = new NinjectInstanceProvider(kernel, serviceType);

        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
            {
                DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
                dispatchRuntime.InstanceProvider = instanceProvider;
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
}
}

NinjectBehavior ExtensionElement.cs:

using System;
using System.ServiceModel.Configuration;

namespace MyWCFProject.Infrastructure
{
    public class NinjectBehaviorExtensionElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(NinjectBehaviorAttribute); }
        }

        protected override object CreateBehavior()
        {
            return new NinjectBehaviorAttribute();
        }
    }
}

NinjectInstanceProvider.cs:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Ninject;

namespace MyWCFProject.Infrastructure
{
    public class NinjectInstanceProvider : IInstanceProvider
    {
        private Type serviceType;
        private IKernel kernel;

        public NinjectInstanceProvider(IKernel kernel, Type serviceType)
        {
            this.kernel = kernel;
            this.serviceType = serviceType;
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return this.GetInstance(instanceContext, null);
        }

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return kernel.Get(this.serviceType);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }
}

目前,这一解决办法似乎运作良好;依赖注射正在南欧论坛和MVC3站点工作,我可以要求向南欧论坛建筑商提供附属条件,并在申请期间保持欧阵环境。





相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签