Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF Service problem using Ninject (Moving from Windsor) (InvalidOperationException)
    text
    copied!<p>Any Ninject experts in the house? :) I've recently been trying to convert my WCF Service Application and Windows Forms Client Application from Castle Windsor dependency injection to Ninject.</p> <p>All has gone fine on the Win Forms side, but I am encountering issues on the WCF side of things. I have learned so far I believe I need to WCF extensions available for Ninject in order to use DI with WCF which I have done and referenced but still experiencing an issue I believe when my service is attempted to be resolved:</p> <p><strong>System.InvalidOperationException: The type 'WcfMemberService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.</strong></p> <p>The code I have which is what I thought to be correct to access just for instance my WcfMemberService is as follows:</p> <p><strong>ServiceModule.cs:</strong></p> <pre><code>public class ServiceModule : NinjectModule { private IKernel _parentContainer; public ServiceModule(IKernel container) { this._parentContainer = container; } public override void Load() { Bind&lt;IDataContextProvider&gt;().To&lt;DataContextProvider&gt;() .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); Bind(typeof(IRepository&lt;&gt;)).To(typeof(Repository&lt;&gt;)); Bind&lt;IServiceLocator&gt;().ToConstant(new NinjectServiceLocator(_parentContainer)); Bind&lt;IUserService&gt;().To&lt;UserService&gt;(); // ** WCF Services ** Bind&lt;Business.Common.Wcf.Services.Contracts.IMemberServiceContract&gt;().To&lt;Business.Common.Wcf.Services.MemberService&gt;().InSingletonScope().Named("WcfMemberService"); } } </code></pre> <p>The assumption I made is when converting over from my working Castle Windsor configuration is that <code>Named()</code> should be the same entry which is featured in the declaration of your WCF <code>.svc</code> file. So I have done that as follows:</p> <pre><code>&lt;%@ ServiceHost Language="C#" Service="WcfMemberService" Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" %&gt; </code></pre> <p>Pretty simple, I have taken this approach from the TimeService example solution featured on the Ninject Wcf Extensions GitHub page <a href="http://github.com/ninject/ninject.extensions.wcf/" rel="nofollow">here</a> by the way.</p> <p>Can anyone see what I have done wrong here and why "WcfMemberService" would not be resolving? It is bound to be "WcfMemberService" in the Kernel and referenced in the @ServiceHost declaration. I can't see what else could be wrong. This is exactly the same as how I declare it in Castle Windsor, except for roughly different syntax, but both use <code>Named()</code> and both ref that name in the Service part of the <code>.svc</code> file.</p> <p>*<strong><em>Update</em>*</strong> I have discovered if I do not use the <code>Named()</code> approach, and simply set my service in <code>@ ServiceHost</code> declaration as <code>Business.Common.Wcf.Services.MemberService, Business.Common.Wcf.Services</code> it works. But I am still stumped as to why I can't use Named services. Thanks.</p> <p>By the way, my Ninject module is loaded via <code>Global.asax.cs</code> in this fashion:</p> <pre><code>public class Global : NinjectWcfApplication { #region Overrides of NinjectWcfApplication /// &lt;summary&gt; /// Creates the kernel that will manage your application. /// &lt;/summary&gt; /// &lt;returns&gt;The created kernel.&lt;/returns&gt; protected override IKernel CreateKernel() { // config to reside in executing directory log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config")); var _container = new StandardKernel(); _container.Load(new ServiceModule(_container)); //_container.Load(new Log4netModule()); ServiceLocator.SetLocatorProvider(() =&gt; _container.Get&lt;IServiceLocator&gt;()); return _container; } #endregion } </code></pre>
 

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