我正在尝试使用WSHttpBinding.CreateBindingElements方法中描述的方法编写代码以将WCF wsHttpBinding转换为customBinding .
Binding wsHttpBinding = ...
BindingElementCollection beCollection = originalBinding.CreateBindingElements();
foreach (var element in beCollection)
{
customBinding.Elements.Add(element);
}
Run Code Online (Sandbox Code Playgroud)
生成自定义绑定后,我想为该新的自定义绑定生成XML表示.(与应用程序的.config文件中的XML表示形式相同).
有没有办法做到这一点?
(我知道这个答案中引用的工具:https://stackoverflow.com/a/4217892/5688,但我需要一些我可以在应用程序中调用的东西,而不依赖于云中的服务)
我正在寻找的类是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;
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案感觉像是一个黑客,因为需要生成空的临时文件,但它的工作原理.
现在,我将不得不寻找一种方法来获得System.Configuration.Configuration的完全内存实例(可能通过编写我自己的实现)
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |