Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Multicasting using UdpBinding in WCF 4.5

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
10 Apr 2014CPOL4 min read 39.3K   1.1K   26   15
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)


Written By
Technical Lead Infogain India Pvt Ltd
India India


Samir NIGAM is a Microsoft Certified Professional. He is an insightful IT professional with results-driven comprehensive technical skill having rich, hands-on work experience n web-based applications using ASP.NET, C#, AJAX, Web Service, WCF, jQuery, Microsoft Enterprise Library , LINQ, MS Entity Framework, nHibernate, MS SQL Server & SSRS.



He has earned his master degree (MCA) from U.P. Technical University, Lucknow, INDIA, his post graduate dipoma (PGDCA ) from Institute of Engineering and Rural Technology, Allahabad, INDIA and his bachelor degree (BSc - Mathematics) from University of Allahabad, Allahabad, INDIA.



He has good knowledge of Object Oriented Programming, n-Tier Architecture, SOLID Principle, and Algorithm Analysis & Design as well as good command over cross-browser client side programming using JavaScript & jQuery,.



Awards:



Comments and Discussions

 
QuestionHow do you add a Service Reference in a Web Client? Pin
Member 1135711719-Jan-17 7:22
Member 1135711719-Jan-17 7:22 
QuestionUDP and Azure Pin
bangkokrat19-Mar-16 5:02
bangkokrat19-Mar-16 5:02 
QuestionWPF vs ConsoleHost Pin
Victor Arnoldo Olvera5-Jul-15 9:36
Victor Arnoldo Olvera5-Jul-15 9:36 
QuestionSendStockDetail returns Stock instead of void, and then capture info Pin
Victor Arnoldo Olvera4-Jul-15 4:28
Victor Arnoldo Olvera4-Jul-15 4:28 
QuestionCannot start multiple receivers Pin
Member 117121302-Jun-15 8:09
Member 117121302-Jun-15 8:09 
Suggestion[My vote of 2] Reference to original? Pin
Amund Gjersøe26-May-14 9:30
Amund Gjersøe26-May-14 9:30 
GeneralRe: [My vote of 2] Reference to original? Pin
Samir NIGAM26-May-14 19:16
Samir NIGAM26-May-14 19:16 
GeneralRe: [My vote of 2] Reference to original? Pin
Amund Gjersøe26-May-14 20:30
Amund Gjersøe26-May-14 20:30 
GeneralRe: [My vote of 2] Reference to original? Pin
Samir NIGAM26-May-14 21:50
Samir NIGAM26-May-14 21:50 
GeneralMy vote of 5 Pin
Volynsky Alex10-Apr-14 11:53
professionalVolynsky Alex10-Apr-14 11:53 
GeneralRe: My vote of 5 Pin
Samir NIGAM10-Apr-14 19:16
Samir NIGAM10-Apr-14 19:16 
GeneralRe: My vote of 5 Pin
Volynsky Alex10-Apr-14 22:31
professionalVolynsky Alex10-Apr-14 22:31 
QuestionQuery about UDP & multicasting, Pin
Tridip Bhattacharjee9-Apr-14 8:53
professionalTridip Bhattacharjee9-Apr-14 8:53 
AnswerRe: Query about UDP & multicasting, Pin
Samir NIGAM9-Apr-14 9:02
Samir NIGAM9-Apr-14 9:02 
GeneralRe: Query about UDP & multicasting, Pin
Tridip Bhattacharjee9-Apr-14 20:45
professionalTridip Bhattacharjee9-Apr-14 20:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.