64

C# .NET 3.5 と WCF を使用して、クライアント アプリケーション (クライアントが接続しているサーバーの名前) の WCF 構成の一部を書き出そうとしています。

明らかな方法はConfigurationManager、構成セクションをロードし、必要なデータを書き出すために使用することです。

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");

常に null を返すようです。

var serviceModelSection = ConfigurationManager.GetSection("appSettings");

完璧に動作します。

構成セクションは App.config に存在しますが、何らかの理由ConfigurationManagerでセクションの読み込みを拒否していますsystem.ServiceModel

xxx.exe.config ファイルを手動でロードして XPath を使用することは避けたいのですが、どうしてもそうする必要がある場合はそうします。ちょっとしたハックのようです。

助言がありますか?

4

5 に答える 5

64

http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html

// Automagically find all client endpoints defined in app.config
ClientSection clientSection = 
    ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

ChannelEndpointElementCollection endpointCollection =
    clientSection.ElementInformation.Properties[string.Empty].Value as     ChannelEndpointElementCollection;
List<string> endpointNames = new List<string>();
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
    endpointNames.Add(endpointElement.Name);
}
// use endpointNames somehow ...

うまく機能しているように見えます。

于 2008-08-21T10:35:08.510 に答える
50

<system.serviceModel>要素は、セクションではなく、構成セクショングループです。System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()グループ全体を取得するには、を使用する必要があります。

于 2008-08-21T10:38:07.083 に答える
16

これは、ポインターの@marxidadのおかげで私が探していたものです。

    public static string GetServerName()
    {
        string serverName = "Unknown";

        Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
        BindingsSection bindings = serviceModel.Bindings;

        ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;

        for(int i=0; i<endpoints.Count; i++)
        {
            ChannelEndpointElement endpointElement = endpoints[i];
            if (endpointElement.Contract == "MyContractName")
            {
                serverName = endpointElement.Address.Host;
            }
        }

        return serverName;
    }
于 2008-08-21T10:51:07.800 に答える
10

GetSectionGroup() はパラメーターをサポートしていません (フレームワーク 3.5 で)。

代わりに次を使用します。

Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
于 2011-05-13T02:19:35.870 に答える
7

Thanks to the other posters this is the function I developed to get the URI of a named endpoint. It also creates a listing of the endpoints in use and which actual config file was being used when debugging:

Private Function GetEndpointAddress(name As String) As String
    Debug.Print("--- GetEndpointAddress ---")
    Dim address As String = "Unknown"
    Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Debug.Print("app.config: " & appConfig.FilePath)
    Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig)
    Dim bindings As BindingsSection = serviceModel.Bindings
    Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints
    For i As Integer = 0 To endpoints.Count - 1
        Dim endpoint As ChannelEndpointElement = endpoints(i)
        Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString)
        If endpoint.Name = name Then
            address = endpoint.Address.ToString
        End If
    Next
    Debug.Print("--- GetEndpointAddress ---")
    Return address
End Function
于 2013-09-17T08:34:13.247 に答える