我的RESTUL WCF无法正常工作。 [英] My RESTUL WCF doesn't work properly.

查看:53
本文介绍了我的RESTUL WCF无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:



在Competitions.svc:

I have the followings:

In Competitions.svc:

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





在ICompetitions.cs中:



In 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中:



In 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中:





In 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>







当我输入网址




When I enter the url

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

,它不起作用。

我在方法的开头有一个断点,我可以看到该方法被称为两次,但它没有返回什么(我甚至不认为它发送任何HTTP代码)。



我做错了什么?

, it doesn't work.
I have a breakpoint at the begining of the method, and I can see the method gets called twice, yet it doesn't return anything (I don't even think it send any HTTP code backs).

What did I do wrong?

推荐答案

这不是解决方案,但看起来我无法显示代码在评论中这是我认为最好的方式来表达我的问题。



你确定你的代码:

This is not a solution but it looks like i can't show code in a comment so this was the way i thought was best to show my question.

are you sure your code:
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
});





返回值?



如果返回null,它没有看到任何东西。



你可以尝试做类似的事情:



returns a value?

if that returns null its normal that you don't see anything.

can you try to do something like:

Dictionary<datetime,>> dictionaryList = 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
});

return dictionaryList;





放一个返回语句的断点。

当程序停止时检查dictionaryList是否为空或空



put a break point on that return statement.
and when the program stops check if dictionaryList is not null or empty


好的,我解决了这个问题。



我使用自定义代码片段将字典序列化为JSON字符串,我不再使用DateTime对象作为键(因为它们不是JSON字典的有效键)。



Ok, I solved the problem.

I use a custom piece of code to serialize the dictionnry into a JSON string and I don't use DateTime objects as keys anymore (as those aren't valid keys for a JSON dictionary).

public Stream Test()
{
    var c = new Competitions();
    var result = 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
    }).ToDictionary(
        kp => kp.Key.ToString("yyyy-MM-dd")
        , kp => kp.Value
    );

    var javaScriptSerializer = new JavaScriptSerializer();
    var json = Encoding.UTF8.GetBytes(javaScriptSerializer.Serialize(result));
    var memoryStream = new MemoryStream(json);
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    return memoryStream;
}


这篇关于我的RESTUL WCF无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆