Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a WCF Service with an HTTP Context?

Tags:

wcf

I need to debug a WCF service but it needs to have an HTTP Context.

Currently I have a solution with a WCF service web site, when I click on debug it starts up and then fires up an html page that contains no test form.

While the project is running I tried starting the wcftestclient manually, then provided the address of my service, it finds the service but when I invoke it, it bypasses the IIS layer (or development server), so the httpContext is null...

What is the correct way to debug a WCF service through an IIS context?

like image 941
JL. Avatar asked Oct 05 '09 12:10

JL.


2 Answers

In WCF, the HttpContext is set to NULL by default and by design, even if the WCF service is hosted in IIS; after all, WCF is not ASP.NET.

If you actually do need an HttpContext, you need to turn it on separately, through config (web.config if you host in IIS, your self-host app's app.config otherwise):

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

and you need to specify that fact (that your service allows or even expects the ASP.NET compatibility mode) by putting this attribute on your service class (that implements the service contract):

[AspNetCompatibilityRequirements
(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]    
public class MyWCFService : IMyWCFService
{
  ......
}  

RequirementsMode=Allowed just simply allows the ASP.NET compatibility mode, while RequirementsMode=Required actually requires is and will not work without it.

Once you do that, you should get your HttpContext.Current when you attach your debugger to the IIS worker process.

Marc

like image 117
marc_s Avatar answered Oct 04 '22 18:10

marc_s


You will have to attach your debugger (Visual Studio) to the IIS service process.

In Visual Studio, go to Debug -> Attach to process and select the IIS process in the Attach to Process dialog.

On IIS7, the name of the process is w3wp.exe, but you may need to select the Show processes from all users or Show process in all sessions before it becomes available.

When the debugger is properly attached to the IIS process, you can set one or more breakpoints in your code and invoke the service.

like image 26
Mark Seemann Avatar answered Oct 04 '22 19:10

Mark Seemann