【问题标题】:Add node system.serviceModel in APP.Config C# in runtime在运行时在 APP.Config C# 中添加节点 system.serviceModel
【发布时间】:2018-01-26 22:24:23
【问题描述】:

我需要在我的程序运行时将此部分添加到我的 app.config 文件中

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="InvioTelematicoSS730pMtomPortBinding" messageEncoding="Mtom">
      <security mode="Transport" />
    </binding>
    <binding name="RicevutaPdf730PortBinding">
      <security mode="Transport" />
    </binding>
    <binding name="RicevutaPdf730PortBinding1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"
      binding="basicHttpBinding" bindingConfiguration="InvioTelematicoSS730pMtomPortBinding"
      contract="InvioFlussi730.InvioTelematicoSS730pMtom" name="InvioTelematicoSS730pMtomPort" />
  <endpoint address="https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"
      binding="basicHttpBinding" bindingConfiguration="RicevutaPdf730PortBinding"
      contract="ServiceReference1.RicevutaPdf730" name="RicevutaPdf730Port" />
</client>

有什么办法吗?提前致谢

【问题讨论】:

  • 对不起,但这没有帮助...我想知道是否有可能在项目运行时将我粘贴到文件 app.config 的字符串添加...如何可以在 c# 中添加 system.serviceModel 部分吗?我可以帮忙吗?

标签: c# runtime app-config


【解决方案1】:

.NET 为 WCF 服务公开配置元素类以在运行时管理它们。编写了一个简单的方法来为您构建和生成这些部分。

private static void WriteWCFConfig()
{
    //standard method from System.Configuration 
    Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    //the main section in the app.config file for WCF 
    ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);

    var httpBindings = serviceModel.Bindings.BasicHttpBinding;
    if (!httpBindings.ContainsKey("InvioTelematicoSS730pMtomPortBinding"))
    {
        BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("InvioTelematicoSS730pMtomPortBinding");
        newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
        newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;

        httpBindings.Bindings.Add(newHttpBindng);
    }
    if (!httpBindings.ContainsKey("RicevutaPdf730PortBinding"))
    {
        BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("RicevutaPdf730PortBinding");
        newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
        newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;

        httpBindings.Bindings.Add(newHttpBindng);
    }

    //the section 
    ChannelEndpointElementCollection endPoints = serviceModel.Client.Endpoints;

    //Get endpoint names
    List<string> endpointNames = new List<string>();
    foreach (ChannelEndpointElement endpointElement in endPoints)
    {
        endpointNames.Add(endpointElement.Name);
    }

    if (!endpointNames.Contains("InvioTelematicoSS730pMtomPort"))
    {
        ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"), "InvioFlussi730.InvioTelematicoSS730pMtom");
        endPoint.Name = "InvioTelematicoSS730pMtomPort";
        endPoint.Binding = "basicHttpBinding";
        endPoint.BindingConfiguration = "InvioTelematicoSS730pMtomPortBinding";

        endPoints.Add(endPoint);
    }
    if (!endpointNames.Contains("RicevutaPdf730Port"))
    {
        ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"), "ServiceReference1.RicevutaPdf730");
        endPoint.Name = "RicevutaPdf730Port";
        endPoint.Binding = "basicHttpBinding";
        endPoint.BindingConfiguration = "RicevutaPdf730PortBinding";

        endPoints.Add(endPoint);
    }

    appConfig.Save();
}

你可以拿来根据自己的需要修改。

希望这会有所帮助。

【讨论】:

  • 非常感谢
猜你喜欢
  • 2012-01-23
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多