How to debug a WCF Service with an HTTP Context? - 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?

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

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.

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

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.

Related

Auto-start appfabric seems to work sequentially

I have a couple of wcf services which will call a registration service at startup (through a custom ServiceHostFactory). All these services (including the registration service) are running in the same app pool. I installed appfabric to auto-start all the services. I won't explain all the detailed settings (because I think it's not necessary yet), but in short, the services use net.tcp bindings and is running in IIS 7.5 on Windows 7.
The problem:
When I set all services to auto-start it will begin with one (random) service, this service tries to connect to the registration service. But somehow this registration service won't start-up. It looks like it wants to start all services sequentially.
The (not wanted) solution 1:
When I put the registration service in it's own app pool everything works fine and alle services come online in a matter of seconds and register themself.
The (not wanted) solution 2:
When I disable the auto-start and try to access one of the services after a iisreset, it all works fine. The service will try to register itself and the registration service in his turn will start.
I'm new to appfabric, so it might be some simple configuration issue. The weird thing though is that it won't work whith auto-start, but seems to work fine with a manual start.
Any idea's?
There is no priority in AppFabric Auto Start feature. You can't assume that one service will always be started before the others. Also, you can't assume they will start all at the same time.
For the specific scenario, it's recommanded to use WCF 4.0 Discovery and Announcement. Announcement feature enables service to announce their availability (by sending Hello and Bye announcements) whereas Discovery feature allow clients to discover service address at runtime.
All your services are on the same server ? could be pertinent to use a namedPipeBinding.

Windows Services & WCF

I need to create a Windows Service to watch a folder on our network and action files that are placed within it. The process is quite slow and I need the ability to check the progress from a client application (which will be running in about 10 places on the same network as the machine running the Windows service).
Is hosting some WCF service in the windows service the right way to go about this and if so, are there any resources on how I would do this?
Thanks!
it seems a reasonable approach to me.
you can get details of how to host a WCF service inside a windows service in the MSDN how to
This code project page also has an example.
you might need to debug start up issues with the service, and I find adding a
Debugger.Launch();
to the beginning of the OnStart method is the easiest way of doing that. it enables you to debug through the start up process of your service and see any exceptions that occur.

WCF + SmtpClient: only works in a Windows 2008 environment

Continuing the discussion started in another post: I used windows service to host a WCF mail sender. The service itself is quite simple: it uses a DataContract to get the basic System.Net.Mail.MailMessage class properties (supports attachment - please see the mentioned post, code by edosoft) and the System.Net.Mail.SmtpClient class to actually send the e-mail. The .NET framework used was 3.5SP1 (with VS2008).
Please note that I already had the code working inside an assembly; the exercise was just to expose the functionality by using a WCF service. I used basicHttpBinding for the endpoint binding, no additional configurations.
When I tested the service in my desktop environment (Windows XP) I noticed the following:
The e-mails were only sent if I restarted the windows service in the first 10-15 seconds after calling the WCF service, otherwise nothing would happen;
No exception/error messages whatsoever in the service trace/log files;
No exception/error messages when calling the SmtpClient.Send(MailMessage msg) method or anywhere else when running the service code.
I played with different bindings and configurations for a while, even tried to send the message from a different thread - no changes. I actually came accross with a forum question related to this problem: the same method called from within an ASMX webservice would only work is an IISRESET was performed. I was able to replicate this issue by creating an ASMX webservice using the same sending code used in the WCF. Hosting the WCF service under IIS was also of no help (same problem).
I decided then to deploy the service in a Windows 2008 environment (windows service hosted) - see, no changes in the original, simple code. Same configuration, same everything. It worked right away, no delays or any other problem I could identify. Does anyone have any idea about this issue? I'll try it in a Windows 2003 environment now, but I'd like to know if it's actually a known bug/limitation, or if it's somehow related to the ASMX bug related (as it appears to be). Does anyone have seen it before?
Thanks,

WCF Service hosted in IIS - Can't seem to cache or retain state?

I'm trying to cache some application data that only needs to be instantiated when the application starts. I've tried using HttpRuntime.Cache, creating a static object that is instantiated only when the service starts, and I've tried making the service singleton and using global variables. Every time a new request hits the service I loose state... I could create the WCF service as a windows service I suppose, but I'd love to figure out what's happening here... I see that only one IIS worker process is spawning, but I'm guessing it's unloading and re-loading the service every time.
Am I missing some WCF configuration or possibly not setting it up right in IIS? It's running as a normal 2.0 website within IIS.
This my first post here, if someone can tell me how to post my app.config XML I will... I think stackoverflow is trying to parse it as HTML, it doesn't show up.
Thank you!
Tim
We use enterprise library caching with WCF services, works for us:
http://msdn.microsoft.com/en-us/library/dd203099.aspx
Edit
This answer is a bit old we have now stopped using Enterprise Library Caching, we use app fabric instead, see: http://msdn.microsoft.com/en-us/windowsserver/ee695849

WCF can no longer step into a service that's locally hosted -- why not?

I have a WCF test service and a test client in the same solution. The service is configured to run on localhost (Ie, "http://localhost:8731/Design_Time_Addresses/MyService/Service") I run the client app and it correctly invokes the service and gets back the correct answer. I've verified via logs that it's definitely running the service that's local to my machine.
However, I can't debug into the service when running the client. WCF is supposed to allow just stepping through, but no breakpoints on the service get hit, and stepping in to the service call doesn't work either. Has anyone ever seen this?
I've checked all the obvious stuff like "is the PDB file being generated?" -- yes, it is. If I run the service project by itself, then hit it with some test client, breakpoints get hit correctly. So debugging on the service works. I can even explicitly "attach to remote process" and debug the service that way. But WCF will not automatically step from the client to the service.
I've had this happen as well. Curiously it seems to happen after I make changes to the build configuration (from x64 to x86). Here's how I've been able to fix it:
Right-click the solution, select Properties.
Under "Startup Project" switch from "Single startup project" to "Multiple startup projects".
Set Action to "Start" for the WCF project.
Even though I'm hosting the project in the local IIS server (not the VS Development Server) this solves it, which is a little puzzling. I suspect that setting a project to "Start" also causes VS to attach to the process for debugging.
http://msdn.microsoft.com/en-us/library/bb157685.aspx
For Visual Studio multi-configurations:
In the Property Pages of your solution, ensure that the "Configuration" for your web-service is set for "Debug", not "Release".
Are you using the automatic WCF service hosting feature in Visual Studio? You can check this by opening the project properties of your WCF service project and navigating to the WCF Options tab. Is the Start WCF Service Host when debugging another project in the same solution checkbox checked? If it is, then this explains the problem.
What's happening is that when you run your client, a separate process is created to automatically host your WCF service. Because it is a separate process, you will not be able to "step into" it when debugging your client. You'll have to run two separate debug sessions, one for your service and one for your client.
An easy way to do this is to put a call to System.Diagnostics.Debugger.Break() in the startup logic for your service. When your service is automatically started by the WCF service host, you'll be prompted to debug it, allowing you to open a second instance of Visual Studio for its debug session.
Also when using VS debug and attaching it to the w3wp.exe process and you have specific port bindings other than http:80:* you must have the debug > web in VS set to the site and port http is running on.
Yes, I got the same problem, WCF uses another processs.
At last, I found a way, i start a new instance(debug->start a new instance) of WCF service first, then start the client side, the breakpoints in the WCF service hits!
One other reason for not stepping into WCF service may be the size of your data that is passed to the WCF methods.
For ex., I was passing an array of dobules of size 1000, and I was getting a bad request error (400) with seeing how the method is run. I made the size 500 and now it debugs the WCF.
In my case, I had published it using Release configuration. You need to select Debug here.
I had similar problem. WCF hosted by WCF Service Host, requests tested by WCF Test Client. My problem was caused by space in build configuration name. As soon as I used configuration name without spaces, debbuging worked.
I guess the real problem was the build output folder ending with folder named by the configuration name.
#msulis - yes, thanks, this remedied it for me. I had also changed recently, the target CPU from any to x86. I couldn't reply directly to you message for some reason so apologize if this comes up as an "answer".