C#WCF:在共享库中有一个app.config,提供对服务的访问

And*_*ech 5 c# wcf reference

我目前有一个包含几个项目的解决方案,其中一个是WCF服务.我创建了另一个使用静态类的投影,它基本上提供了一个WCF客户端实例的网关,如下所示:

public static class WSGateway
{
    public static DBInteractionGatewayClient MR_WebService
    {
        get
        {
            return new DBInteractionGatewayClient();
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

这是(或者我认为)我可以使用app.config仅在该库中的单个文件,然后其他项目可以只引用它并从该属性获取对该客户端的引用.

但问题是,当一个项目试图访问该属性时,会抛出一个异常,告诉我我需要app.config在应用程序中,当我将app.config我的网关库复制到应用程序时,它可以工作.


有没有办法避免app.config在应用程序中有多个文件,并且只有一个可能是一个库?


[更新]解决方案:

按照Anderson Imes的建议,现在我决定对类中的客户端引用配置进行硬编码,从而消除了对多个app.configs的需求.

因此,我从this(app.config)翻译了我的配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IDBInteractionGateway" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="6000000"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <security mode="None"/>
                    <readerQuotas maxDepth="6000000" maxStringContentLength="6000000" maxArrayLength="6000000"
                        maxBytesPerRead="6000000" maxNameTableCharCount="6000000" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://agnt666laptop:28666/DBInteractionGateway.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDBInteractionGateway"
                contract="DBInteraction_Service.IDBInteractionGateway" name="WSHttpBinding_IDBInteractionGateway">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration> 
Run Code Online (Sandbox Code Playgroud)

对此(a static class):

public static class WSGateway
{
    private static WSHttpBinding binding;
    private static EndpointAddress endpointAddress;

    static WSGateway()
    {
        var readerQuotas = new XmlDictionaryReaderQuotas()
        {
            MaxDepth = 6000000,
            MaxStringContentLength = 6000000,
            MaxArrayLength = 6000000,
            MaxBytesPerRead = 6000000,
            MaxNameTableCharCount = 6000000
        };
        binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};

        endpointAddress = new EndpointAddress("http://agnt666laptop:28666/DBInteractionGateway.svc"); 
    }
    public static DBInteractionGatewayClient MR_WebService
    {
        get
        { 
            return new DBInteractionGatewayClient(binding, endpointAddress);
        }
    }
    public static void ExecuteCommand(Action<DBInteractionGatewayClient> command)
    {
        var ws = MR_WebService;
        command.Invoke(ws);
        ws.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*mes 3

出现该错误的原因是 WCF 客户端代理的默认构造函数从本地配置查找通道配置。您可以通过指定要使用/连接的绑定和地址来覆盖此行为。

这里有多个选项,每个选项都有不同的部署模型。

  1. 将端点信息硬编码在“网关”库中(常用术语是“代理”)。您只需返回 new DBInteractionGatewayClient(binding, address); 对于此解决方案,您只需分发 WSGateway 代码所在的程序集(以下称为“WSGateway 程序集”)。
  2. 创建所有站点都可以访问的通用配置文件。如果这些服务都在同一台机器上,那么这很容易做到。将配置数据放置在共享的公共驱动器位置并从那里读取。如果您希望所有可能的 WCF 配置可用,则需要使用 ConfigurationManager.OpenMappedExeConfiguration 方法并手动读取它,然后在打开客户端通道之前将其手动应用到您的绑定。为此,您需要确保可以访问位于中心的配置文件并分发您的 WSGateway 程序集。
  3. 将您的配置移至可从所有应用程序访问的公共资源,例如数据库。这将允许您从解决方案中的任何点访问此配置数据。对于此解决方案,您需要确保可以从解决方案中的所有点访问您的配置数据库并分发您的 WSGateway 程序集。

这些是我能立即想到的解决方案。让我们知道您决定做什么。