Frage

Ich habe eine app.config-Datei, die in Form von:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <system.serviceModel>
      <client>
        <endpoint address="http://something.com"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
        contract="ABC" name="XXX" />
        <endpoint address="http://something2.com"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
        contract="ABC2" name="YYY" />
      </client>
    </system.serviceModel>
  </configuration>

Ich möchte den Wert an Attribut „Adresse“ des Knotens Endpunkt lesen, die den Namen hat = „XXX“. Bitte zeigen Sie mir, wie es geht!

(weiter belowing mit marc_s Disscussing. Es tut uns Leid, den Text hier zu setzen, da Kommentar nicht erlauben, zu Formatcodes) @marc_s: Ich verwende die folgenden Codes, um die oben angegebene Datei zu lesen, aber es zeigt, dass die clientSection.Endpoints 0 Mitglieder hat (Count = 0). Bitte Hilfe!

public MainWindow()
    {
        var exeFile = Environment.GetCommandLineArgs()[0];
        var configFile = String.Format("{0}.config", exeFile);
        var config = ConfigurationManager.OpenExeConfiguration(configFile);
        var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
        var clientSection = wcfSection.Client;
        foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints)
        {
            if (endpointElement.Name == "XXX")
            {
                var addr = endpointElement.Address.ToString();
            }
        }
    }
War es hilfreich?

Lösung

Sie nicht wirklich brauchen, um -. WCF-Laufzeit wird für Sie all das tun

Wenn Sie wirklich müssen - gleich aus welchem ??Grund - Sie können dies tun:

using System.Configuration;
using System.ServiceModel.Configuration;

ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

string address = null;

foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints)
{
   if(endpoint.Name == "XXX")
   {
      address = endpoint.Address.ToString();
      break;
   }
}

Andere Tipps

Sie können die ServiceModelSectionGroup verwenden (System.ServiceModel.Configuration) den Zugriff auf die Konfiguration:

    var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup;
    foreach (ChannelEndpointElement endpoint in config.Client.Endpoints)
    {
        Uri address = endpoint.Address;
        // Do something here
    }

Ich hoffe, das hilft.

var config = ConfigurationManager.OpenExeConfiguration("MyApp.exe.config");
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
var clientSection = wcfSection.Client;
foreach(ChannelEndpointElement endpointElement in clientSection.Endpoints) {
    if(endpointElement.Name == "XXX") {
        return endpointElement.Address;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top