Sergei Rogovtcev
Sergei Rogovtcev

Reputation: 5832

WCF request doesn't flow via asp.net pipeline

I have a WCF service hosted in an asp.net application.

Here's the service (shortened):

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceContract(Name = Name, Namespace = Namespace)]
[ServiceBehavior(Name = Name, Namespace = Namespace)]
public class WcfMaintenanceFacade {...}

Here's hosting:

RouteTable.Routes.Add(new ServiceRoute("entity/maintenance/5.20", new ServiceHostFactory(), typeof(WcfMaintenanceFacade)));

And here's relevant config section:

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

My service instantiates, the requests are coming in, and event HttpContext.Current is not empty.

There are two (major, for me) issues that I can't solve:

  1. HttpContext.Current.Session is empty
  2. Global.asax's Application_BeginRequest is never called

And yes, from the call stack it seems like the request is going through WCF activation pipeline, not ASP.net pipeline. So what am I doing wrong?

Upvotes: 2

Views: 875

Answers (3)

Sergei Rogovtcev
Sergei Rogovtcev

Reputation: 5832

And the answer is simple (and, well, obvious):

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">

Yes. RAMMFAR.

Upvotes: 1

Mimas
Mimas

Reputation: 523

For me it is clear that these two technologies are not supposed to be working in the same application, and that's why they have two different pipelines. Service by the nature is an isolated thing. Now you just try to find a workaround, relying on the fact they both work on same IIS.

I would recommend you rather start from the goals, from what you want to achieve. If you need new service-like functionality integrated into native ASP.NET application you can

1) use ASMX services (will give you SAOP if you need it) and/or page methods

2) try to integrate WEB API in case you need JSON service.

Upvotes: 0

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

  1. About the session, you handle it with OperationContext.Current.RequestContext instead of HttpContext.Current.Session.

HttpContext: Current is always null when accessed from within a WCF service. Use T:System.ServiceModel.OperationContext.Current.RequestContext instead.

Read more here: https://msdn.microsoft.com/en-us/library/aa702682.aspx

  1. Application_BeginRequest are used by ASP.Net applications, but WCF works different from common web applications, thus BeginRequest could not be hit on each request.

The ASP.NET HTTP runtime handles ASP.NET requests but does not participate in the processing of requests destined for WCF services .... he WCF Service Model intercepts messages addressed to WCF services and routes them through the WCF transport/channel stack

So, your problem can be related to this issue. This information is also available at the same link.

Hope it helps with your questions.

Upvotes: 4

Related Questions