简体   繁体   English

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

[英]How to call WCF REST/JSON Service from client application

I have WCF REST/JSON Service, I create it by using this template. 我有WCF REST / JSON服务,我使用模板创建了它。 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. PictureData这是我的实体数据,我尝试通过EF将其保存在DB中。

In my WPF client application I try to call this method: 在我的WPF客户端应用程序中,我尝试调用此方法:

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 我也尝试在WebInvoke属性中使用Method =“ POST”
  • Also I try to use address without "Create" in HttpClient and then use it in client.Post in first parameter 我也尝试在HttpClient中使用不带“ Create”的地址,然后在client.Post中的第一个参数中使用它

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 我收到了错误的请求400

UPDATE 2 I found my problems: 更新2我发现了我的问题:

  • 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. 我使用JSON.NET序列化对象,当我接收字节数组时,它将转换为base64格式,但是我的服务需要字节数组-它解决了使用字节列表的问题。

  • 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. 第二个问题-我试图以很高的防御力收到我的桌面的尖叫,并且我有相同的响应(错误请求400),如果我将图片分辨率更改为800x600,服务效果很好,还有我的问题-如何增加配额请求消息。 I try yo use, inside standardEndpoint section(web.config) 我尝试哟使用,在standardEndpoint部分(web.config)内

readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" readerQuotas maxArrayLength =“ 2147483647” maxBytesPerRead =“ 2147483647” maxDepth =“ 2147483647” maxNameTableCharCount =“ 2147483647” maxStringContentLength =“ 2147483647”

But it doesn't work 但这不起作用

Have you tried monitoring the exact request / response with a tool like Fiddler? 您是否尝试过使用Fiddler之类的工具监视确切的请求/响应? Perhaps your post is not as you would expect it to be? 也许您的帖子不是您期望的那样?

Does the WCF service know to accept REST? WCF服务是否知道接受REST? If you are not using the WCF.WebApi there is usually the horrible wcf bindings to configure, eg: 如果您不使用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>

Does a simple REST Get work? 简单的REST可以工作吗?

我建议Hammock ,这很容易并且效果很好。

The error 400 Bad request can be due to many possiblities. 错误400错误的请求可能是由于许多可能性造成的。 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时,将readerQuotas分隔为以下内容:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM