Archive

Posts Tagged ‘ninject’

Ninject & WCF

October 5th, 2009

Ninject relies on an HttpModule when instantiating the Kernel with OnePerRequestBehavior. WCF hosting infrastructure intercepts requests when the PostAuthenticateRequest is raised and doesn’t return processing to the ASP.NET HTTP pipeline. Ninject’s OnePerRequestModule only registers an event handler on the EndRequest event and thus isn’t ran on requests to a WCF service.

A quick fix for this is to enable AspNetCompatibilityMode on the WCF hosting infrastructure. In your web.config, add the following tag under your <system.serviceModel> declaration:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

You also have to decorate your .svc class with the following attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

I don’t know if this is an ideal solution however, and if it has any negative effects on performance etc. More elegant solutions seem to be available. More information on AspNetCompatibilityMode is available on MSDN.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Technology ,