2013-03-13 39 views
0

我正在使用只能通過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); 
    } 
} 

回答

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什麼URL。

+0

你能詳細解釋一下嗎?該代碼段是否進入類庫或引用它的項目? – Logarr 2013-03-13 14:37:14

+0

@記者 - 編輯 - 這有幫助嗎?它在你所在的類庫中(大概)已經在打電話了。 – Bobson 2013-03-13 15:01:13

+0

不是真的......我編輯了這個問題來展示我現在如何調用API的示例。在這種情況下,我看不出如何應用您的方法。 – Logarr 2013-03-13 15:16:04