I was trying to create a simple project based on WCF where a service can notify clients about something (as oposed to the standard scenarios where a client asks a service to perform some operation). The simplest way to do so is to use callbacks. I have made up a very simple solution to demonstrate this concept. A sample solution has two interfaces: one for the usual service contract and another one for the client callback.
[ServiceContract(CallbackContract = typeof(IContractCallback))]
public interface IContract
{
[OperationContract]
void Foo();
}
[ServiceContract]
public interface IContractCallback
{
[OperationContract]
void OnFooCallback();
}
A very basic implementation of these contract can look like this
internal class WcfService : IContract
{
public void Foo()
{
//Do work...
var callback = OperationContext.Current
.GetCallbackChannel<IContractCallback>();
callback.OnFooCallback();
}
}
internal class ContractCallback : IContractCallback
{
public void OnFooCallback()
{
Console.WriteLine("...");
}
}
This is how this small app looks


Using the callback contract can be straight-forward, however there are some limitations:
- Only NetTcpBinding, NetNamedPipeBinding and WSDualHttpBinding bindings are supported
- One callback contract per service contract is allowed
- Client must keep the connection open the whole time
- Service must use reentrant or multiple concurrency mode
This project can be downloaded using the link below or from the github.
CallbackService.zip (17.49 kb) [Downloads: 376]

If you enjoyed this post, make sure you subscribe to my RSS feed!
c5eea1dd-b34c-4515-b2cc-a001f7d31e6a|0|.0
Any service oriented architecture, including WCF, follows a convention where a client implements a proxy pattern. Proxy allows the client to abstract from the concrete implementations and locations of the service. Client often creates stubs that work well in the compile time, whereas in runtime stubs are resolved and actual calls are made to the service.
Closing WCF client
A client has an inherited responsibility of gracefully closing the connection. It is always recommended to close the proxy client. If the binding between a client and a service is transport-layer sessionful, then closing a proxy is essential to tear down the connection between both parties. Service has a payload threshold defined for concurrent connections. If the number of concurrent connections goes above this threshold linearly then the overall service performance exponentially decreases. This is why it is crucial to dispose of the connection as soon as possible. Closing the proxy also notifies the service instance that it is no longer in use and may be collected by GC. If the client does not close a connection, it is still automatically torn down by WCF timeouts (found in the configuration files).
Aborting WCF client
In the situation where there is a fault in the service-client interaction, the objects on both ends are potentially totally broken. Thus using a proxy after the exception is not advised. Given the WCF binding use transport sessions, the client after a fault would not even be able to close it (if there was no transport layer session then the client could use or close the proxy, but this is not recommended as the configuration of sessions could change). So after a fault has happened the only safe operation is to abort a proxy.
Coding
Close (and Abort on fault) needs to be called for all the client calls, therefore these methods can be moved to a proxy itself
class Client1 : ClientBase<IContract1>, IContract1
{
public void DoWork()
{
try
{
Channel.DoWork();
Close();
}
catch
{
Abort();
throw;
}
}
}
If you enjoyed this post, make sure you subscribe to my RSS feed!
cc69a51c-4367-4251-aae4-04ca22863759|0|.0
It is well known that WCF service can be hosted as a self-hosted application, IIS web service or a Windows service. This post is dedicated to the Windows service hosting and it goes through some advanced techniques on how to host many WCF services in one windows service.
So, here I have got three projects: multiple WCF services project, a client and a Windows service installer. Let us start with the WCF services. This project has three service interfaces and correspondently three service implementations.

Sample interface looks like this:
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoAction1();
}
And its implementation is very simple
public class Service1 : WindowsManagedService, IService1
{
public Service1() : base(typeof(Service1)) { }
public string DoAction1()
{
return "Action 1";
}
}
These are the interfaces in WCF service project

Class diagram for the services that implement those interfaces

You must have noticed that there are a few other types like IWindowsManagedService and WindowsManagedService - this is basically a bridge that allows WCF services to be hosted within Windows service.
public interface IWindowsManagedService
{
void Start();
void Stop();
}
public abstract class WindowsManagedService : IWindowsManagedService
{
private ServiceHost serviceHost;
protected WindowsManagedService(Type serviceType)
{
serviceHost = new ServiceHost(serviceType);
}
public void Start()
{
serviceHost.Open();
}
public void Stop()
{
serviceHost.Close();
}
}
Next, let’s take a quick look into the host project. This project contains a configuration file for the WCF services. It also has one installer class which is picked up by installutil. But the main type here is WindowsManagedServices which uses reflection to find WCF services that can be hosted in one Windows service
public class WindowsManagedServices : ServiceBase
{
private List<IWindowsManagedService> services;
public WindowsManagedServices()
{
services = new List<IWindowsManagedService>();
var assembly = Assembly.GetAssembly(typeof(IWindowsManagedService));
var types = assembly.GetTypes();
foreach (var type in types)
{
if (!type.IsAbstract && typeof(IWindowsManagedService).IsAssignableFrom(type))
{
var serviceObject = (IWindowsManagedService)Activator.CreateInstance(type);
services.Add(serviceObject);
}
}
}
protected override void OnStart(string[] args)
{
foreach (var service in services)
{
service.Start();
}
}
protected override void OnStop()
{
foreach (var service in services)
{
service.Stop();
}
}
}
Then, I used installutil to install the windows service and here is what I end up with
- 1 Windows Service WCFServices
- 3 WCF services
http://localhost:8083/WCFServices/IService1
http://localhost:8083/WCFServices/IService2
http://localhost:8083/WCFServices/IService3
Lastly, a sample client to this application
class Program
{
static void Main(string[] args)
{
IService1 client1 = new Service1Client();
IService2 client2 = new Service2Client();
IService3 client3 = new Service3Client();
Console.WriteLine("Client 1 says: " + client1.DoAction1());
Console.WriteLine("Client 2 says: " + client2.DoAction2());
Console.WriteLine("Client 3 says: " + client3.DoAction3());
Console.ReadLine();
}
}

The whole project can be downloaded using the link below. Unzip it, run the solution file and hit F5 in Visual Studio 2010. It should start two projects: services and the client.
WCFServiceHosting.zip (46.48 kb) [Downloads: 468] [UPDATED after discovery of the client's port mismatch)

If you enjoyed this post, make sure you subscribe to my RSS feed!
3f9c2985-f063-447f-84ab-8261ee842cac|0|.0
I have been looking for some command query responsibility segregation videos and found a brilliant bloger - Udi Dahan. He talks about messaging, services oriented architecture and enterprise development. So if you are interested in that, that's the guy to follow.
If you enjoyed this post, make sure you subscribe to my RSS feed!
a584f1af-76ac-4c90-a626-b4b77875003e|0|.0