2009-04-14 66 views
4

我現在有一對夫婦的項目的解決方案,其中之一是一個WCF服務訪問的共享庫的單一的app.config。我創造了另一個預測與靜態類,基本上提供了一個通往WCF客戶端的情況下,像這樣的:C#WCF:具有在提供服務

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

這是使(或因此我認爲)我可以用一個單一的app.config文件,將只能在該庫中,然後其他項目纔可以引用它並從該屬性獲取對該客戶端的引用。

但問題是,當一個項目試圖訪問該屬性,拋出一個異常,告訴我,我需要app.config應用程序,當我的app.config了我的門戶庫複製到應用程序,它的工作原理。


有沒有一種方法,以避免在應用程序的多個app.config文件,僅具有一個可能在單個庫?


[更新]解決方案:

Anderson Imes的建議,現在我決定硬編碼在類客戶參考配置,從而消除了多個app.config S上的需要。

因此,我翻譯我從這裏(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> 

爲了這個(一個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(); 
    } 
} 
+2

感謝您發佈您的解決方案......這將有助於其他人嘗試作出此決定。 – 2009-04-15 15:27:25

+0

感謝您的提示。您的示例被截斷,所以我粘貼下面的解決方案(http://stackoverflow.com/questions/746107/c-wcf-having-a-single-app-config-in-a-shared-library-that-provides-訪問到/ 7652518#7652518) – 2011-10-04 18:40:25

+0

不知道爲什麼代碼被截斷。無論如何,這是它應該看起來如何:http://pastebin.com/8jii24JV – 2011-10-05 14:25:02

回答

3

你得到這個錯誤的原因是,對於一個默認的構造函數WCF客戶端代理從本地配置查找通道配置。您可以通過指定要使用/連接到的綁定和地址來覆蓋此行爲。

您有幾種選擇在這裏,每一個不同的部署模式。

  1. 對您的「網關」庫(通用術語「代理」)中的端點信息進行硬編碼。你只需要返回新的DBInteractionGatewayClient(binding,address);對於這個解決方案,你只分發組件您WSGateway代碼是(以下簡稱「WSGateway大會」。
  2. 創建的所有網站都可以使用公共配置文件,如果這些都在同一臺機器上的所有服務,這是容易做到的。將配置數據在一個共同的驅動器位置並從那裏讀它。如果你想可能WCF配置的全域可用,你需要使用ConfigurationManager.OpenMappedExeConfiguration方法和手動閱讀並手動應用到您打開客戶端通道之前,你的綁定。爲此,您將確保您位於市中心的配置文件是訪問和分發您WSGateway大會。
  3. 將您的配置,從所有的應用程序,如數據庫訪問的公共資源。這將允許你訪問這個公司從解決方案中的任意位置獲取配置數據。對於這個解決方案,您可以確保您的配置數據庫可以從解決方案中的所有點訪問,並分發您的WSGateway程序集。

這些都是我能想到的解決方案。讓我們知道你決定做什麼。

0

Andreas的答案以「p」結尾,所以我認爲某些東西沒有得到正確的複製和粘貼。但它讓我走上了正軌,而我想出了這個道理。我從MS(計算器)的WCF例子開始。

這是一個客戶端應用程序

 CalculatorClient client = new CalculatorClient(); 

這是不需要的app.config硬編碼的版本使用的app.config舊方法。我再次檢查了綁定部分中app.config中的所有值都是默認值,並且不需要顯式複製。但是,您可以將所有這些值直接添加到binding的屬性中。

 string address = "http://localhost:8000/ServiceModelSamples/Service/CalculatorService"; 
     WSHttpBinding binding = new WSHttpBinding(); 
     binding.Name = "WSHttpBinding_ICalculator"; // not sure if this is necessary. 
     EndpointAddress endpointAddress = new EndpointAddress(address); 
     CalculatorClient client = new CalculatorClient(binding, endpointAddress); 
     return client; 

順便說一句,我不知道,我與所有這些綁定在做,我只是想獲得它的工作!我剛剛瞭解到了WCF昨天...我想把整個接口放入一個DLL中,並且不想複製或合併app.config和GUI前端。