操作的预期消息格式为“Xml"、“Json" [英] The expected message formats for the operation are 'Xml', 'Json'

查看:32
本文介绍了操作的预期消息格式为“Xml"、“Json"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 wcf 休息服务.如您所见:

I create a wcf rest service .as you can see the details of that :

  [ServiceContract]
    public interface INewsRepository
    {


        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/AddNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool Add(News entity);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/DeleteNews/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool Remove(string id);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/EditNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool Edit(News entity);

    }

我像这样在我的客户端中调用此服务:

I call this service in my client like this :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Web.Script.Serialization;
using CMSManagement.Domain.Entity;
using UPTO.UI.Infrastructure.Configuration;
using System.Text;

namespace UPTO.UI.Services
{
    public class NewsRepository : IServiceRepository<News>
    {
        private WebClient ClientRequest;
        private MemoryStream ms;
        private DataContractJsonSerializer serializerToUplaod;
        public string ServiceHostName = "http://cmsmanagement"+ ServiceServer.Address;
        public NewsRepository()
        {
            ClientRequest = new WebClient();
            ClientRequest.Headers["Content-type"] = "application/json";
            ms=   new MemoryStream();
        }     

        public bool Add(News data)
        {

            serializerToUplaod = new DataContractJsonSerializer(typeof(News));
            serializerToUplaod.WriteObject(ms, data);
            string Result = System.Text.Encoding.UTF8.GetString(ClientRequest.UploadData(ServiceHostName+"/NewsRepository.svc/AddNews", "POST", ms.ToArray()));
            return Result.ToLower().Equals("true");

        }

        public bool Delete(string id)
        {
            byte[] data = ClientRequest.UploadData(ServiceHostName+"/NewsRepository.svc/DeleteNews/"+id, "POST", ms.ToArray());
            string Result = System.Text.Encoding.UTF8.GetString(data);
            return Result.ToLower().Equals("true");
        }

        public bool Edit(News data)
        {
            serializerToUplaod = new DataContractJsonSerializer(typeof(News));
            serializerToUplaod.WriteObject(ms, data);

            string Result = System.Text.Encoding.UTF8.GetString(ClientRequest.UploadData("http://localhost:47026/NewsRepository.svc/EditNews", "POST", ms.ToArray()));
            return Result.ToLower().Equals("true");
        }


    }
}

问题是我的服务的编辑部分:当我调用我的编辑方法时,我在 wcf 日志中收到此错误:

The problem is the edit part of my service : When i call my edit method i get this error in my wcf log :

The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.

推荐答案

最后我发现我应该搬家

Finally i found that i should move

  ClientRequest.Headers["Content-type"] = "application/json";

从构造函数到我的 Edit 方法.为什么?

from constructor to my Edit method .Why ?

这篇关于操作的预期消息格式为“Xml"、“Json"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆