WCF 客户端实现,有没有办法在 class 库 app.config 中保留服务和端点详细信息,而不是 ASP.NET MVC 应用程序的 web.config

WCF Client Implementation, is there way to keep services and end-point detail in class library app.config rather then web.config for ASP.NET MVC app

我有 .NET 项目,其中有 APP.Client.Proxy class 库项目,负责为客户端实现 WCF,所以所有服务和端点配置细节都在那里,然后我有另一个 ASP.NET MVC 项目使用这些服务实现,即来自控制器。现在,如果我将 app.config 库项目的服务和端点详细信息放入 class 库项目,则它不起作用,抱怨找不到端点,但如果我将端点配置详细信息放入 web.config 用于 MVC 网络应用程序。

我的问题是如何将服务和端点详细信息分开保存在 class 库的 app.config 中,而不是 web.config

应用域在执行时使用一个配置文件。而应用程序设置和连接字符串部分允许分别引用外部文件和 configSource。

在快速 GoogleFu 之后,我发现了这两篇文章

http://weblogs.asp.net/cibrax/configsource-attribute-on-system-servicemodel-section

https://blogs.msdn.microsoft.com/youssefm/2009/11/03/separating-out-wcf-configuration-into-multiple-files-with-configsource/

关键在于你不能将它留在 class 库配置中,但你仍然可以将它们分离到外部配置文件中。

<configuration>
  <!-- other code removed for brevity -->  
  <system.serviceModel>
    <services configSource="Services.config" >

    </services>
    <bindings configSource="Bindings.config">

    </bindings>
    <behaviors configSource="Behaviors.config">

    </behaviors>
  </system.serviceModel>
  <!-- other code removed for brevity -->
</configuration>