2008-08-21 13 views
55

सी # .NET 3.5 और डब्ल्यूसीएफ का उपयोग कर सिस्टम। सर्विसमोडेल कॉन्फ़िगरेशन सेक्शन लोड हो रहा है, मैं क्लाइंट एप्लिकेशन में कुछ डब्ल्यूसीएफ कॉन्फ़िगरेशन लिखने की कोशिश कर रहा हूं (क्लाइंट के सर्वर से कनेक्ट होने वाले सर्वर का नाम)।कॉन्फ़िगरेशन मैनेजर

कॉन्फ़िगरेशन अनुभाग लोड करने के लिए ConfigurationManager का उपयोग करने का स्पष्ट तरीका है और मुझे आवश्यक डेटा लिखना है।

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

हमेशा शून्य लौटने लगता है।

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

पूरी तरह से काम करता है।

कॉन्फ़िगरेशन अनुभाग App.config में मौजूद है लेकिन किसी कारण से ConfigurationManagersystem.ServiceModel अनुभाग लोड करने से इंकार कर देता है।

मैं xxx.exe.config फ़ाइल मैन्युअल रूप से लोड करने और XPath का उपयोग करने से बचना चाहता हूं, लेकिन अगर मुझे इसका सहारा लेना है तो मैं इसका सहारा लेना चाहता हूं। बस एक हैक की तरह लगता है।

कोई सुझाव?

उत्तर

55

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 ... 

अच्छी तरह से काम करने के लिए प्रकट होता है।

+1

endpointCollection = clientSection.ElementInformation.Properties [string.Empty] .Value ChannelEndpointElementCollection के रूप में के लिए भ्रामक लाइन; को क्लाइंटसेक्शन.इंडपॉइंट्स के लिए सरलीकृत किया जाना चाहिए; – joedotnot

14

यही वह है जो मैं सूचक के लिए @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; 
    } 
8

GetSectionGroup() कोई पैरामीटर (फ्रेमवर्क 3.5 के तहत) का समर्थन नहीं करता है।

इसके बजाय का उपयोग करें:

Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config); 
7

अन्य पोस्टर के लिए धन्यवाद इस समारोह मैं एक नामित endpoint की यूआरआई प्राप्त करने के लिए विकसित की है। यह भी उपयोग में और जो अंतिम बिंदुओं वास्तविक कॉन्फ़िग फ़ाइल जब डिबगिंग किया जा रहा था की एक सूची बनाता है:

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 
संबंधित मुद्दे