【问题标题】:Referencing a class library without having to reference the library's service references引用类库而不必引用库的服务引用
【发布时间】:2013-03-01 12:41:48
【问题描述】:

我正在使用只能通过 Web 和服务引用访问的第 3 方系统。在构建处理自动化处理的 Windows 服务时,我发现如果解决方案使用 Web/服务引用,则还必须在引用第一个的任何解决方案中设置它们。

有没有办法防止这种情况发生?我想创建一个类库来保存所有实际的 API 调用并将其用作 NuGet 包,而不必同时添加对每个项目的引用。

编辑:这是我当前如何调用 API 的示例:

internal class ApiAccess
{
    private readonly Account_SSPSoapClient _apiAccount;

    public ApiAccess()
    {
        _apiAccount = new Account_SSPSoapClient();
    }

    public string GetAccountId(string accountName)
    {
        return _apiAccount.GetID(accountName);
    }
}

【问题讨论】:

    标签: c# nuget service-reference


    【解决方案1】:

    这是一个问题,但不是看起来的问题。

    您实际上并不需要所有使用您的代码的项目中的服务引用 - 您所需要的只是 app.config 中的一些信息。具体来说,绑定和端点地址。您可以将它们硬编码到您的代码中,然后您应该能够很好地引用它。

    最简单的情况:

    var request = new MyServiceRequest { /* set properties here */ };
    var client  = MyServiceReferenceClient(new BasicHttpBinding(), new EndpointAddress(@"https://my.service.com/path/to/service"));
    var channel = client.ChannelFactory.CreateChannel();
    var result  = channel.MyService(request);
    

    您需要在 BasicHttpBinding 上设置一些参数以匹配 app.config 文件中的内容,并且 URL 也来自那里。

    请参阅 this answer 了解为什么默认情况下它不起作用。


    编辑:对于您的代码,您只需将 new Account_SSPSoapClient(); 替换为以下内容:

    new Account_SSPSoapClient(new BasicHttpBinding(), new EndpointAddress(@"https://my.service.com/path/to/service"));
    

    其他一切都应该相同,但它将使用这些值而不是 app.config 值(这是它在没有参数的情况下所做的)。

    在 app.config 文件中查找类似的内容:

        <bindings>
            <basicHttpBinding>
                <binding name="LabelRecoveryBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
    

    其中的所有内容都对应于您可以在上面创建的 BasicHttpBinding 对象上设置的属性 - 其中大部分是默认值,但您可能希望手动设置所有内容以确保安全。

    同样,寻找

    <client>
            <endpoint address="http://153.2.133.60:48010/xoltws_ship/LBRecovery"
                binding="basicHttpBinding" bindingConfiguration="LabelRecoveryBinding"
                contract="UPSLabelRecoveryService.LabelRecoveryPortType" name="LabelRecoveryPort" />
    </client>
    

    这告诉您要为new EndpointAddress 提供什么网址。

    【讨论】:

    • 您能稍微详细说明一下吗?那个代码 sn-p 是进入类库还是引用它的项目?
    • @Logarr - 已编辑 - 有帮助吗?它位于您(可能)已经在进行调用的类库中。
    • 不是真的...我已经编辑了这个问题,以展示我现在如何调用 API 的示例。在这种情况下,我看不出如何应用您的方法。
    • @Logarr - 啊,这有助于澄清。已编辑。
    • 这更有意义。这并不是说我要完全替换 app.config,而是以编程方式覆盖它。谢谢!
    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多