簡體   English   中英

引用類庫而不必引用庫的服務引用

[英]Referencing a class library without having to reference the library's service references

我正在使用只能通過Web和服務引用進行訪問的第三方系統。 在構建處理自動化處理的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);
    }
}

這是一個問題,但似乎不是問題。

實際上,在所有使用代碼的項目中實際上都不需要服務引用-您所需的只是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也從那里出現。

有關為什么默認情況下不起作用的信息,請參見此答案


編輯:對於您的代碼,您只需替換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 URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM