响应主体是字节流。请参阅服务文档以了解允许的内容类型

发布于 2024-10-15 23:09:06 字数 1804 浏览 6 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-10-22 23:09:06

您无法返回流,然后告诉 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

記柔刀 2024-10-22 23:09:06

在返回内存流之前,我必须将这一行放入其中,以将其格式化为 JSON:

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";

I had to put this line in before returning the memory stream to get it to format as JSON:

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文