Using UDP multicast channel in WCF
I recently needed to build a WCF UDP application as a proof of concept.
Sadly UDP is not among the channels bundled with the WCF, so I needed to put up something myself:
-
-
-
Added the extensions section to the config file to add the UDP support (config file coming up soon)
-
Added multicast="true" switch to the binding section
-
The address prefix is hardcoded in the UdpChannelHelpers.cs file, so if you like something other than "soap.udp" you need to edit the "Scheme" constant.
-
You need to remember that since this is multicast, the client is the one doing the transmitting while the service is a only a passive listener.
Client configuration:
<configuration>
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
</bindingElementExtensions>
<bindingExtensions>
<add name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingCollectionElement, UdpTransport" />
</bindingExtensions>
</extensions>
<client>
<endpoint address="net.udp://225.225.0.1:2222/Observer/"
binding="customBinding"
bindingConfiguration="DatagramServer" contract="MyNamespace.IObserver"
name="ClientService" />
</client>
<bindings>
<customBinding>
<binding name="DatagramServer">
<binaryMessageEncoding></binaryMessageEncoding>
<udpTransport multicast="true" />
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>
Service configuration:
<configuration>
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
</bindingElementExtensions>
<bindingExtensions>
<add name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingCollectionElement, UdpTransport" />
</bindingExtensions>
</extensions>
<services>
<service name="ObserverService.Observer">
<endpoint address="net.udp://225.225.0.1:2222/Observer/"
binding="customBinding"
bindingConfiguration="DatagramServer"
contract="MyNamespace.IObserver" />
</service>
</services>
<bindings>
<customBinding>
<binding name="DatagramServer">
<binaryMessageEncoding></binaryMessageEncoding>
<udpTransport multicast="true" />
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>