【发布时间】:2010-12-06 14:47:03
【问题描述】:
我有一个基于实体框架 POCO 类的简单原型 WCF 服务。当我在未指定响应格式的情况下运行其中一种公开方法时,它会将 XML 格式的预期数据返回给浏览器。但是,如果我指定“ResponseFormat = WebMessageFormat.Json”,则不会向浏览器返回任何数据。如果我尝试使用 Fiddler 查看更多信息,我会发现对浏览器的响应是“ReadResponse() failed: The server did not return a response for this request.”。
这是服务合同:
[ServiceContract]
public interface ITimeService
{
[OperationContract]
[WebGet(UriTemplate = "/Customer?ID={customerID}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Customer GetCustomer(string customerID);
[OperationContract]
[WebGet(UriTemplate = "/Customers", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Customer> GetCustomers();
[OperationContract]
[WebGet(UriTemplate = "/Tasks/?CustomerID={customerID}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Task> GetTasks(string customerID);
}
以及实现:
public Customer GetCustomer(string customerID)
{
var ID = new Guid(customerID);
var context = new PinPointTimeEntities();
var customer = context.Customers.Include("TimePeriods").Include("Tasks").Where(c => c.ID == ID).SingleOrDefault<Customer>();
return customer;
}
public List<Customer> GetCustomers()
{
var context = new PinPointTimeEntities();
var customers = context.Customers.ToList();
return customers;
}
public List<Task> GetTasks(string customerID)
{
var ID = new Guid(customerID);
var context = new PinPointTimeEntities();
var tasks = context.Tasks.Include("TimePeriods").Where(c => c.CustomerID == ID).ToList();
return tasks;
}
我尝试了许多建议的解决方案,但均未成功。我想这是一个简单的设置或需要的东西。需要怎么做才能成功返回json格式的数据?
【问题讨论】:
-
您可以发布服务合同或至少发布您正在使用的方法吗?你也可以发布那个服务方法的实现吗?