2011-12-13 88 views
2

我想讓我的第一個WCF服務。 我有問題 - 我寫了一個結構,一個類和幾個方法。 我的服務合同是這樣的:wcf服務不工作 - 未能添加服務

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace WebshopReports 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReports" in both code and config file together. 
    [ServiceContract] 
    public interface IReports 
    { 
     [OperationContract] 
     List<Category> getReportCategories(string dir); 

     [OperationContract] 
     List<CReport> getReportList(Category category); 

    } 
} 

當我使用測試客戶端在Visual Studio中運行它,我得到如下:

無法添加服務。服務元數據可能無法訪問。使 確定您的服務正在運行並展示元數據。

我確定它與我的web.config文件有關。在下面。是否有人可以幫助我,我不能確定如何配置web.config中:

<?xml version="1.0"?> 
<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> 

奇怪的是,如果我改變從一類CReport一個struct,它會工作。

回答

0

您需要確保CReport是可序列化的,否則WCF不可能創建元數據以交換(通過您的案例中的WSDL)關於您的服務。請閱讀將DataContract屬性添加到您的CReport課程。

+0

也許我看着我的解決方案錯誤的方式。我想創建一個服務來接受參數,運行水晶報表並返回數據。 WSDL服務是解決這個問題的最佳方法嗎? – Lock 2011-12-14 00:56:19

1

根據我們的經驗,也有這種類型的故障的兩個主要原因:

1)在服務器端序列化。這可能是由於不可序列化的類或試圖在不使用KnownType屬性的情況下序列化抽象類。

2)由於WCF在具有相同通用簽名的多個集合類型或字典類型類之間的混淆,客戶端的序列化。

根據您的描述,聽起來像項目1是您的問題。

解決WCF之類的問題,這是下面塊添加到您的web.config文件中的配置節的最佳方式:

<system.diagnostics> 
    <sources> 
     <source name="System.ServiceModel" 
       switchValue="Information, ActivityTracing" 
       propagateActivity="true"> 
     <listeners> 
      <add name="traceListener" 
       type="System.Diagnostics.XmlWriterTraceListener" 
       initializeData="c:\log\WebTrace.svclog" /> 
     </listeners> 
     </source> 
    </sources> 
    </system.diagnostics> 

一旦你嘗試使用界面和失敗,雙點擊svclog文件,這將打開WCF的日誌查看器工具。任何發生的異常都會以紅色突出顯示,您可以深入查看導致WCF服務失敗的特定問題。

0

仔細檢查您是否已將CReport標記爲可序列化。這將允許WCF序列化程序公開WCF元數據中的類定義。

0

如果我沒有弄錯。我無法找到您的配置中的任何服務節點。 你應該inlude像這樣在您的配置文件

<configuration> 
     <System.ServiceModel> 
     <Services> 
     <Service name="YourService.Somthing"> 
     <EndPoint name="default" address="" binding="basicHttpBinding" bindingConfiguration="" contract="YourContrat.Something"></Endpoint> 
     <endpoint kind="mexEndpoint" address="/mex" /> 
     </Service> 
     </Services> 
<System.ServiceModel> 
</configuration> 

希望這有助於。

謝謝

1

看看我的網頁。配置,我認爲你的配置文件是錯誤的:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="NoneSecurity" 
      maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false"> 
      <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/> 
      <security mode="None"/> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="Elong.GlobalHotel.Host.IHotelAPIBehavior" 
     name="Elong.GlobalHotel.Host.IHotelAPI"> 
     <endpoint address="" binding="wsHttpBinding" bindingConfiguration="NoneSecurity" contract="Elong.GlobalHotel.Host.IIHotelAPI"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="Elong.GlobalHotel.Host.IHotelAPIBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel>