Click here to Skip to main content
12,886,364 members (41,167 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 9 Apr 2014

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


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:// and an UDP endpoint can be added in a service via app.config file in following manner-

<endpoint address="soap.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://"));

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

public interface IStockService
    [OperationContract(IsOneWay = true)]
    void SendStockDetail(Stock stock);

public class Stock
    public string Name;

    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" />
       <add baseaddress="soap.udp://">

Please note that, above I've used multicast address: in the base address of the service. It is important to note that multicast addresses must be in the range of to 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();

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


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.


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.


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


About the Author

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


You may also be interested in...


Comments and Discussions

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

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