使用Ninject的WCF无参数构造函数错误

时间:2015-04-01 20:08:48

标签: c# .net wcf ninject

我有一个WCF服务,我正在使用Ninject构造函数注入。

这是在.net 4.5框架

当我访问该服务时,我收到以下错误。

[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +66
   System.ServiceModel.Configuration.ServiceAuthorizationElement.CreateBehavior() +504
   System.ServiceModel.Description.ConfigLoader.LoadBehaviors(ServiceModelExtensionCollectionElement`1 behaviorElement, KeyedByTypeCollection`1 behaviors, Boolean commonBehaviors) +188
   System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress, Boolean skipHost) +12973997
   System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection) +69
   System.ServiceModel.ServiceHostBase.ApplyConfiguration() +178
   System.ServiceModel.ServiceHost.ApplyConfiguration() +61
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +184
   System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) +46
   Ninject.Extensions.Wcf.NinjectAbstractServiceHost`1..ctor(IServiceBehavior serviceBehavior, T instance, Uri[] baseAddresses) +208
   Ninject.Extensions.Wcf.NinjectIISHostingServiceHost`1..ctor(IServiceBehavior serviceBehavior, T instance, Uri[] baseAddresses) +43
   DynamicInjectoredd01103a277420e8359d2f089e3fee2(Object[] ) +199
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +884
   Ninject.Activation.Context.ResolveInternal(Object scope) +217
   Ninject.Activation.Context.Resolve() +276
   Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +157
   System.Linq.Enumerable.Single(IEnumerable`1 source) +121
   Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) +69
   Ninject.Extensions.Wcf.BaseNinjectServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +212
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +492
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1429
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +52
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +598

[ServiceActivationException: The service '/Authentication.svc' cannot be activated due to an exception during compilation.  The exception message is: No parameterless constructor defined for this object..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +486568
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +174
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +374230
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9708641

现在奇怪的部分就是这个。我在构造函数中设置了一个断点,当我调用该服务时,调用带有注入的构造函数并正确地注入所有内容然后返回。从非默认构造函数返回后发生此错误。

这是我的构造函数

    private IAuthenticationBl Bl;


    public Authentication(IAuthenticationBl AuthenticationBl)
    {
        Bl = AuthenticationBl;

    }

我已加入服务标记

Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

对于遇到类似问题的任何人。问题的根源是服务有一个需要注入的CustomAuthorizationPolicy。这需要创建一个CustomServiceHostFactory。