JL.
JL.

Reputation: 81292

How to debug a WCF Service with an HTTP Context?

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?

Upvotes: 6

Views: 4686

Answers (4)

Omer Khayyam
Omer Khayyam

Reputation: 11

Thanks for the Solutions. I was getting the same problem. My Solution is Working fine now with 2 svc files.

In Order to solve the problem i made two changes

In Web.Config I Commented the Line

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

and

Added Attribute [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] on the classs that are implementing the Interfaces.

Upvotes: 1

marc_s
marc_s

Reputation: 754568

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

Upvotes: 9

Philippe
Philippe

Reputation: 4051

You must attach to the IIS process, namely aspnet_wp.exe under XP and w3wp.exe on 2003 server. This way you will hit breakpoints etc.

If you are looking for a way to test the WCF service itself, I would suggest using WcfTestClient.

And remember that the IIS process won't show in the task manager until you hit the server at least once (for example after a reboot, you'll have to hit a page on the server at least once to make the process start).

Upvotes: 1

Mark Seemann
Mark Seemann

Reputation: 233150

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.

Upvotes: 1

Related Questions