August 13, 2008

WCF Debugging in Visual Studio 2008

Debugging WCF Services is a lot easier in Visual Studio 2008. There is a WCF Service Host built-in to privately host WCF Services, as well as a built-in client that can be used as a test harness, and to view XML Requests and Responses. Here’s a quick walk-through on how to set up a quick sample WCF service, execute it using the client utility, and to create a simple console app that calls the service that will allow you to debug both client and server code without going through hosting the service in IIS, attaching to process, etc…
First, create a new WCF Service Project (File…New…Project -> Expand C#..WCF -> Select WCF Service Library)




The newly created project has a sample method GetData(int) that simply takes in an int, and returns a string. That’s the method we’ll be using. Now right-click the App.config file and select Edit WCF Configuration. This brings up a GUI view of the XML config file. Click the first “(Empty Name)” link that uses the binding wsHttpBinding.




This allows you to edit the Service Endpoint. Give it a name.




Save the configuration, now hit f5 to run the app. The WCF Test Client will launch. Double-click the GetData() method.




This brings up a test harness that will allow you to test the method. Enter a value for the Request Value and click Invoke. In a few seconds, the Response Value should display “You entered: [number]”.




OK, Now we’re ready to create a console client application that will call our service, and allow us to debug through client and server code. Add a new Console Application to the solution.




Right-click the newly created console app and select “Add Service Reference”. This brings up a dialog box that allows you to add a service reference. Click the down arrow next to the Discover button and select “Services in Solution”. Select your service in the dropdown and click Go, when it appears under Services, click OK.




Now go into the Main method of Program.cs in your console app. Add the following call to the referenced service:

ServiceReference1.Service1Client sc = new Service1Client();
Console.WriteLine(sc.GetData(11));

And set a breakpoint at Console.WriteLine.




Now you’re ready to start debugging. First run the service (F5), then start a new debugging instance of the console app. The debugger should now be stopped at your breakpoint in the console app. Step into the code (F11) and you will step through the GetData method in your service code.

When you finish stepping through your code, the console should display the return value.



A full Silverlight presentation of WCF Debugging is also available here.

Share |

3 comments:

  1. I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

    Sorry for offtopic

    ReplyDelete
  2. Who knows where to download XRumer 5.0 Palladium?
    Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

    ReplyDelete