views:

372

answers:

1

I'm creating my nhibernate session in the PreRequestHandlerExecute event handler of the HttpApplication class.

It works fine for MVC, however, when in WCF (REST) the event is never fired.

Is there a way to make it happen or any other better idea to set the session both in MVC and WCF/Rest?

Thanks in advance,

André Carlucci

+2  A: 

I got it!

I found the answer in this article:

http://blogs.msdn.com/wenlong/archive/2006/01/23/516041.aspx

When using WCF in the mixed mode, the module intercepts the request in the early stage of the pipeline: BeginRequest. That means the other events are never called.

To fix that, I changed my web.config to make WCF work in the Asp.Net compatibility mode:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

And then explicitely tell my service to be compatible too:

[AspNetCompatibilityRequirements(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService { ...

And done! Now I have all events and also the HttpContext.Current instead of OperationContext.Current

I hope this helps someone with the same problem.

Cheers,

André Carlucci

andrecarlucci
Of course, the alternative would have been to do the same thing the "WCF way". There's a cost to being ASP.NET compatible.
John Saunders
Hi John, how is the WCF way to open hibernate session and put it in the OperationContext? I mean, I just have the BeginRequest/EndRequest events and no access to OperationContext there :(
andrecarlucci