【问题标题】:How can I discover current endpoints of my c# application programmatically?如何以编程方式发现我的 c# 应用程序的当前端点?
【发布时间】:2011-08-31 10:12:01
【问题描述】:

如何编写用于读取客户端端点配置的 c# 示例:

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

而目标是获取端点地址数组:

List<string> addresses = GetMyCurrentEndpoints();

结果我们会:

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

【问题讨论】:

    标签: c# wcf endpoints


    【解决方案1】:

    这是我第一次回答。温柔点:)

    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;
    }
    

    【讨论】:

    • 不错!对于任何试图在 Web 应用程序上使用它的人,您应该有一个 ArgumentException,例如“不在独立 exe 中运行时必须指定 exePath。”对于阅读客户端部分,@Dmitry 提示很好。但我也在投票赞成你的答案!
    【解决方案2】:
    // 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多