2014-04-17 52 views
0

我有以下內容:我RESTUL WCF不能正常工作

In Competitions.svc: 

<%@ ServiceHost Language="C#" Debug="true" Service="MySite_WebSite.Pages.Client.CompetitionsSVC" CodeBehind="Competitions.svc.cs" %> 

在ICompetitions.cs:

namespace MySite_WebSite.Pages.Client 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICompetitions" in both code and config file together. 
    [ServiceContract(Name="CompetitionsSVC")] 
    public interface ICompetitions 
    { 
     [OperationContract] 
     [WebInvoke(
      Method = "GET" 
      , RequestFormat = WebMessageFormat.Json 
      , ResponseFormat = WebMessageFormat.Json 
      , UriTemplate = "DoWork" 
      , BodyStyle=WebMessageBodyStyle.Wrapped 
     )] 
     Dictionary<DateTime, List<Competitions.Entry>> DoWork(); 
    } 
} 

在Competitions.svc.cs:

namespace MySite_WebSite.Pages.Client 
{ 
    [DataContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
    public class CompetitionsSVC : ICompetitions 
    { 
     #region ICompetitions Members 

     public Dictionary<DateTime, List<Competitions.Entry>> DoWork() 
     { 
      var c = new Competitions(); 

      return c.GetMonthlyEntries(new Competitions.ParamGetMonthlyEntries() 
      { 
       Start = DateTime.Now.Date.AddMonths(-1) 
       , End = DateTime.Now.Date.AddMonths(2) 
       , UserLang = "fr-BE" 
       , ActiveLang = "fr-BE" 
       , IsExternal = false 
      }); 
     } 

     #endregion 
    } 
} 

在Web.config:

<system.serviceModel> 
    <services> 
     <service name="MySite_WebSite.WS.WCF.SubsetMID"> 
     <endpoint address="" 
        binding="wsHttpBinding" 
        contract="MySite_WebSite.WS.WCF.ISubsetMID" /> 

     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
     <service name="MySite_WebSite.Pages.Client.CompetitionsSVC"> 
     <endpoint address="" 
        binding="webHttpBinding" 
        behaviorConfiguration="WebBehavior" 
        contract="MySite_WebSite.Pages.Client.ICompetitions" /> 

     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <bindings> 
     <wsHttpBinding> 
     <binding> 
      <security mode="None"/> 
     </binding> 
     </wsHttpBinding> 
     <netTcpBinding> 
     <binding name="NetTcpBinding_IServiceWCallBack" sendTimeout="00:10:00" 
      maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> 
      <readerQuotas maxStringContentLength="2147483647" /> 
      <security mode="None" /> 
     </binding> 
     <binding name="NetTcpBinding_IHandleSubset"> 
      <security mode="None" /> 
     </binding> 
     </netTcpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="WebBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment 
     multipleSiteBindingsEnabled="true" 
     aspNetCompatibilityEnabled="true" 
    /> 
    </system.serviceModel> 

當我輸入網址

localhost2/MySite_WebSite/Pages/Client/Competitions.svc/DoWork 

,這是行不通的。 我在方法開始時有一個斷點,我可以看到該方法被調用兩次,但它不返回任何東西(我甚至不認爲它發送任何HTTP代碼後退)。

我做錯了什麼?

其他備註:

Entry實際上是 「基類」。

public class EntryCompetition : Entry 
    public class EntryEvent : Entry 

在我的代碼字典實際上包含EntryCompetitionEntryEvent實例。

+0

我添加了「附加註釋」 –

回答

0

感謝您發佈您的代碼,絕對有幫助。但是我認爲你需要展示更多的工作,以及關於你的項目失敗的更多具體結果。但是,以免讓你無奈。我建議在看提琴手

http://www.telerik.com/fiddler

它允許您創建HTTP請求和看到的答覆裏面的控制檯。它特別有助於查看您的端點返回的http響應代碼,並允許您通過作曲者窗口修改您的請求。

另一個有用的提示將是一步一步通過你的代碼,所以你可以指出我們到底是什麼行失敗或在你的方法完成之前設置了什麼值。

沒有更多的信息,我最好的猜測是你的代碼正在拋出,並且很可能吞嚥了一個異常。或者您的方法或調用設置的空值不會返回您所期望的值。如果您仍然遇到問題,請在您設置一些進一步的測試並更新您的問題後回覆。

+0

結果就像服務器根本沒有響應。沒有404或什麼如此。例如,當我用firefox發出請求時,它最終會追加www。和.com到「localhost2」。 –

+0

[Fiddler] ReadResponse()失敗:服務器未返回此請求的響應。服務器返回0字節。 (注意:我將browser.fixup.alternate.enabled設置爲false,以防止+ www。+ .com) –

+0

也許我誤解了,但是您能否確認在撥打電話時進入了您的代碼。這聽起來像你可以調試和打破點。 – Madullah

0

好的,我解決了這個問題。我使用自定義的代碼將字典序列化成JSON字符串,並且不再使用DateTime對象作爲關鍵字。