共享库中的 C# WCF : Having a single app. 配置提供对服务的访问

标签 c# wcf reference

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

public static class WSGateway
{
    public static DBInteractionGatewayClient MR_WebService
    {
        get
        {
            return new DBInteractionGatewayClient();
        } 
    }
}

这样(或者我认为)我可以使用一个单独的 app.config 文件,该文件将仅在该库中,然后其他项目可以只引用它并获得对该属性(property)的那个客户。

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


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


[更新]解决方案:

正在关注 Anderson Imes ' 建议,现在我决定在类中对客户端引用配置进行硬编码,从而消除了对多个 app.config 的需要。

因此,我从这个 (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> 

对此(静态类):

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

最佳答案

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

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

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

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

关于共享库中的 C# WCF : Having a single app. 配置提供对服务的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/746107/

相关文章:

c# - 更改冲突的程序集引用

C# 在继承的构造函数中初始化 Disposable

c# - 如何使用 Interface Builder 在 UIButton 或 UIBarButtonItem 上添加 BgImage?

c# - 更改端点地址后如何修复 'Unrecognized Message Version'

c# - WCF 和 Windows 身份验证

Perl 对数组的引用数组进行排序

c# - 为什么在调用 HtmlHelper.AntiForgeryToken 时会收到 TypeInitializationException?

c# - 如何在 C# 中从指向内存流的 int 指针获取数据?

wcf - ASP.NET、SilverLight、WCF 和表单例份验证 - 如何配置端点?

c - C 中 Void 函数在 vector 上的返回