2014-02-21 35 views
0

我已經開始從NinjectWcfApplication繼承的應用程序(從Ninject 2)照顧,我想將它升級到Ninject 3.隨着Ninject 3 ServiceSecurityContext.Current是服務建設期間空

應用注入電流的WindowsIdentity當它由NinjectServiceHostFactory構造時,將其轉換爲服務對象。

這是通過將WindowsIdentity綁定到對ServiceSecurityContext.Current.WindowsIdentity的調用完成的。

Bind<WindowsIdentity>().ToMethod(c => ServiceSecurityContext.Current.WindowsIdentity); 

隨着Ninject 2本工作得很好,但隨着Ninject 3 ServiceSecurityContext.Current爲空,導致施工服務對象的過程中的異常。

從異常調用堆棧是:

[NullReferenceException: Object reference not set to an instance of an object.] 
    MyApp.Web.Service.WebServiceConfigurationModule.<Load>b__0(IContext c) in e:\Work\Technical\MyApp\MyApp.Web.Service\Global.asax.cs:29 
    Ninject.Activation.Provider`1.Create(IContext context) +35 
    Ninject.Activation.Context.Resolve() +293 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +247 
    System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +299 
    System.Linq.WhereSelectArrayIterator`2.MoveNext() +81 
    System.Linq.Buffer`1..ctor(IEnumerable`1 source) +519 
    System.Linq.Enumerable.ToArray(IEnumerable`1 source) +102 
    Ninject.Activation.Providers.StandardProvider.Create(IContext context) +778 
    Ninject.Activation.Context.Resolve() +293 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +247 
    System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +299 
    System.Linq.WhereSelectArrayIterator`2.MoveNext() +81 
    System.Linq.Buffer`1..ctor(IEnumerable`1 source) +519 
    System.Linq.Enumerable.ToArray(IEnumerable`1 source) +102 
    Ninject.Activation.Providers.StandardProvider.Create(IContext context) +778 
    Ninject.Activation.Context.Resolve() +293 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +247 
    System.Linq.Enumerable.Single(IEnumerable`1 source) +281 
    Ninject.Extensions.Wcf.BaseNinjectServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +224 
    System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +577 
    System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1450 
    System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +75 
    System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +900 

[ServiceActivationException: The service '/MyAppService/MyAppService.svc' cannot be activated due to an exception during compilation. The exception message is: Object reference not set to an instance of an object..] 
    System.Runtime.AsyncResult.End(IAsyncResult result) +654324 
    System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +210877 
    System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +282 

編輯:現在縮小的問題,我有一個更好的理解這是怎麼回事的。

回答

0

我還沒有找到在服務建設期間獲取當前WindowsIdentity的方法。

但在我的情況下,我實際上並不需要身份,直到後來在請求處理中,所以我能夠解決該問題,而不是在需要時提供WindowsIdentity上的屬性的服務中注入類(基本上通過使用上面綁定的方法)。