1

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");
        }


    }
}

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.

enter image description here

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

1 Answers1

1

Finally i found that i should move

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

from constructor to my Edit method .Why ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180