tags:

views:

716

answers:

5

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?

A: 

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.

Mark Seemann
What is the name of the process?
JL
@JL: Edited my answer to include the name of the process.
Mark Seemann
Unfortunately does not work, breakpoint is simply not met.
JL
It seems to me HTTPContext is not meant to be used in WCF, am I right?
JL
Update: breakpoint met, but httpContext is always null...
JL
A: 

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).

Philippe
Doesn't work. Breakpoint simply not met, using WcfTestClient alone has no http context. I need a way to test with a valid httpContext.
JL
Correction, break point is met, but httpContext is always null
JL
+6  A: 

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

marc_s
This config to the web.config for the iis application?
JL
yes, it's where your other WCF config resides - in the system.serviceModel section. web.config in IIS, or app.config of your self-hosting app.
marc_s
now getting this error: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.
JL
finally, thanks its sorted out.
JL
A: 

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

and

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

Omer Khayyam
A: 

IN VB.NET and studio 2010 it is this:

Public Class myHelloWorld Implements ImyHelloWorld

Doug Lubey of Louisiana
This should have been a comment. I recommend you read Faq first. http://stackoverflow.com/faq
Ismail