如何从客户端应用程序调用 WCF REST/JSON 服务
我有 WCF REST/JSON 服务,我使用 this 模板。在我的服务中,我有一个方法
[WebInvoke(UriTemplate = "Create", Method = "*",RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
public void Create(PictureData pictureData)
{
var context = new EFDBContext();
context.PictureData.Add(pictureData);
context.SaveChanges();
}
PictureData 这是我的实体数据,我尝试通过 EF 将其保存在数据库中。
在我的 WPF 客户端应用程序中,我尝试调用此方法:
using (var client = new HttpClient("http://localhost:8080/ScreenPictureService/Create"))
{
var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData);
client.Post("", dataContract);
}
但没有发生
- 我还尝试在 WebInvoke 属性中使用 Method="POST"
- 另外,我尝试在 HttpClient 中使用不带“Create”的地址,然后使用它在 client.Post 中的第一个参数
UPDATE
尝试此操作后,
var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData, typeof (PictureData));
var client = new HttpClient();
using(var response = client.Post("http://localhost:8080/ScreenPictureService/Create", dataContract))
{
response.EnsureStatusIs(HttpStatusCode.OK);
}
我收到了错误请求 400
UPDATE 2 我发现了我的问题:
我使用 JSON.NET 序列化我的对象,当我收到字节数组时它转换为 base64 格式,但我的服务需要字节数组 - 它解决了使用字节列表的问题。
第二个问题 - 我尝试接收高清晰度的桌面屏幕截图,并且我有相同的响应(错误请求 400),如果我将图片分辨率更改为 800x600,服务运行良好,并且有我的问题 - 如何增加请求消息的配额。我尝试在 standardEndpoint 部分(web.config)中使用
readerQuotas maxArrayLength =“2147483647”maxBytesPerRead =“2147483647”maxDepth =“2147483647”maxNameTableCharCount =“2147483647”maxStringContentLength =“2147483647”
但它没有'工作
I have WCF REST/JSON Service, I create it by using this template. In my service I have a method
[WebInvoke(UriTemplate = "Create", Method = "*",RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
public void Create(PictureData pictureData)
{
var context = new EFDBContext();
context.PictureData.Add(pictureData);
context.SaveChanges();
}
PictureData this my entity data, which I try to save in DB via EF.
In my WPF client application I try to call this method:
using (var client = new HttpClient("http://localhost:8080/ScreenPictureService/Create"))
{
var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData);
client.Post("", dataContract);
}
But nothing happen
- I also try to use Method="POST" in WebInvoke attribute
- Also I try to use address without "Create" in HttpClient and then use it in client.Post in first parameter
UPDATE
After I try this
var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData, typeof (PictureData));
var client = new HttpClient();
using(var response = client.Post("http://localhost:8080/ScreenPictureService/Create", dataContract))
{
response.EnsureStatusIs(HttpStatusCode.OK);
}
I have received Bad Request 400
UPDATE 2
I found my problems:
I use JSON.NET to serialize my object, and when I receive byte array it convert to base64 format, but my service expect byte array - it solved to use list of bytes.
And second problem - I try to receive screnshot of my desctop with high defenition, and i have the same response(Bad Request 400), if I change picture resolution to 800x600, service works well, and there is my question - How to increase quota of request message. I try yo use, inside standardEndpoint section(web.config)
readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"
But it doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过使用 Fiddler 这样的工具来监控确切的请求/响应?也许您的帖子并不如您所期望的那样?
WCF 服务是否知道接受 REST?如果您不使用 WCF.WebApi,通常会存在可怕的 wcf 绑定配置,例如:
简单的 REST 可以工作吗?
Have you tried monitoring the exact request / response with a tool like Fiddler? Perhaps your post is not as you would expect it to be?
Does the WCF service know to accept REST? If you are not using the WCF.WebApi there is usually the horrible wcf bindings to configure, eg:
Does a simple REST Get work?
我建议Hammock,它很简单而且效果很好。
I suggest Hammock, it's easy and works well.
错误 400 Bad request 可能由多种原因引起。尝试在您的服务上启用跟踪。可以通过此处链接来完成此操作。
另外,如果您有一个带有配置的配置文件,请确保您使用 webHttpBinding 时指定了 readerQuotas,如下所示:
如果您使用 REST API 通过 global.asax 中的路由定义服务并使用标准端点,请使用以下内容:
The error 400 Bad request can be due to many possiblities. Try enabling tracing on your service. It can be done by following the link here.
Also if you have a config file with configuration make sure you have the readerQuotas sepecified as below if you are using webHttpBinding:
If you are using the REST API with defining your service with routes in global.asax and using a standard endpoint use the below: