【问题标题】:HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'HTTP/1.1 415 无法处理消息,因为内容类型为 'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'
【发布时间】:2012-11-29 01:48:39
【问题描述】:

试图将 json 字典发布到 C# WCF,当我调用它时 HTTP 响应 415。有人可以告诉我我的代码有什么问题。

对象类

 [DataContract]
public class Class1
{
    [DataMember]
    public string AccNo;
     [DataMember]
    public string CompanyName;
     [DataMember]
    public string DocDate;
}

IService1.cs

   [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        string PostSalesOrderData(string data);

Service1.svc.cs

 public string PostSalesOrderData(string data)
    {

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        Dictionary<string, Class1> dict = serializer.Deserialize<Dictionary<string, Class1>>(data);

        return dict["Debtor"].AccNo.ToString();
    }

小提琴细节

HTTP/1.1 415 无法处理消息,因为内容类型为 'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'。 服务器:Microsoft-IIS/7.5 X-Powered-By: ASP.NET 日期:2012 年 11 月 29 日星期四 01:21:55 GMT 内容长度:0

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您的服务的端点未正确配置为接收 JSON 输入。为了使用[WebInvoke] 属性,您的端点需要具有webHttpBinding,并且它还应该具有&lt;webHttp/&gt; 类型的端点行为

    确保正确配置的一种简单方法是使用 .svc 文件的Factory 属性。类似于下面的示例:

    <%@ ServiceHost Language="C#" Debug="true"
                    Service="YourNamespace.YourServiceClass"
                    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    

    【讨论】:

    • 如何在classLibrary项目中添加工厂?事实上,我正在尝试在类库中调用 Web 服务?
    猜你喜欢
    • 2014-12-24
    • 2013-01-02
    • 2020-04-25
    • 2019-05-28
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多