The final piece that we need to add to our Transport is configuration support. To expose our transport through config, we need to implement two configuration sections.
Binding Element Extension Section
The first class we add is to configure UdpTransportBindingElement. This is so that custom binding implementations can include udp. In UdpTransportSection we define our configuration section name (how we are referenced from config), the type of our binding element, and how to create our binding element; all with just a few simple overrides. We can then register our extension section in a .config file as follows:
<configuration>
<system.serviceModel>
<extensions>
<bindingelementextensions>
<add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportSection, UdpTransport" />
</bindingelementextensions>
</extensions>
</system>
</configuration>
And it can be referenced from custom bindings to use UDP as their transport:
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding configurationName=”UdpCustomBinding”>
<udpTransport/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>
Binding Section
The second class we add is to configure SampleProfileUdpBinding, our “standard binding”. Through SampleProfileUdpBindingSection we expose this binding to the configuration system. Most of the methods delegate to SampleProfileUdpBindingConfigurationElement, which derives from StandardBindingConfigurationElement.
SampleProfileUdpBindingConfigurationElement has properties that correspond to the properties on SampleProfileUdpBinding, and functions that map from the ConfigurationElement to Binding and vice-versa.
Finally, we need to override the OnApplyConfiguration() function in our SampleProfileUdpBinding:
protected override void
OnApplyConfiguration(
string
configurationName)
{
SampleProfileUdpBindingSection
section = (
SampleProfileUdpBindingSection
)
ConfigurationManager
.GetSection(
UdpConstants
.UdpBindingSectionName);
SampleProfileUdpBindingConfigurationElement
element = section.Bindings[configurationName];
if
(element ==
null
)
{
throw new
ConfigurationErrorsException
(string
.Format(CultureInfo
.CurrentCulture, “There is no binding named {0} at {1}.”, configurationName, section.SectionInformation.Name));
}
else
{
element.ApplyConfiguration(this
);
}
}
To register this handler with the configuration system, we add the following section to a .config file:
<configuration>
<configSections>
<sectionGroup name=”system.serviceModel”>
<sectionGroup name=”bindings”>
<section name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingSection, UdpTransport" />
</sectionGroup>
</sectionGroup>
</configSections>
</configuration>
It can then be referenced from serviceModel config:
<configuration>
<system.serviceModel>
<client>
<endpoint configurationName=”calculator”
address=”soap.udp://localhost:8080/”
bindingConfiguration=”CalculatorServer”
bindingSectionName="sampleProfileUdpBinding"
contractType= “Microsoft.ServiceModel.Samples.ICalculatorContract”>
</endpoint>
</client>
</system.serviceModel>
</configuration>
The result is a completely functional third party transport that can be used in any Indigo program!