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

网络配置

    <?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>
4

2 回答 2

0

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

编辑:您的GetEvent方法也[OperationContract]应用了一个属性,并且根据您的描述它有效吗?

于 2012-04-17T06:33:38.243 回答
0

您是否考虑过将 asp.net mvc 用于您的休息服务?这真的很简单,安全和快速。这肯定会让你的生活变得更好。像您在此处发布的服务这样的服务可以在大约 20 分钟内完成,代码不超过 10 行。希望能帮助到你。

于 2012-06-22T15:20:22.317 回答