响应主体是字节流。请参阅服务文档以了解允许的内容类型
当我使用 REST 调用 WCF 服务时,它会跳过正在调用的方法,并出现以下错误:
响应正文是字节流。 请参阅服务文档 允许的内容类型。
我正在处理将对象放入内存中,然后尝试将它们序列化为 JSON 字符串。为此,我有一个服务合同的接口:
[ServiceContract]
public interface IDashboardService
{
[WebGet(UriTemplate = "dashboard/group/id/{id}", ResponseFormat=WebMessageFormat.Json)]
[OperationContract]
Stream GetGroupById(string id);
}
并且在我的服务合同中覆盖了被跳过的方法:
public class Dashboard : GroupBase, Contracts.IDashboardService
{
public Dashboard() : base()
{
if (!ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated)
throw new WebException("Unauthorized: Class: Dashboard, Method: Dashboard()",
System.Net.HttpStatusCode.Forbidden);
}
public override System.IO.Stream GetGroupById(string id)
{
return base.GetGroupById(id);
}
}
这是基本方法:
public virtual Stream GetGroupById(string id)
{
byte[] bytes = null;
var groupId = System.Convert.ToInt32(id);
var serializer = new JavaScriptSerializer();
switch (groupId)
{
case 0: // regions
var regions = GroupRepository.GetAllRegions();
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(regions));
break;
case 1: // customers
var customers = CustomerRepository.GetAllCustomers();
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(customers));
break;
}
return new MemoryStream(bytes);
}
知道如何让它工作吗?谢谢。
When I used REST to call my WCF service it skips the method being called with the following error:
The response body is a byte stream.
See the service documentation for
allowed content types.
I am dealing with placing objects in memory and then trying to serialize them as a JSON string. To do this, I have an interface to my service contract:
[ServiceContract]
public interface IDashboardService
{
[WebGet(UriTemplate = "dashboard/group/id/{id}", ResponseFormat=WebMessageFormat.Json)]
[OperationContract]
Stream GetGroupById(string id);
}
And in my service contract overrides the method that is being skipped:
public class Dashboard : GroupBase, Contracts.IDashboardService
{
public Dashboard() : base()
{
if (!ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated)
throw new WebException("Unauthorized: Class: Dashboard, Method: Dashboard()",
System.Net.HttpStatusCode.Forbidden);
}
public override System.IO.Stream GetGroupById(string id)
{
return base.GetGroupById(id);
}
}
Here is the base method:
public virtual Stream GetGroupById(string id)
{
byte[] bytes = null;
var groupId = System.Convert.ToInt32(id);
var serializer = new JavaScriptSerializer();
switch (groupId)
{
case 0: // regions
var regions = GroupRepository.GetAllRegions();
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(regions));
break;
case 1: // customers
var customers = CustomerRepository.GetAllCustomers();
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(customers));
break;
}
return new MemoryStream(bytes);
}
Any idea how I can get this working? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法返回流,然后告诉 WCF 将其格式化为 JSON。只需删除 MessageFormat 属性并将 Content Type 标头手动设置为 application/Json
You can't return a stream and then tell WCF to format it as JSON. Just remove the MessageFormat attribute and set the Content Type header manually to application/Json
在返回内存流之前,我必须将这一行放入其中,以将其格式化为 JSON:
I had to put this line in before returning the memory stream to get it to format as JSON: