【问题标题】:wcf restful service method invoke error for http posthttp post的wcf restful服务方法调用错误
【发布时间】:2011-07-11 03:22:44
【问题描述】:

我有一个简单的安静的 wcf 服务,我创建它只是为了好玩。我尝试了调用 post 方法,但我失败了。这是摘要;

这是我的服务合约接口;

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

    }
}

这是我的服务的数据成员,它是 Category2 类;

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

    }
}

这是我试图调用 post 方法的代码;

    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();

这是我用来发送的xml文件,即category.xml;

<? xml version =" 1.0 "?>
<Category2>
  <CategoryID />
  <CategoryName> Tugberk's Category </CategoryName>
  <Description> .net, wcf, wpf, mvc, silverlight </Description>
</Category2>

当我运行代码时,我一尝试调用 _WebRequest.GetResponse() 就会收到以下错误;

传入的消息有一个 意外消息格式“原始”。这 预期的消息格式 操作是'Xml'; '杰森'。这个可以 是因为 WebContentTypeMapper 有 没有在绑定上配置。 参见文档 WebContentTypeMapper 了解更多详情。

我也尝试使用带有请求生成器功能的提琴手发布它,我也遇到了同样的错误。

那么我在这里想念什么?

【问题讨论】:

    标签: wcf rest http-post webrequest wcf-rest


    【解决方案1】:

    将请求的内容类型更改为text/xml; charset=utf-8

    【讨论】:

    • 我用 application/xml 做到了; charset=utf-8 和你用 text/xml;charset=utf-8 指出的那样。现在我得到了这个例外;检查 WcfServiceWithNortwind.Smooth.Category2 类型对象的开始元素时出错。根级别的数据无效。第 1 行,位置 1。
    【解决方案2】:

    确保您的请求中有标头 Content-Type: application/xml 并在 XML 有效负载上设置命名空间从 DataContract 中删除命名空间。喜欢 [DataContract(Namespace="")]

    【讨论】:

    • 我认为问题也与命名空间有关。我没有时间尝试,但我今天会。谢谢!
    • 我们能否看到解决方案最终使其发挥作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多