错误请求:带有入门工具包的 WCF REST 服务

发布于 2025-01-05 16:54:31 字数 2516 浏览 0 评论 0原文

我正在尝试构建一个 REST &基于 json 的 WCF 服务,采用复杂类型作为输入。在客户端上,我尝试使用作为 WCF REST Starter Kit 的一部分提供的 HttpClient 使用此服务。

以下是我的服务代码:

[WebInvoke(Method = "POST", UriTemplate = "/SendData", BodyStyle = WebMessageBodyStyle.Wrapped)]
public void SendData(List<EditorData> input)
{
//Do something
}

我使用了可在 WebMessageBodyStyle 枚举中找到的其他选项,但无济于事。

这是我也在客户端中使用的复杂类型数据协定:

public class EditorData
{
    public string key { get; set; }
    public long quesno { get; set; }
    public string quescontent { get; set; }
}

客户端代码:

List<EditorData> listEditor = new List<EditorData> { new EditorData { key = "key1", quescontent = "qcontent1", quesno = 1},new EditorData { key = "key2", quescontent = "qcontent2", quesno = 2}};
string jsonEditorList = listEditor.ToJSON();
HttpClient client = new HttpClient("http://localhost/RestWcfService/RestService.svc/");
client.DefaultHeaders.Accept.Add("application/json");
HttpResponseMessage response = null;
response = client.Post("SendData", HttpContent.Create(jsonEditorList));
response.EnsureStatusIsSuccessful();

要将我的自定义对象列表转换为 json 字符串,我正在使用我找到的扩展方法 此处

当我运行此应用程序时,出现以下错误:

BadRequest (400) is not one of the following: OK (200), Created (201), Accepted (202), NonAuthoritativeInformation (203), NoContent (204), ResetContent (205), PartialContent (206)

有什么想法吗?

编辑:

这是小提琴手屏幕截图:

在此处输入图像描述

更新:

,我检查了 fiddler 中的响应。内容如下:

The server encountered an error processing the request. See server logs for more details.

因此,我进入 IIS 日志,这是 IIS 中记录的错误:

2012-02-15 13:20:08 fe80::ecdd:d2dd:7f70:bef6%11 POST /RestWcfService/RestService.svc/SendData - 80 - fe80::ecdd:d2dd:7f70:bef6%11 - 400 0 0 0

更新 2

根据 Rajesh 的建议,我为我的 wcf 服务启用了跟踪。下面是服务器抛出的异常:

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.

当我将内容类型指定为 json 时,我仍然不明白它是如何获取原始格式的。

I am trying to build a REST & json based WCF service that takes a complex type as input. On the client I am trying to consume this service using HttpClient that comes as a part of WCF REST Starter Kit.

Below is my service code:

[WebInvoke(Method = "POST", UriTemplate = "/SendData", BodyStyle = WebMessageBodyStyle.Wrapped)]
public void SendData(List<EditorData> input)
{
//Do something
}

I have used other options that could be found in the WebMessageBodyStyle enum to no avail.

Here is my complex type data contract which I am using in my client as well:

public class EditorData
{
    public string key { get; set; }
    public long quesno { get; set; }
    public string quescontent { get; set; }
}

Client code:

List<EditorData> listEditor = new List<EditorData> { new EditorData { key = "key1", quescontent = "qcontent1", quesno = 1},new EditorData { key = "key2", quescontent = "qcontent2", quesno = 2}};
string jsonEditorList = listEditor.ToJSON();
HttpClient client = new HttpClient("http://localhost/RestWcfService/RestService.svc/");
client.DefaultHeaders.Accept.Add("application/json");
HttpResponseMessage response = null;
response = client.Post("SendData", HttpContent.Create(jsonEditorList));
response.EnsureStatusIsSuccessful();

To convert my custom object list to json string, I am using the extension method I found here

When I run this application I get the following error:

BadRequest (400) is not one of the following: OK (200), Created (201), Accepted (202), NonAuthoritativeInformation (203), NoContent (204), ResetContent (205), PartialContent (206)

Any thoughts?

EDIT:

Here is the fiddler screenshot:

enter image description here

UPDATE:

As suggested by Jason Freitas, I checked the response in fiddler. This is what is says:

The server encountered an error processing the request. See server logs for more details.

So I went into IIS logs and here is the error logged in IIS:

2012-02-15 13:20:08 fe80::ecdd:d2dd:7f70:bef6%11 POST /RestWcfService/RestService.svc/SendData - 80 - fe80::ecdd:d2dd:7f70:bef6%11 - 400 0 0 0

UPDATE 2

As per Rajesh's suggestion I enabled tracing for my wcf service. Below is the exception thrown by the server:

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 still don't understand how it is getting Raw format when I have specified content type as json.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2025-01-12 16:54:31

首先尝试在 WCF 服务上启用跟踪,以查看具体原因400 错误请求错误。

似乎发布的输入格式错误。您已将 EditorData 列表定义为该方法的参数,并发布一些键值对(参考您的 fiddler 屏幕截图)确保反序列化时 fiddler 中的 json 字符串转换为 EditorData 对象列表。

您还设置了要包裹的身体样式。尝试删除它,看看它是否有效。当您有多个参数时,将使用包装请求,在这种情况下,您将所有参数包装在方法名称元素内并通过网络发送。

更新:

请确保将 Content-Type 添加到标头,如下所示:

client.DefaultHeaders.ContentType = "application/json";

First try to enable Tracing on your WCF Service to see the exact cause of the 400 bad request error.

Seems like the input being posted is in a wrong format. You have defined a list of EditorData as a parameter to the method and posting some key value pairs (referring to your fiddler screenshot) Make sure that the json string in fiddler when deserialized gets converted to list of EditorData objects.

Also you have set the body style to be wrapped. Try to remove that and see if it works. Wrapped requests are used when you have multiple parameters then in that case you wrap all the parameters inside the method name element and send it across the wire.

UPDATE:

Please make sure to add the Content-Type to the header as shown below:

client.DefaultHeaders.ContentType = "application/json";
薄荷港 2025-01-12 16:54:31

[WebInvoke] 默认为 XML 序列化。您需要告诉它您正在以 JSON 格式发送数据。在 WebInvoke 属性中设置 RequestFormat,如下所示

RequestFormat = WebMessageFormat.Json

[WebInvoke] defaults to XML serialization. You need to tell it that you are sending the data as JSON. Set the RequestFormat in the WebInvoke attribute like this

RequestFormat = WebMessageFormat.Json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文