Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreRequestHandlerExecute event not fired for REST calls

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

like image 895
andrecarlucci Avatar asked Feb 28 '23 12:02

andrecarlucci


1 Answers

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

like image 174
andrecarlucci Avatar answered Mar 09 '23 00:03

andrecarlucci