WCF REST Service Template 40 と Entity FrameWork を使用して 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>