UDP Binding Example in WCF 4.5

UDP:

UDP (User Datagram Protocol) is a connectionless protocol that runs on top of IP networks. It is similar to TCP/IP. However, it provides less error handling and recovery that TCP/IP. It provides a direct way of send and receive datagrams over an IP network, primarily used for broadcasting messages across networks.

UDP and WCF 4.5:

WCF 4.5 framework supports UDP bindings. It is one of the new features of WCF 4.5. Following is an example how to use UDP Binding in .Net 4.5

Service Configuration :

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name=””>

<serviceMetadata httpGetEnabled=”true” httpsGetEnabled=”true”/>

<serviceDebug includeExceptionDetailInFaults=”false”/>

<serviceThrottling maxConcurrentCalls=”1000″ maxConcurrentInstances=”1000″ maxConcurrentSessions=”1000″/>

</behavior>

</serviceBehaviors>

</behaviors>

<services>

<service name=”UdpServiceHost.HelloService”>

<endpoint address=”soap.udp://localhost:8080/” binding=”udpBinding” contract=”UdpServiceHost.IService”/>

<host>

<baseAddresses>

<add baseAddress=”http://localhost:8080/UdpServiceHost/”/&gt;

</baseAddresses>

</host>

</service>

</services>

</system.serviceModel>

Following is the service file and service host

[ServiceContract]
public interface IService
{
[OperationContract]
string SayHello(string input);
}

public class HelloService:IService
{

public string SayHello(string input)
{
if (string.IsNullOrEmpty(input))
{
Console.WriteLine(“You didnt entered your name …”);
return “Hello…”;
}
Console.WriteLine(“Hi {0}”, input);
return “Hello “+input;
}
}

class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(HelloService));
host.Open();

Console.WriteLine(“Press enter to terminate host…”);
Console.ReadLine();
host.Close();

}

Troubleshooting: 

You may get the following error while running the UDP service

If you get this error you need to run the service with administrative privileges or open the port with UDP protocol in Firewall where the service is hosted.

Sample Code:

Download the complete code samples from Here.

2 Responses to UDP Binding Example in WCF 4.5

  1. Larry Smith says:

    I installed .Net Framework 4.5 on Windows Server 2008 R2.

    I tried adding an Adapter Service Reference to a Visual Studio 2012 console application project, and the UDP binding does not appear in binding combo box.

    Also, i don’t see the UDP binding if configuring Biztalk port

    Am I missing something?

  2. Robert Phillips says:

    I have the same issue with the udpbinding type. I can’t get it in the available list.
    Does anybody know how to solve that problem ?

Leave a comment