Standard Bindings Custom Binding Equivalents

Tuesday, 02 June 2009 01:19 PM
by Coose

Ever wanted to know the CustomBinding equivalent of a standard binding?  Use this tool to see them.

[More Click here to see the tool]

ERROR - UNABLE TO LOAD CONTROL : ~/User controls/BindingEvaluator.ascx

How’s it done?

Create the binding and save with the ServiceContractGenerator.  That writes to a config file.  Then I read it back.

So I created a web user control with a combo, literal and a button:

<asp:DropDownList ID="BindingList" runat="server">

    <asp:ListItem Text="BasicHttpBinding" Value="System.ServiceModel.BasicHttpBinding" />

    <asp:ListItem Text="CustomBinding" Value="System.ServiceModel.Channels.CustomBinding" />

    <asp:ListItem Text="MsmqIntegrationBinding" Value="System.ServiceModel.MsmqIntegration.MsmqIntegrationBinding" />

    <asp:ListItem Text="NetNamedPipeBinding" Value="System.ServiceModel.NetNamedPipeBinding" />

    <asp:ListItem Text="NetPeerTcpBinding" Value="System.ServiceModel.NetPeerTcpBinding" />

    <asp:ListItem Text="NetTcpBinding" Value="System.ServiceModel.NetTcpBinding" />

    <asp:ListItem Text="WSDualHttpBinding" Value="System.ServiceModel.WSDualHttpBinding" />

    <asp:ListItem Text="WSHttpBinding" Value="System.ServiceModel.WSHttpBinding" />

    <asp:ListItem Text="WS2007HttpBinding" Value="System.ServiceModel.WS2007HttpBinding" />

    <asp:ListItem Text="WSFederationHttpBinding" Value="System.ServiceModel.WSFederationHttpBinding" />

    <asp:ListItem Text="WS2007FederationHttpBinding" Value="System.ServiceModel.WS2007FederationHttpBinding" />

</asp:DropDownList>

<asp:Button ID="GoButton" runat="server" Text="Evaluate"

    onclick="GoButton_Click" />

<br />

<pre><asp:Literal runat="server" ID="BindingMarkup" /></pre>

And in the button handler:

protected void GoButton_Click(object sender, EventArgs e)

{

    Assembly asm = Assembly.Load("System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

    Type t = asm.GetType(BindingList.SelectedValue);

    Binding b = Activator.CreateInstance(t) as Binding;

 

    string configFile = Server.MapPath("~/App_Data/__bindings.config");

 

    Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

    fileMap.ExeConfigFilename = configFile;

    fileMap.MachineConfigFilename = machineConfig.FilePath;

    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

    config.NamespaceDeclared = true;

 

    ServiceContractGenerator gen = new ServiceContractGenerator(config);

    string sectionName, configName;

    gen.GenerateBinding(new CustomBinding(b.CreateBindingElements()), out sectionName, out configName);

    config.Save();

 

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(File.ReadAllText(configFile));

 

    StringBuilder xml = new StringBuilder();

    using (XmlWriter writer = XmlWriter.Create(xml, new XmlWriterSettings() { Indent = true, IndentChars = "  ", NewLineChars = Environment.NewLine, NewLineOnAttributes = true }))

    {

        doc.Save(writer);

    }

 

    this.BindingMarkup.Text = HttpUtility.HtmlEncode(xml.ToString());

    File.Delete(configFile);

}

Comment on this
Development
|

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading