WCF REST服务模板40和JSON格式的实体框架返回空响应

时间:2012-04-11 07:19:06

标签: asp.net json wcf rest entity-framework-4

我尝试使用WCF REST服务模板40和实体框架构建Rest JSON服务。

当我尝试创建常规对象并返回它时,一切正常,我得到了JSON响应(GetEvent方法)

当我尝试以XML格式返回从实体框架调用的对象时,一切正常(GetEvent3方法)

但是当我尝试从实体框架调用的对象返回Json响应时,我得到空响应(GetEvent2方法)

知道为什么吗?

这是我的代码:

    [DataContract]
public class Event
{
    [DataMember]
    public int ID { get; set; }

    [DataType(DataType.Text)]
    [Display(Name = "Event Name")]
    [DataMember]
    public string Name { get; set; }

    [Display(Name = "Event StartTime")]
    [DataMember]
    public DateTime StartTime;

    [Display(Name = "Event EndTime")]
    [DataMember]
    public DateTime EndTime;
}




[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "/GetEvent", ResponseFormat = WebMessageFormat.Json)]
    Event GetEvent();

    [WebGet(UriTemplate = "/GetEvent2", ResponseFormat = WebMessageFormat.Json)]
    Event GetEvent2();

    [WebGet(UriTemplate = "/GetEvent3", ResponseFormat = WebMessageFormat.Xml)]
    Event GetEvent3();
}



[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1
{
    private readonly EventsDB _db = new EventsDB();

    // This method return good response in Json format
    [WebGet(UriTemplate = "/GetEvent", ResponseFormat = WebMessageFormat.Json)]
    public Event GetEvent()
    {
        return new Event
        {
            ID = 1,
            Name = "A vs. B",
            StartTime = new DateTime(2012, 4, 10, 18, 00, 00),
            EndTime = new DateTime(2012, 4, 11, 18, 00, 00),
        };
    }

    // This method return no response
    [WebGet(UriTemplate = "/GetEvent2", ResponseFormat = WebMessageFormat.Json)]
    public Event GetEvent2()
    {
        Event e = _db.Events.ToList()[0];
        return e;
    }

    // This method return good response in XML format
    [WebGet(UriTemplate = "/GetEvent3", ResponseFormat = WebMessageFormat.Xml)]
    public Event GetEvent3()
    {
        Event e = _db.Events.ToList()[0];
        return e;
    }
}

的Web.Config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <connectionStrings>
    <add name="EventsDB"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=EventsDB"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="WcfRestService3.Service1">
        <endpoint address="http://localhost:50235/service1"
              binding="webHttpBinding"
              contract="WcfRestService3.IService1" />
      </service>
    </services>
  </system.serviceModel>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>


</configuration>

2 个答案:

答案 0 :(得分:0)

您是否遗漏了[OperationContract]方法的GetEvent2属性? 我可以看到“示例”部分@ http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.responseformat%28v=vs.90%29.aspx

中提到了该属性

编辑:您的GetEvent方法也应用了[OperationContract]属性,根据您的说明,它有效吗?

答案 1 :(得分:0)

您是否考虑过使用asp.net mvc进行休息服务?它非常简单,安全,快捷。这是确保让你的生活更美好的事情。您在此处发布的服务可以在大约20分钟内完成,不超过10行代码。希望它有所帮助。