Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Multicasting using UdpBinding in WCF 4.5

4.83/5 (12 votes)
10 Apr 2014CPOL4 min read 40.7K   1.1K  
This article describe about new UdpBinding in WCF 4.5 and implementing multicasting using the same.

Table of Contents

Introduction

WCF 4.5 came with a support of User Datagram Protocol (UDP). UDP is a connectionless, lightweight and unreliable protocol, and sometimes called "send and pray" because there is no dialog between the sender and receiver. If the receiver does not get a message, then the sender will never know about the same. But UDP is very efficient when there is little chance of errors. It is important to note that, UDP messages are not routed across the network boundaries and thus can only be used within a network. UDP is faster than TCP because there is no error-checking for packets. WCF 4.5 offers a new binding UdpBinding based on this protocol. As UDP supports multicasting, so now one can write multicasting applications using this binding. This transport is useful in cases where a sender needs to send out small messages to multiple receivers simultaneously without ensuring whether any specific message is actually received or lost. So here speed is more important than reliability.

UDP Endpoint in WCF 4.5

UdpBinding address begins with soap.udp:// (e.g. soap.udp://224.0.0.1:8081) and an UDP endpoint can be added in a service via app.config file in following manner-

ASP.NET
<endpoint address="soap.udp://180.18.84.116:8080/StockService/udp" binding="udpBinding" contract="StockServiceLibrary.IStockService" />

One can also add a UDP endpoint to a service programmatically. For this, reference of System.ServiceModel.Channels.dll assembly has to be added in the service hosting project and then UDP endpoint can be added like below-

C#
// Creating Host
ServiceHost host = new ServiceHost(typeof(StockService), new Uri("soap.udp://224.0.0.1:40000/StockService"));

// Adding service endpoint
host.AddServiceEndpoint(typeof(IStockService), new UdpBinding(), "udp");

Features of UdpBinding

  • Interoperability - .NET application. It can be achieved by implementing the standard SOAP-over-UDP specification which this binding implements.
  • Security - None
  • Session – None
  • Transactions – None
  • Duplex – NA
  • Encoding – Text
  • Streaming – No
  • Message - Request/Response and One Way
  • One-Way messages are true one-way unidirectional calls. No exception would be thrown by the client if the service isn't available

Limitations of UdpBinding

  • No IIS/WAS activation support as there is no UDP shared listener for WAS.
  • No UDP multicast support on Azure.

Multicasting Demo using UdpBinding

What is multicasting? Multicast is the term used to describe communication where a piece of information is sent from one or more points to a set of other points. In multicasting, sender >= 1 (i.e. there should be at least one sender) and receiver >= 0 (i.e. there may be no receivers).

For this demo, I’m going to create a simple stock ticker application using One-Way messages. I've defined a service contract and a data contract as below-

C#
[ServiceContract]
public interface IStockService
{
    [OperationContract(IsOneWay = true)]
    void SendStockDetail(Stock stock);
}

[DataContract]
public class Stock
{
    [DataMember]
    public string Name;

    [DataMember]
    public double Price;
}

Then I've implemented IStockService service contract in order to create StockService service as below-

C#
public class StockService : IStockService
{
    public void SendStockDetail(Stock stock)
    {
        Console.WriteLine(string.Format("Stock Name: {0}, Price: ${1}", stock.Name, stock.Price));
    }
}

Now each client (receiver) that wants to receive multicast messages must host StockService service by exposing IStockService interface. For this demo, I've created a console application to host the StockService (receiver) and exposed UDP endpoint through app.config file as below-

ASP.NET
<endpoint address="udp" binding="udpBinding" contract="StockServiceLibrary.IStockService" />
<host>
    <baseAddresses>
       <add baseaddress="soap.udp://224.1.1.1:40000/StockService">
    </baseAddresses>

Please note that, above I've used multicast address: 224.1.1.1:4000 in the base address of the service. It is important to note that multicast addresses must be in the range of 224.0.0.0 to 239.255.255.255. This address range refers class D in the context of IPv4. All addresses of this range have a binary pattern begin with 1110. Addresses of this range are reserved for special purposes. So free uses on this range are limited.

Finally I've created a console client application that will act as a sender for this demo. This client application (sender) will actually send out multicast messages and each service instance (receiver) will receive the multicast messages by listening at the correct UDP address. I've simulated multicasting in order to send multicast stock information by generating stock name and its price dynamically using Random class. Below is the client application code-

C#
var cf = new ChannelFactory<IStockService>("UdpBinding_IStockService");
var channel = cf.CreateChannel();

Console.WriteLine("*** One-Way Multicast Demo using UdpBinding ***\n");
Console.WriteLine("Please any key to start multicasting ...");
string command = Console.ReadLine();

    while (true)
    {
        var stock = GetStockDetails();
        channel.SendStockDetail(stock);

        Console.WriteLine("Details of the Stock: {0} has been sent.", stock.Name);
        System.Threading.Thread.Sleep(3000);
    }

((IClientChannel)channel).Close();

The method GetStockDetails generates stock information dynamically and then message is sent to the receiver services listening on the correct UDP address by using the IStockService service contract.

In order to run this demo, first set Host project as a StartUp Project and then start multiple instances of the service by pressing Ctrl+F5 multiple times. Then set Client project as a StartUp Project and hit Ctrl+F5 again to start Client project. Finally press any key on the Client (sender) console to start multicasting. You'll see all receivers are getting messages sent by the sender provided they are not lost.

Image 1

Now just stop any one receiver, and you’ll see that the sender is now multicasting messages to available receivers (running) without any problem.

Image 2

Next stop all receivers and you'll observer that the sender is still multicasting messages although there is no receiver. Why? As in the multicasting, there might be no receivers.

Image 3

Finally you can start multiple instances of the service (receivers) as well as multiple instances of the client (senders), and you'll notice that all receivers are getting messages from each sender provided they aren't lost. Why? As in multicasting, there might be more than one sender.

Image 4

Conclusion

So I've shown here how we can implement multicasting using UdpBinding in WCF 4.5 using One-Way messages. Please let me know your feedback and suggestions. Happy coding.

License

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