创建 WCF 绑定实例的 XML 表示

发布于 2024-12-21 07:43:35 字数 709 浏览 3 评论 0原文

我正在尝试使用 WSHttpBinding.CreateBindingElements 方法

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

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

有办法做到这一点吗?

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

I'm trying to write code to convert a WCF wsHttpBinding to customBinding, using the method described on WSHttpBinding.CreateBindingElements Method
.

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

Once I have generated the custom binding, I want to generate an XML representation for that new custom binding. (The same XML representation that's found in an application's .config file).

Is there a way to do that?

(I'm aware of the tool referenced in this answer: https://stackoverflow.com/a/4217892/5688, but I need something I can call within an application and without depending on a service in the cloud)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-12-28 07:43:35

我正在寻找的类是 System.ServiceModel.Description。 ServiceContractGenerator

为任何类型的 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;
}

此解决方案感觉像是黑客,因为需要生成空的临时文件,但它确实有效。

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

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)

飘落散花 2024-12-28 07:43:35

添加了缺少的代码部分:

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

     public static string SerializeBindingToXmlString(Binding绑定)
    {
        var tempConfig = System.IO.Path.GetTempFileName();
        var tempExe = tempConfig + ".exe";
        var tempExeConfig = tempConfig + ".exe.config";
        使用(System.IO.FileStream fs = new System.IO.FileStream(tempExe,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite))
        {
    
        }
        使用 (System.IO.FileStream fs = new System.IO.FileStream(tempExeConfig, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
        {
            fs.SetLength(0);
            使用 (System.IO.StreamWriter sr = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8)) {
                sr.WriteLine("");
                sr.WriteLine(@"<配置/>");
            }
        }
    
        var 配置 = System.Configuration.ConfigurationManager.OpenExeConfiguration(tempExe);
        var ContractGenerator = new System.ServiceModel.Description.服务合约生成器(配置);
        字符串绑定部分名称;
        字符串配置名称;
        ContractGenerator.GenerateBinding(绑定,出绑定部分名称,出配置名称);
    
       var bindingsSection =System.ServiceModel.Configuration.BindingsSection.GetSection(contractGenerator.Configuration);
    
        // 需要调用此函数才能让 GetRawXml() 返回更新后的配置
        //(否则将返回空字符串)
        ContractGenerator.Configuration.Save();
    
        字符串 xmlConfig = bindingsSection.SectionInformation.GetRawXml();
        System.IO.File.Delete(tempExeConfig);
        System.IO.File.Delete(tempExe);
        返回 xmlConfig;
    }
    

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;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文