2011-05-23 9 views
11

मैं अपने क्लाइंट endpoint विन्यास पढ़ने के लिए एसी # नमूना कोड कर सकते हैं:प्रोग्रामिंग के रूप में मैं अपने सी # अनुप्रयोग के वर्तमान अंतराल कैसे खोज सकता हूं?

List<string> addresses = GetMyCurrentEndpoints(); 

परिणाम के रूप में हम के लिए होता है:

<client> 
    <endpoint address="http://mycoolserver/FinancialService.svc" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IFinancialService" 
     contract="IFinancialService" name="WSHttpBinding_IFinancialService"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="http://mycoolserver/HumanResourcesService.svc" 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHumanResourceService" 
     contract="IHumanResourceService" name="WSHttpBinding_IHumanResourceService"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 

और लक्ष्य अंतिमबिंदुओं पते की एक सरणी प्राप्त करने के लिए है

[0] http://mycoolserver/FinancialService.svc 
[1] http://mycoolserver/HumanResourcesService.svc 

उत्तर

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

(http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html से लिया)

17

यह मेरा पहला जवाब है। सभ्य रहें :)

private List<string> GetMyCurrentEndpoints() 
{ 
    var endpointList = new List<string>(); 

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config); 

    foreach (ChannelEndpointElement endpoint in serviceModel.Client.Endpoints) 
    { 
     endpointList.Add(endpoint.Address.ToString()); 
    } 

    return endpointList; 
} 
+0

अच्छा! वेब एप्लिकेशन पर इसका उपयोग करने की कोशिश करने वाले किसी भी व्यक्ति के लिए, आपके पास एक ArgumentException होना चाहिए जैसे 'exePath को केवल स्टैंड के अंदर नहीं चलते समय निर्दिष्ट किया जाना चाहिए।' क्लाइंट सेक्शन पढ़ने के लिए, @Dmitry टिप ठीक है। लेकिन मैं आपका जवाब भी दे रहा हूं! –

संबंधित मुद्दे

 संबंधित मुद्दे