Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I am currently using Ninject (v3) with my WCF is based on the Ninject WCF extension and <a href="http://pieterderycke.wordpress.com/2011/05/09/using-an-ioc-container-to-create-wcf-service-instances/" rel="nofollow">Pieter De Rycke's great blog post</a>.</p> <p>In a nutshell, here's what I'm doing:</p> <p>1) Via NuGet, I've added a reference to Ninject.Extensions.Wcf into my WCF project. This creates the App_Start folder with NinjectWebCommon.cs, which takes care of initializing Ninject.</p> <p>2) Typically, you'd set up your Ninject mappings in the CreateKernel method in NinjectWebCommon.cs. However, since I have a MVC3 site in the same solution and want the same Ninject mappings for that site, my CreateKernel looks like this:</p> <pre><code> private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel); kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;(); InfrastructureSetup.RegisterServices(kernel); return kernel; } </code></pre> <p>3) In InfrastructureSetup.RegisterServices, I have my Ninject mappings:</p> <pre><code>public static class InfrastructureSetup { public static void RegisterServices(IKernel kernel) { kernel.Bind&lt;IRepositoryContext&gt;().To&lt;MyEntityFrameworkContext&gt;().InRequestScope(); kernel.Bind&lt;IFooRepository&gt;().To&lt;FooRepository&gt;().InRequestScope(); kernel.Bind&lt;IBarRepository&gt;().To&lt;BarRepository&gt;().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 } } </code></pre> <p>4) I want to also inject stuff (IFooService etc.) to my WCF constructors, so I've edited the Web.config for the WCF project with the advice from Pieter De Rycke:</p> <pre><code> &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name=""&gt; &lt;serviceMetadata httpGetEnabled="true" /&gt; &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt; &lt;!-- Add the Ninject behavior to the WCF service. This is needed to support dependency injection to the WCF constructors --&gt; &lt;ninject /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;extensions&gt; &lt;behaviorExtensions&gt; &lt;!-- Add the Ninject behavior extension --&gt; &lt;add name="ninject" type="MyWCFProject.Infrastructure.NinjectBehaviorExtensionElement, MyWCFProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt; &lt;/behaviorExtensions&gt; &lt;/extensions&gt; </code></pre> <p>5) In the MyWCFProject.Infrastructure namespace, I have three files which are basically copy-paste from Pieter:</p> <p>NinjectBehaviorAttribute.cs:</p> <pre><code>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&lt;ServiceEndpoint&gt; 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) { } } } </code></pre> <p>NinjectBehaviorExtensionElement.cs:</p> <pre><code>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(); } } } </code></pre> <p>NinjectInstanceProvider.cs:</p> <pre><code>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) { } } } </code></pre> <p>At the moment, this solution seems to be working well; dependency injection is working for both the WCF and the MVC3 site, I can request dependencies to be injected to the WCF constructors and the EF context stays around for the duration of the request. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload