繁体   English   中英

我的RESTUL WCF无法正常工作

[英]My RESTUL WCF doesn't work properly

我有以下几点:

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实例。

感谢您发布肯定有帮助的代码。 但是我认为您将需要显示更多的工作,以及一些有关项目失败的具体结果。 但是,以免让您无助。 我建议看提琴手

http://www.telerik.com/fiddler

它允许您创建Http请求并在其控制台内部查看响应。 这对于明确查看端点返回的http响应代码很有用,并允许您通过composer窗口修改请求。

另一个有用的技巧是,逐步完成代码,因此您可以在方法完成之前向我们指出确切的行失败或设置的值。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM