简体   繁体   中英

How to call WCF REST/JSON Service from client application

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

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?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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