WCF – Duplex


The November 2005 CTP release of WinFX shows that Microsoft is moving towards a cleaner interface for WCF. If you’ve got any of the Indigo programming books (e.g. Programming “Indigo”) then you should be aware that certain classes/parameters/names/enum’s have changed (e.g. ServiceSite, bindingSectionName, ServiceHost etc). What follows is a simple (very) sample of using duplex channels over HTTP and TCP.

WCF does make life every easy to write communicating applications. However, I’m still left with the question of will WCF attain the performance needed to be truely useful in a front-end trading application with real-time price feeds? – WCF really needs to exceed .NET Remotings performance!

What follows is very simple source code for a Quote server – what else would you expect from a trading desk 🙂

using System; using System.ServiceModel; namespace IndigoLib { [ServiceContract(CallbackContract=typeof(IQuoteCallback))] public interface IQuoteContract { [OperationContract] void GetQuote(string stock); } public interface IQuoteCallback { [OperationContract(IsOneWay=false)] void QuoteChanged(string stock, double value); } }
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <service type="IndigoServer.QuoteService"> <endpoint address="http://localhost:8000/service" binding="wsDualHttpBinding" contract="IndigoLib.IQuoteContract" /> <endpoint address="net.tcp://localhost:8080/service" binding="netTcpBinding" contract="IndigoLib.IQuoteContract" /> </service> </services> </system.serviceModel> </configuration>
using System; using System.ServiceModel; using IndigoLib; using System.Threading; namespace IndigoServer { [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] class QuoteService : IQuoteContract { IQuoteCallback callback; private Thread t = null; private ThreadStart ts = null; private string stock; public QuoteService() { callback = OperationContext.Current.GetCallbackChannel<IQuoteCallback>(); ts = new ThreadStart(DoWork); t = new Thread(ts); t.Start(); } public void GetQuote(string stock) { this.stock = stock; Console.WriteLine("Called :" + stock); } public void DoWork() { Thread.Sleep(1000); callback.QuoteChanged("Callback " + stock, 12); } } class Program { static void Main(string[] args) { using (ServiceHost serviceHost = new ServiceHost(typeof(QuoteService))) { serviceHost.Open(); Console.WriteLine("Service Running - Press Any key to quit"); Console.ReadLine(); serviceHost.Close(); } } } }

Source code coloured by Get Colouring! (via Brad’s blog).

Client code to follow shortly.

~ by mdavey on November 28, 2005.

3 Responses to “WCF – Duplex”

  1. […] Here’s the code for a simplistic client to talk TCP and HTTP to the previously blogged Quote Server. […]

  2. Thanks for this. With the latest WCF note a couple of code changes:
    ChannelFactory -> DuplexChannelFactory
    CreateDuplexChannel -> CreateChannel

    Cheers,
    -Mat

  3. \”We can\’t solve problems by using the same kind of thinking we used when we created them.\” – Albert Einstein

    Well said great fellow!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.