我的RESTUL WCF无法正常工作

时间:2014-04-17 12:44:06

标签: c# 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实际上是&#34;基类&#34;。

    public class EntryCompetition : Entry
    public class EntryEvent : Entry

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

2 个答案:

答案 0 :(得分:0)

感谢您发布明确有用的代码。但我认为你需要展示更多的工作,以及一些关于项目失败的具体结果。但是为了不让你无助。我建议看Fiddler

http://www.telerik.com/fiddler

它允许您创建Http请求并查看其控制台内部的响应。它特别适用于查看端点返回的http响应代码,并允许您通过编辑器窗口修改请求。

另一个有用的提示,就是逐步完成代码,这样你就可以指出我们在方法完成之前确切地说明了哪一行失败或者设置了什么值。

如果没有更多信息,我最好的猜测是您的代码正在抛出并且很可能吞噬异常。或者您的方法或调用设置的空值不会返回您期望的值。如果您仍然遇到问题,请在设置一些进一步的测试后回复并更新您的问题。

答案 1 :(得分:0)

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