我試圖用WCF REST服務模板40和實體框架來構建休息JSON服務。WCF REST服務模板40和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>
您是否試圖在返回之前調試GetEvent2方法以檢索從數據庫檢索的內容。另外檢查你是否將懶加載設置爲true,並嘗試將延遲加載設置爲false,然後將你的Event對象加載爲JSON數據約定序列化程序不會序列化具有內部集合的實體框架對象。 – Rajesh 2012-04-11 08:22:35
更多的信息在這裏:http://connect.microsoft.com/VisualStudio/feedback/details/354859/entity-framework-entitycollection-and-datacontractjsonserializer(或)http://social.msdn.microsoft.com/Forums/en -US/adodotnetentityframework/thread/9bff1c80-41e3-483a-8762-304efa630328 – Rajesh 2012-04-11 08:22:55
你有沒有想過這個?我有同樣的問題。 – fuzz 2012-04-17 01:02:00