如何从客户端应用程序调用 WCF REST/JSON 服务

发布于 2024-12-09 20:06:39 字数 1979 浏览 1 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(3

雨后彩虹 2024-12-16 20:06:39

您是否尝试过使用 Fiddler 这样的工具来监控确切的请求/响应?也许您的帖子并不如您所期望的那样?

WCF 服务是否知道接受 REST?如果您不使用 WCF.WebApi,通常会存在可怕的 wcf 绑定配置,例如:

<service name="MyWcfServiceWebRole.xyz.IAbcService">
    <endpoint address="" behaviorConfiguration="webby" binding="webHttpBinding" bindingConfiguration="RestBinding" contract="MyWcfServiceWebRole.xyz.IAbcService" />
</service>

<behaviors>
   <endpointBehaviors>
      <behavior name="webby">
         <webHttp />
      </behavior>
   </endpointBehaviors>
</behaviors>

简单的 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:

<service name="MyWcfServiceWebRole.xyz.IAbcService">
    <endpoint address="" behaviorConfiguration="webby" binding="webHttpBinding" bindingConfiguration="RestBinding" contract="MyWcfServiceWebRole.xyz.IAbcService" />
</service>

<behaviors>
   <endpointBehaviors>
      <behavior name="webby">
         <webHttp />
      </behavior>
   </endpointBehaviors>
</behaviors>

Does a simple REST Get work?

回眸一笑 2024-12-16 20:06:39

我建议Hammock,它很简单而且效果很好。

I suggest Hammock, it's easy and works well.

忘年祭陌 2024-12-16 20:06:39

错误 400 Bad request 可能由多种原因引起。尝试在您的服务上启用跟踪。可以通过此处链接来完成此操作。

另外,如果您有一个带有配置的配置文件,请确保您使用 webHttpBinding 时指定了 readerQuotas,如下所示:

<webHttpBinding>
    <binding name="RestBinding">
      <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384"
        maxBytesPerRead="4096" />
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>

如果您使用 REST API 通过 global.asax 中的路由定义服务并使用标准端点,请使用以下内容:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
    <readerQuotas maxStringContentLength="5242880" maxArrayLength="4194304"
            maxBytesPerRead="4194304" />
</standardEndpoint>

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:

<webHttpBinding>
    <binding name="RestBinding">
      <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384"
        maxBytesPerRead="4096" />
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </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:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
    <readerQuotas maxStringContentLength="5242880" maxArrayLength="4194304"
            maxBytesPerRead="4194304" />
</standardEndpoint>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文