{"id":40,"date":"2005-08-04T09:21:15","date_gmt":"2005-08-04T16:21:15","guid":{"rendered":"http:\/\/kennyw.com\/indigo\/40"},"modified":"2005-08-04T09:21:15","modified_gmt":"2005-08-04T16:21:15","slug":"channels-130-configuration-support","status":"publish","type":"post","link":"https:\/\/kennyw.com\/?p=40","title":{"rendered":"Channels 130: Configuration support"},"content":{"rendered":"<p>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.<\/p>\n<h4>Binding Element Extension Section<\/h4>\n<p>The first class we add is to configure UdpTransportBindingElement. This is so that custom binding implementations can include udp. In <em>UdpTransportSection<\/em> 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:<\/p>\n<div class=\"code\">\n&lt;configuration&gt;<br \/>\n&nbsp;&lt;system.serviceModel&gt;<br \/>\n&nbsp;&nbsp;&lt;extensions&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;bindingelementextensions&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<code class=\"keyword\"><strong>&lt;add name=\"udpTransport\" type=\"Microsoft.ServiceModel.Samples.UdpTransportSection, UdpTransport\" \/&gt;<\/strong><\/code><br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/bindingelementextensions&gt;<br \/>\n&nbsp;&nbsp;&lt;\/extensions&gt;<br \/>\n&nbsp;&lt;\/system&gt;<br \/>\n&lt;\/configuration&gt;\n<\/div>\n<p>And it can be referenced from custom bindings to use UDP as their transport:<\/p>\n<div class=\"code\">\n&lt;configuration&gt;<br \/>\n&nbsp;&lt;system.serviceModel&gt;<br \/>\n&nbsp;&nbsp;&lt;bindings&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;customBinding&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;binding configurationName=&#8221;UdpCustomBinding&#8221;&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code class=\"keyword\"><strong>&lt;udpTransport\/&gt;<\/strong><\/code><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/binding&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/customBinding&gt;<br \/>\n&nbsp;&nbsp;&lt;\/bindings&gt;<br \/>\n&nbsp;&lt;\/system.serviceModel&gt;<br \/>\n&lt;\/configuration&gt;\n<\/div>\n<h4>Binding Section<\/h4>\n<p>The second class we add is to configure SampleProfileUdpBinding, our &#8220;standard binding&#8221;. Through <em>SampleProfileUdpBindingSection<\/em> we expose this binding to the configuration system. Most of the methods delegate to <em>SampleProfileUdpBindingConfigurationElement<\/em>, which derives from <em>StandardBindingConfigurationElement<\/em>.<\/p>\n<p>SampleProfileUdpBindingConfigurationElement has properties that correspond to the properties on SampleProfileUdpBinding, and functions that map from the ConfigurationElement to Binding and vice-versa.<\/p>\n<p>Finally, we need to override the OnApplyConfiguration() function in our SampleProfileUdpBinding:<\/p>\n<div class=\"code\">\n    <code class=\"keyword\">protected override void<\/code> OnApplyConfiguration(<code class=\"keyword\">string<\/code> configurationName)<br \/>\n    {<\/p>\n<div class=\"indent1\"><code class=\"classname\">SampleProfileUdpBindingSection<\/code> section = (<code class=\"classname\">SampleProfileUdpBindingSection<\/code>)<code class=\"classname\">ConfigurationManager<\/code>.GetSection(<code class=\"classname\">UdpConstants<\/code>.UdpBindingSectionName);<br \/>\n    <code class=\"classname\">SampleProfileUdpBindingConfigurationElement<\/code> element = section.Bindings[configurationName];<br \/>\n    <code class=\"keyword\">if<\/code> (element == <code class=\"keyword\">null<\/code>)<br \/>\n        {<\/p>\n<div class=\"indent1\"><code class=\"keyword\">throw new<\/code> <code class=\"classname\">ConfigurationErrorsException<\/code>(<code class=\"keyword\">string<\/code>.Format(<code class=\"classname\">CultureInfo<\/code>.CurrentCulture, &#8220;There is no binding named {0} at {1}.&#8221;, configurationName, section.SectionInformation.Name));<\/div>\n<p>}<br \/>\n        <code class=\"keyword\">else<\/code><br \/>\n        {<\/p>\n<div class=\"indent1\">element.ApplyConfiguration(<code class=\"keyword\">this<\/code>);<\/div>\n<p>}<\/p><\/div>\n<p>}<\/p><\/div>\n<p>To register this handler with the configuration system, we add the following section to a .config file:<\/p>\n<div class=\"code\">\n&lt;configuration&gt;<br \/>\n&nbsp;&lt;configSections&gt;<br \/>\n&nbsp;&nbsp;&lt;sectionGroup name=&#8221;system.serviceModel&#8221;&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;sectionGroup name=&#8221;bindings&#8221;&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<code class=\"keyword\"><strong>&lt;section name=\"sampleProfileUdpBinding\" type=\"Microsoft.ServiceModel.Samples.SampleProfileUdpBindingSection, UdpTransport\" \/&gt;<\/strong><\/code><br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/sectionGroup&gt;<br \/>\n&nbsp;&nbsp;&lt;\/sectionGroup&gt;<br \/>\n&nbsp;&lt;\/configSections&gt;<br \/>\n&lt;\/configuration&gt;\n<\/div>\n<p>It can then be referenced from serviceModel config:<\/p>\n<div class=\"code\">\n&lt;configuration&gt;<br \/>\n&nbsp;&lt;system.serviceModel&gt;<br \/>\n&nbsp;&nbsp;&lt;client&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;endpoint configurationName=&#8221;calculator&#8221;<\/p>\n<div class=\"indent1\">address=&#8221;soap.udp:\/\/localhost:8080\/&#8221;<br \/>\n                bindingConfiguration=&#8221;CalculatorServer&#8221;<br \/>\n                <code class=\"keyword\"><strong>bindingSectionName=\"sampleProfileUdpBinding\"<\/strong><\/code><br \/>\n                contractType= &#8220;Microsoft.ServiceModel.Samples.ICalculatorContract&#8221;&gt;<\/div>\n<p>&nbsp;&nbsp;&nbsp;&lt;\/endpoint&gt;<br \/>\n&nbsp;&nbsp;&lt;\/client&gt;<br \/>\n&nbsp;&lt;\/system.serviceModel&gt;<br \/>\n&lt;\/configuration&gt;\n<\/p><\/div>\n<p>The result is a completely functional third party transport that can be used in any Indigo program!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-indigo"],"_links":{"self":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":0,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}