2013-03-28 79 views
0

我編寫了一個在DLL中引用的WCF服務。 在我添加了服務引用後,一個app.config自動生成了需要的數據。引用的WCF服務不使用app.config

客戶端正在使用dll與wcf服務進行通信......沒有什麼特別的。 但是,當我試圖創建一個服務引用的對象,它崩潰,說它無法找到一個端點地址。

我用Google搜索周圍,通過passsing的綁定和地址,該服務的參考固定它:

readonly BasicHttpBinding _binding = new BasicHttpBinding(); 
readonly EndpointAddress _address = new EndpointAddress("http://localhost:50309/CustomerService.svc"); 

using (CustomerServiceClient client = new CustomerServiceClient(_binding, _address)) 
{ 
    return client.GetActions(customerNumber); 
} 

現在我想知道,爲什麼我要傳遞的參數,當這些數據已經在自動生成app.config。 我刪除了app.config的內容......並且似乎這些數據不會在任何地方使用。

我做錯了什麼?

編輯:在dll項目

應用程序配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_ICustomerService" 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> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:50309/CustomerService.svc" 
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerService" 
      contract="ServiceReference.ICustomerService" name="BasicHttpBinding_ICustomerService" /> 
    </client> 
</system.serviceModel> 
</configuration>  
+0

請包括你的app.config。另外我假設app.config是爲你的入口點.exe – Aron 2013-03-28 09:13:12

+0

你確定你的DLL不使用你的客戶端的app.config?或者你在談論那個DLL的app.config? – 2013-03-28 09:13:15

+0

如果你的dll看起來像app.config,你最有可能使用錯誤的。 – 2013-03-28 09:21:44

回答

3

請注意以下幾點:的DLL可以有自己的app.config

如果你想在DLL使用的配置值,一般使用屬性的DLL項目創建它們,但隨後從DLL的app.config複製設置部分到EXE的app.config。此外,您需要複製各自的sectionGroup條目。

DLL將使用應用程序的exe.config文件中的設置。

app.config一個EXE項目,對於應用程序和DLL都提供了設置的例子:

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="ExeProject" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
     <section name="DllProject" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 

    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 

    <applicationSettings> 
    <ExeProject> 
     <setting name="..." serializeAs="String"> 
     <value>...</value> 
     </setting> 
    </ExeProject> 

    <DllProject> 
     <setting name="..." serializeAs="String"> 
     <value>...</value> 
     </setting> 
    </DllProject> 

    </applicationSettings> 
</configuration> 
+0

......或者如果「客戶端」是網站,則在Web.config文件中。 – 2013-03-28 10:15:53

+0

mhh,聲音合乎邏輯。我嘗試過,但沒有運氣。我的服務引用在創建沒有參數的服務對象時仍然崩潰。可能我的配置文件看起來不正確。我用dll和exe項目的名稱替換了部分名稱和applicationsettings成員,並將部分複製到了dll項目部分..有什麼錯誤? – Jan 2013-03-28 10:17:08

+1

''部分需要位於全局級別的應用程序'app.config'中。 'applicationSettings'節不能包含'serviceModel'節。 – 2013-03-28 10:24:42

0

因爲您的DLL 無法自身來看,它需要一個可執行文件從使用(無論是Windows服務,Web服務,普通桌面應用程序等)。正因爲如此,dll使用它們使用的可執行文件的.config文件 - 這就是爲什麼你的dll似乎忽略了它的配置。