【问题标题】:How do I read the "System.ServiceModel" configuration section group from a class library?如何从类库中读取“System.ServiceModel”配置节组?
【发布时间】:2016-08-18 23:03:41
【问题描述】:

有什么方法可以访问“System.ServiceModel”客户端配置,即基于类 (dll) 项目中的 app.config?


【问题讨论】:

  • 目前还不清楚基于类的项目 的含义。它是输出类型为“类库”的项目,即 DLL?
  • 是的。它需要读取一个 dll。
  • 如果您的 dll “consumer” 是一个 asp.net 应用程序/网站,只需将配置部分放入您的 web.config。
  • 看到这个,它可以帮助你:[stackoverflow.com/questions/14963870/…
  • 没有。其读取 (WCF) system.servicemodel 客户端配置。其次,在asp.net应用程序或网站中不消耗dll。

标签: c# wcf wif


【解决方案1】:

ConfigurationManager.GetSection(string) 允许您从正在执行的应用程序的 app.config 或 web.config 打开一个部分。但system.ServiceModel 不是一个部分,它是一个部分组。 ConfigurationManager 不提供获取分区组的方法。

有很多方法可以在没有ConfigurationManager 的情况下获得Configuration,但这有点混乱,因为您必须区分app.config 和web.config。

但是如果您可以跳过system.ServiceModel 到您想要的实际配置组,那么这真的很容易,因为您可以使用ConfigurationManager。例如,

var section = ConfigurationManager.GetSection("system.serviceModel/client");

或者你可以使它成为强类型:

var section = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");

var behaviorSection = 
    (BehaviorsSection)ConfigurationManager.GetSection("system.serviceModel/behaviors");

【讨论】:

  • 我想使用 config.app 中定义的 WCF 客户端配置。它不是读取/访问用户定义的值/组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2012-01-02
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
相关资源
最近更新 更多