2015-04-01 98 views
2

我有一個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

對於遇到類似問題的任何人。問題的根源在於服務有一個需要注入的CustomAuthorizationPolicy。這需要創建一個CustomServiceHostFactory。