I have a simple restful wcf service which I have created just for fun. I tried the invoke the post method but I am failling. here is the summary;
this is my service contract interface;
namespace WcfServiceWithNortwind.Smooth {
    [ ServiceContract]
    public interface INorthwindService {
        [ WebGet (UriTemplate = "/" )]
        [ OperationContract ]
 开发者_如何学运维       List <Category2 > GetCategories();
        [ WebGet (UriTemplate = "categories/{id}" )]
        [ OperationContract ]
        Category2 GetCategory(string id);
        [ WebInvoke (UriTemplate = "categories/{id}" , Method = "DELETE")]
        [ OperationContract ]
        void DeleteCategory(string id);
        [ WebInvoke (UriTemplate = "categories" , Method = "POST")]
        void AddCategory(Category2 category);
    }
}
this is the data members of my service which is Category2 Class;
namespace WcfServiceWithNortwind.Smooth {
    [ DataContract]
    public class Category2 {
        [ DataMember ]
        public int CategoryID { get; set ; }
        [ DataMember ]
        public string CategoryName { get; set ; }
        [ DataMember ]
        public string Description { get; set ; }
    }
}
this is the code which I am trying to invoke the post method;
    System.Xml. XmlDocument doc = new System.Xml. XmlDocument();
    doc.Load(context.Server.MapPath( "~/@xml/category.xml" ));
    string strHostAddress = "http://localhost:54860/Smooth/Nortwind.svc/categories" ;
    string xmldata = doc.OuterXml;
    string _data = String .Format( "{0}{1}", "category=" , xmldata);
    WebRequest _WebRequest = WebRequest .Create(strHostAddress);
    _WebRequest.Method = "POST" ;
    byte [] byteArray = Encoding .UTF8.GetBytes(_data);
    _WebRequest.ContentType = "application/x-www-form-urlencoded" ;
    _WebRequest.ContentLength = byteArray.Length;
    Stream dataStream = _WebRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    var _response = _WebRequest.GetResponse();
and this is the xml file that I am using to send which is category.xml;
<? xml version =" 1.0 "?>
<Category2>
  <CategoryID />
  <CategoryName> Tugberk's Category </CategoryName>
  <Description> .net, wcf, wpf, mvc, silverlight </Description>
</Category2>
when I run the code, I am getting the following error as soon as I try to _WebRequest.GetResponse() call;
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.
I also tried to post it with fiddler with request builder function of it and I got the same error as well.
So what I am missing here guys?
Make sure you have the header Content-Type: application/xml in your request and either set a namespace on your XML payload or remove the Namespace from your DataContract. Like [DataContract(Namespace="")]
Change the request's content type to text/xml; charset=utf-8
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论