2010-09-28 63 views
3

我學習WCF和現在我得到一個例外,在運行應用程序: 例外:原因此異常WCF

找不到默認端點 元素引用合同 「IService1」在ServiceModel客戶端 配置部分。這可能是 ,因爲沒有爲您的應用程序找到配置文件 ,或者因爲在客戶端 元素中找不到與此 合約匹配的端點元素 。

服務代碼:

namespace StockService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class Service1 : IService1 
    { 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite == null) 
      { 
       throw new ArgumentNullException("composite"); 
      } 

      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 

      return composite; 
     } 

     public string GetCompositedata() 
     { 
      CompositeType ct = new CompositeType(); 
      return ct.StringValue; 
     } 
    } 
} 

的web.config:

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

現在使用SvcUtil工具我已經產生proxyclass(generatedProxy.cs)和配置文件(serviceapp.config),並將其添加(客戶端)

客戶端:

Service1Client sc = new Service1Client(); 
Console.WriteLine(sc.GetCompositedata()); 
Console.ReadKey(); 

配置:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IService1" 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:2614/Service1.svc" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_IService1" contract="IService1" 
       name="BasicHttpBinding_IService1" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

我無法弄清楚爲什麼我收到此異常。

請幫

回答

6

如果是網站/網絡應用程序,那麼該配置是否顯示爲客戶端應用程序的app.configweb.config的客戶端部分?

您需要將這些部分包含在應用程序的配置中 - 僅將這些配置放在由svcutil.exe創建的單獨文件中是不夠的 - 它需要成爲應用程序配置的一部分。

+0

它是一個控制檯應用程序(客戶端) – Wondering 2010-09-28 15:43:12

+1

好了,現在正在工作,在控制檯應用程序中添加一個app.config,並將代碼(從svcutil生成)添加到該文件,現在正在工作。謝謝。 – Wondering 2010-09-28 15:45:57

2

的SvcUtil工具生成的配置需要進入你的主要的app.config,不包括原樣。

您需要將app.config添加到您的項目中,然後將svcutil config的內容合併到配置部分。

+0

謝謝RUP,你的幫助。 – Wondering 2010-09-28 15:47:32