Click here to Skip to main content
12,249,488 members (40,899 online)
Click here to Skip to main content
Add your own
alternative version

Stats

14.1K views
582 downloads
25 bookmarked
Posted

Multicasting using UdpBinding in WCF 4.5

, 10 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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-

<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-

// 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-

[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-

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-

<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-

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.

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

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.

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.

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)

Share

About the Author

Samir NIGAM
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:


You may also be interested in...

Comments and Discussions

 
QuestionUDP and Azure Pin
bangkokrat19-Mar-16 6:02
memberbangkokrat19-Mar-16 6:02 
QuestionWPF vs ConsoleHost Pin
Victor Arnoldo Olvera5-Jul-15 10:36
memberVictor Arnoldo Olvera5-Jul-15 10:36 
QuestionSendStockDetail returns Stock instead of void, and then capture info Pin
Victor Arnoldo Olvera4-Jul-15 5:28
memberVictor Arnoldo Olvera4-Jul-15 5:28 
QuestionCannot start multiple receivers Pin
Member 117121302-Jun-15 9:09
memberMember 117121302-Jun-15 9:09 
Suggestion[My vote of 2] Reference to original? Pin
Amund Gjersøe26-May-14 10:30
memberAmund Gjersøe26-May-14 10:30 
GeneralRe: [My vote of 2] Reference to original? Pin
Samir NIGAM26-May-14 20:16
memberSamir NIGAM26-May-14 20:16 
GeneralRe: [My vote of 2] Reference to original? Pin
Amund Gjersøe26-May-14 21:30
memberAmund Gjersøe26-May-14 21:30 
GeneralRe: [My vote of 2] Reference to original? Pin
Samir NIGAM26-May-14 22:50
memberSamir NIGAM26-May-14 22:50 
GeneralMy vote of 5 Pin
Volynsky Alex10-Apr-14 12:53
professionalVolynsky Alex10-Apr-14 12:53 
GeneralRe: My vote of 5 Pin
Samir NIGAM10-Apr-14 20:16
memberSamir NIGAM10-Apr-14 20:16 
GeneralRe: My vote of 5 Pin
Volynsky Alex10-Apr-14 23:31
professionalVolynsky Alex10-Apr-14 23:31 
QuestionQuery about UDP & multicasting, Pin
Tridip Bhattacharjee9-Apr-14 9:53
memberTridip Bhattacharjee9-Apr-14 9:53 
AnswerRe: Query about UDP & multicasting, Pin
Samir NIGAM9-Apr-14 10:02
memberSamir NIGAM9-Apr-14 10:02 
GeneralRe: Query about UDP & multicasting, Pin
Tridip Bhattacharjee9-Apr-14 21:45
memberTridip Bhattacharjee9-Apr-14 21: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 10 Apr 2014
Article Copyright 2014 by Samir NIGAM
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid