Click here to Skip to main content
6,593,923 members and growing! (12,403 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » General     Beginner License: The Code Project Open License (CPOL)

Debug a WCF service with Edit and continue support

By Sarafian

Debuging a WCF service through a provider class
C#, .NET, WCF, Dev
Posted:28 Jun 2008
Views:8,006
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.08 Rating: 3.60 out of 5

1
1 vote, 50.0%
2

3
1 vote, 50.0%
4

5

Introduction

WCF default debugging does not support edit and continue features, as I have said in my blog post

Background

In that post I said that there is an easy way to do it, but I didn't give any code. So this article is about how to it. The reason I wrote this article, is because a fellow member wrote a usefull article about wcf, and in tip1 he suggested using a console application.

My scenario for wcf service consuming and debugging is a Client Server application. I believe my solution can apply and for other scenarios.

How to do it

You need this class DebugServiceHosta

internal class DebugServiceHost:IDisposable
{
private static string serviceExecuteTypeName = "ServiceSide.ExecutionService.ServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
private static string iServiceExecuteTypeName = "ServiceSide.ExecutionService.IServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
public static string Address = @"%22http://localhost:9999/ExecutionService/ServiceExecute/%22">http://localhost:9999/ExecutionService/ServiceExecute/";
public static Binding Binding = new WSHttpBinding();

static DebugServiceHost()
{
isEnabled = Type.GetType(serviceExecuteTypeName) != null;
}
public DebugServiceHost()
{
Type serviceType = Type.GetType(serviceExecuteTypeName);
Type iServiceType = Type.GetType(iServiceExecuteTypeName);
this.debugHost = new ServiceHost(serviceType);
this.debugHost.AddServiceEndpoint(iServiceType, Binding, Address);
this.debugHost.Open();
}
private System.ServiceModel.ServiceHost debugHost;
private static bool isEnabled = false;
public static bool IsEnabled
{
get { return isEnabled; }
}
#region IDisposable Members
private bool disposed = false;
public void Dispose()
{
if (this.disposed)
{
return;
}
this.debugHost.Close();
GC.SuppressFinalize(this);
}
#endregion
}

where ServiceSide.ExecutionService.ServiceExecute is the namespace of the wcf service and ServiceSide.ExecutionService the assembly containing it.

Using the code

I use a provider class, but its up to you how to implement the above feature.

DebugServiceHost has a static property IsEnabled, which basically checks whether the service is locally located to the running client execution folder. If that is true then using an instance of the class, host the service as well, which is automatically disposed.

This a sample code from my project that consumed the true or debug hosted service.

public static class Provider
{
private static DebugServiceHost dbh;
public static void CheckAndRunDebugService()
{
if (DebugServiceHost.IsEnabled)
{
dbh = new DebugServiceHost();
}
}
public static string RemoteURL { get; set; }
private static IServiceExecute iServiceExecute = null;
private static IServiceExecute GetInterface()
{
if (iServiceExecute != null)
return iServiceExecute;
iServiceExecute = CreateClient();
return iServiceExecute;
}
private static ServiceExecuteClient CreateClient()
{
string address = "";
if (DebugServiceHost.IsEnabled)
{
address = DebugServiceHost.Address;
}
else
{
address = String.Format("{0}/Service.svc", Provider.RemoteURL);
}
return new ServiceExecuteReference.ServiceExecuteClient("DefaultEndPoint", address);
}
}

where

  • IServiceExecute is the Interface created when you add the service Reference.
  • ServiceExecuteReference.ServiceExecuteClient is the client created when you add the service reference
  • DefaultEndPoint is the endpoint in my config file.

Points of Interest

At first I at the first call to the service, I create the host. But as I mentioned in my post there was problem with that implementation.

Last problem hopefully
Finally a point of interest which was my last obstacle that took me a half day to find. I had implemented a provider class for the remoting part, which if needed, fired up the service host. Everything worked just great in the test projects, but at some point I tried to extend the framework with WPF.
The trick was that at first request, through static constructor I checked if the service was required to be hosted, and did that. But this did not work when I made the call from WPF. The only error was a timeout exception. I was going crazy, and then it kicked in. Never trust completely a 3 party library.
Solution
I made three clients, one Console, one Windows one WPF and stripped down the functionality of my framework to test. On each UI Client I made the call (and raised subsequently the host) after a UI reaction. I turns out that neither Windows Forms played correctly, when for example the call is made through a button click event. When I saw that, I made the host come up before the UI part was ever initialized and guess what? It all played just fine.
I really can't understand how this has not been mentioned.

So I refactored the code, and at the program main entry method I call Provider.CheckAndRunDebugService()

Final thing is that, you must implement a trick in order for all serverside assemblies to be found in the client execution folder when you are debugging. There are a number of ways to do achieve this mainly by using post build event or by just adding the references and deleting them when in deployment face. I use the first.

Apologies

There is not project code, because the above resides in a framework project, I have written for the company I work for.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sarafian


Member
Alex Sarafian lives in Athens Greece and currently is working for ORAMA Hellas developing Business scale application with .NET2

He has been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 began working for an R&D institute where he was introduced to C# which he worships ever since.

He has recently created a development blog http://sarafianalex.wordpress.com/

He loves component based programming and in his spare time he usualy "wastes" time in front of his media center pc watching sitcoms, preferable SCI-FI.
He wants to play chess but he can't find any real world players to hang out with.
Occupation: Software Developer (Senior)
Company: Orama Hellas
Location: Greece Greece

Other popular Windows Communication Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralPretty cool PinmvpSacha Barber21:04 28 Jun '08  
GeneralRe: Pretty cool PinmemberSarafian0:25 10 Aug '08  
GeneralRe: Pretty cool PinmvpSacha Barber6:33 10 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jun 2008
Editor:
Copyright 2008 by Sarafian
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project