0

I have started looking after an application that inherits from NinjectWcfApplication (from Ninject 2) and I want to upgrade it to Ninject 3.

The application injects the current WindowsIdentity into the service object when it is constructed by the NinjectServiceHostFactory.

This is done by binding WindowsIdentity to a call to ServiceSecurityContext.Current.WindowsIdentity.

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

With Ninject 2 this works fine, but with Ninject 3 ServiceSecurityContext.Current is null resulting in an exception during construction of the service object.

The call stack from the exception is:

[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

EDIT: Narrowed down the question now that I have a better understanding of what's going on.

Dan
  • 7,446
  • 6
  • 32
  • 46

1 Answers1

0

I haven't found a way of getting at the current WindowsIdentity during service construction.

However in my case I don't actually need the identity until later in the request processing so I was able to get around the issue by instead injecting a class into the service that provides the properties on the WindowsIdentity when they are needed (basically by using the method that used to be bound above).

Dan
  • 7,446
  • 6
  • 32
  • 46