【问题标题】:Create XML representation of WCF binding instance创建 WCF 绑定实例的 XML 表示
【发布时间】:2011-12-15 15:58:37
【问题描述】:

我正在尝试编写代码以使用WSHttpBinding.CreateBindingElements Method 中描述的方法将 WCF wsHttpBinding 转换为 customBinding。

Binding wsHttpBinding = ...
BindingElementCollection beCollection = originalBinding.CreateBindingElements();
foreach (var element in beCollection)
{
    customBinding.Elements.Add(element);
}

生成自定义绑定后,我想为新的自定义绑定生成 XML 表示。 (与应用程序的 .config 文件中相同的 XML 表示形式)。

有没有办法做到这一点?

(我知道此答案中引用的工具:https://stackoverflow.com/a/4217892/5688,但我需要可以在应用程序中调用且不依赖于云服务的工具)

【问题讨论】:

    标签: wcf wcf-binding


    【解决方案1】:

    我要找的课程是System.ServiceModel.Description.ServiceContractGenerator

    为任何类型的绑定实例生成配置的示例:

    public static string SerializeBindingToXmlString(Binding binding)
    {
        var tempConfig = Path.GetTempFileName();
        var tempExe = tempConfig + ".exe";
        var tempExeConfig = tempConfig + ".exe.config";
        // [... create empty .exe and empty .exe.config...]
    
        var configuration = ConfigurationManager.OpenExeConfiguration(tempExe);
        var contractGenerator = new ServiceContractGenerator(configuration);
        string bindingSectionName;
        string configurationName;
        contractGenerator.GenerateBinding(binding, out bindingSectionName, out configurationName);
    
        BindingsSection bindingsSection = BindingsSection.GetSection(contractGenerator.Configuration);
    
        // this needs to be called in order for GetRawXml() to return the updated config
        // (otherwise it will return an empty string)
        contractGenerator.Configuration.Save(); 
    
        string xmlConfig = bindingsSection.SectionInformation.GetRawXml();
    
        // [... delete the temporary files ...]
        return xmlConfig;
    }
    

    由于需要生成空的临时文件,这个解决方案感觉像是一个 hack,但它确实有效。

    现在我必须寻找一种方法来拥有一个完全在内存中的 System.Configuration.Configuration 实例(也许通过编写我自己的实现)

    【讨论】:

      【解决方案2】:

      添加了缺失的代码部分:

      • // [...创建空的.exe和空的.exe.config...]
      • // [...删除临时文件...]

            public static string SerializeBindingToXmlString(Binding binding)
        {
            var tempConfig = System.IO.Path.GetTempFileName();
            var tempExe = tempConfig + ".exe";
            var tempExeConfig = tempConfig + ".exe.config";
            using(System.IO.FileStream fs = new System.IO.FileStream(tempExe, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
            {
        
            }
            using (System.IO.FileStream fs = new System.IO.FileStream(tempExeConfig, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
            {
                fs.SetLength(0);
                using (System.IO.StreamWriter sr = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8)) {
                    sr.WriteLine("<?xml version= \"1.0\" encoding=\"utf-8\" ?>");
                    sr.WriteLine(@"<configuration />");
                }
            }
        
            var configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(tempExe);
            var contractGenerator = new System.ServiceModel.Description. ServiceContractGenerator(configuration);
            string bindingSectionName;
            string configurationName;
            contractGenerator.GenerateBinding(binding, out bindingSectionName, out configurationName);
        
           var bindingsSection =System.ServiceModel.Configuration.BindingsSection.GetSection(contractGenerator.Configuration);
        
            // this needs to be called in order for GetRawXml() to return the updated config
            // (otherwise it will return an empty string)
            contractGenerator.Configuration.Save();
        
            string xmlConfig = bindingsSection.SectionInformation.GetRawXml();
            System.IO.File.Delete(tempExeConfig);
            System.IO.File.Delete(tempExe);
            return xmlConfig;
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多