我的RESTUL WCF没有';工作不正常

本文关键字:工作 不正常 RESTUL WCF 没有 我的 | 更新日期: 2023-09-27 17:58:50

我有以下内容:

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();
    }
}

在竞争中.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>

当我输入url 时

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

,它不起作用。我在方法的开头有一个断点,我可以看到该方法被调用了两次,但它没有返回任何内容(我甚至不认为它会发回任何HTTP代码)。

我做错了什么?

附加说明:

Entry实际上是一个"基类"。

    public class EntryCompetition : Entry
    public class EntryEvent : Entry

在我的代码中,字典实际上包含EntryCompetitionEntryEvent实例。

我的RESTUL WCF没有';工作不正常

感谢您发布的代码,这无疑会有所帮助。但我认为你需要展示更多的工作,以及一些关于你的项目如何失败的更具体的结果。但为了不让你孤立无援。我建议查看Fiddler

http://www.telerik.com/fiddler

它允许您创建Http请求,并在控制台中查看响应。它有助于具体查看端点返回的http响应代码,并允许您通过composer窗口修改请求。

另一个有用的提示是逐步完成您的代码,这样您就可以在方法完成之前准确地指出哪一行失败了,或者设置了哪些值。

在没有更多信息的情况下,我的最佳猜测是你的代码正在抛出并很可能吞下一个异常。或者,您的方法或调用正在设置null值,这些值不会返回您期望的值。如果您仍然有问题,请在设置了一些进一步的测试并更新了您的问题后回复。

好的,我解决了这个问题。我使用一段自定义代码将dictionary序列化为JSON字符串,并且不再使用DateTime对象作为键。