我正在我现有的类上使用反射提供程序(http://msdn.microsoft.com/en-us/library/dd728281.aspx)构建 WCF 数据服务(使用 .net 4.5 VS 2012)。我可以在请求标头中使用“Accept: application/atom+xml”成功访问该服务。但是,在请求标头中将“接受”更改为“应用程序/json”时出现错误“请求的媒体类型不支持”。据了解,WCF数据服务支持JSON,我应该怎么做才能在服务上启用查询json数据?
谢谢
编辑:我在下面粘贴我的代码:首先我定义了 Product 类:
[DataServiceKeyAttribute("Id")]
public class Product
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
}
然后我定义了我的 ProductContext 类:
public class ProductContext
{
private List<Product> products = new List<Product>();
public ProductContext()
{
for (int i = 0; i < 100; i++)
{
var product = new Product();
product.Id = i;
product.Name = "ID - " + i.ToString();
product.Price = i + 100;
products.Add(product);
}
}
public IQueryable<Product> Products
{
get
{
return products.AsQueryable();
}
}
}
和我的 ProductService.svc.cs
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ProductsService : DataService<ProductContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead);
//config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}