No Data Returned
I have a simple prototype WCF service based on Entity framework POCO classes. When I run one of the exposed methods without specifying a response format it returns the expected data in XML format to the browser. However if I specify "ResponseFormat = WebMessageFormat.Json" then no data is returned to the browser. If I try using Fiddler to see more of what is going on I find that the response to the browser is "ReadResponse() failed: The server did not return a response for this request.".
Here is the Service Contract:
[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);
}
And the implementation:
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;
}
I've tried a number of suggested solutions with no success. I imagine it is a simple setting or something that is required. What do I need to do to get the data returned successfully in json format?
I believe that this problem is related to self referencing classes. From what I can see both in the scenario above, and in an attempt to store data in Isolated Storage on the Windows Phone using the DataContractJsonSerializer, it seems that JSON does not handle serializing classes with circular references.
I know this question has already been answered, but...
I believe it's because your Customer
is not serializable. It's pretty simple to fix that, just create a datacontract by adding the [ DataContract]
tag to your Customer
class and [DataMember]
tag to your (public) data fields (that you want serialized).
[DataContract]
public class Customer {
...
[DataMember]
public int CustomerID; // this field will show up in the json serialization
public string CustomerSIN; // this field will not
...
}
I had same issue, my wcf service was not formatting the json properly while converting it from Dataset to Json. I got it working by using the following solution:
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
dsData is my Dataset
String JString = Newtonsoft.Json.JsonConvert.SerializeObject(dsData);
return WebOperationContext.Current.CreateTextResponse(JString, "application/json;charset=utf-8", System.Text.Encoding.UTF8);
and "Message" will be the return type.
链接地址: http://www.djcxy.com/p/45808.html上一篇: 无法从wcf json启用的服务将嵌套类型返回给客户端
下一篇: 没有数据返回