![]() |
Platforms, Frameworks & Libraries »
Windows Communication Foundation »
General
Beginner
License: The Code Project Open License (CPOL)
Debug a WCF service with Edit and continue supportBy SarafianDebuging a WCF service through a provider class |
C#, .NET, WCF, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
WCF default debugging does not support edit and continue features, as I have said in my blog post
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.
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.
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 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.
There is not project code, because the above resides in a framework project, I have written for the company I work for.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |