使用Ninject 3 ServiceSecurityContext.Current在服务构造期间为null

时间:2014-02-21 22:19:38

标签: c# wcf ninject

我已经开始寻找从NinjectWcfApplication(来自Ninject 2)继承的应用程序,并且我想将它升级到Ninject 3。

当应用程序由NinjectServiceHostFactory构造时,应用程序将当前的WindowsIdentity注入服务对象。

这是通过将WindowsIdentity绑定到对ServiceSecurityContext.Current.WindowsIdentity的调用来完成的。

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

使用Ninject 2可以正常工作,但使用Ninject 3 ServiceSecurityContext.Current为null会导致在构造服务对象期间出现异常。

异常的调用堆栈是:

[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

编辑:现在我已经更清楚地知道发生了什么,这个问题缩小了。

1 个答案:

答案 0 :(得分:0)

我没有找到在服务构建期间获取当前WindowsIdentity的方法。

但是在我的情况下,我实际上不需要身份直到请求处理的后期,所以我能够解决问题,而不是在需要时在WindowsIdentity上提供属性的服务中注入一个类(基本上通过使用上面绑定的方法。)