Create XML representation of WCF binding instance

吃可爱长大的小学妹 提交于 2019-12-09 03:31:26

The class I was looking for was System.ServiceModel.Description.ServiceContractGenerator

Exemple to generate a configuration for an instance of any kind of Binding:

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;
}

This solution feels like a hack because of the need to generate empty temporary files, but it works.

Now I'll have to look for a way to have a fully in-memory instance of a System.Configuration.Configuration (maybe by writing my own implementation)

Added missing code parts:

  • // [... create empty .exe and empty .exe.config...]
  • // [... delete the temporary files ...]

        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;
    }
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!