Calling a WCF Web api from a c# client.

Topics: Web Api
Aug 3, 2011 at 10:17 PM

Hi,

Is the HttpClient the easiest way of calling a WCF Web Api, or is there a way of generating a wrapper 'proxy'?

Thanks

Graham

Aug 3, 2011 at 10:25 PM
HttpEntityClient (or HttpEntityConventionClient) nugets allow you to bypass a wrapper proxy altogether.

i.e. for the convention client:

client.Get<Person>("23")

does a get and deserializes a Person from "/people/23" relative path.


/kzu

--
Daniel Cazzulino | Developer Lead | MS MVP | Clarius Consulting | +1 425.329.3471


On Wed, Aug 3, 2011 at 18:17, gravy <notifications@codeplex.com> wrote:

From: gravy

Hi,

Is the HttpClient the easiest way of calling a WCF Web Api, or is there a way of generating a wrapper 'proxy'?

Thanks

Graham

Read the full discussion online.

To add a post to this discussion, reply to this email (wcf@discussions.codeplex.com)

To start a new discussion for this project, email wcf@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 4, 2011 at 12:37 AM
Edited Aug 4, 2011 at 12:38 AM
dcazzulino wrote:
HttpEntityClient (or HttpEntityConventionClient) nugets allow you to bypass a wrapper proxy altogether.
i.e. for the convention client:
client.Get<Person>("23")
does a get and deserializes a Person from "/people/23" relative path.

I haven't looked at the code for this yet, but this sounds pretty clever.  I found myself manually creating a proxy for my WCF Web Api service, invoking HttpClient inside the hand-crafted proxy.  I put the resources in a shared assembly so that I could serialize/deserialize the "same" object on each side (without duplicating the code).  I'm going to take a peak at HttpEntityClient right away, it really does sound wonderful and sounds like it would make a great addition to a hand-crafted proxy or accessing the service directly via the client.

Aug 4, 2011 at 8:56 AM

Wow, this looks pretty simple.

Thanks dcazzulino.

Aug 4, 2011 at 3:10 PM
Edited Aug 4, 2011 at 3:18 PM

There are two nugets you can use:

  1. HttpEntityClient: http://kzu.to/kead1K
  2. HttpEntityConventionClient: http://kzu.to/pNOp3l

 

Both can be used passing an explicit path for the resource:

// GET: /people/22
client.Get<Person>("people", "22")

The second adds support for specifying a naming convention, so that the relative path can be calculated if not specified:

// GET: /people/22
conventionClient.Get<Person>("22") 

There is one built-in default convention which is a PluralizerConvention that uses the EF pluralizer built-in .NET 4.0 :). If you use the same convention server-side to register your routes, then it all works end to end automagically :)

 

Coordinator
Aug 4, 2011 at 3:53 PM
You can also just use our in the box HttpClient :-) using the nuget 'HttpClient'

you use it like this...

var client = new HttpClient();
var response = client.Get("http://foo.com/contacts/1"):
var contact = response.Content.ReadAs<Contact>.

The advantage of this client is you have full access to the http requests and responses for example to access headers like etags.

If you are returning json we have a special type JsonValue which is dynamic and does not require a real type.
.
var client = new HttpClient();
var response = client.Get("http://foo.com/contacts/1"):
dynamic contact = response.Content.ReadAs<JsonValue>.


From: gravy
Sent: 8/3/2011 2:17 PM
To: Glenn Block
Subject: Calling a WCF Web api from a c# client. [wcf:267655]

From: gravy

Hi,

Is the HttpClient the easiest way of calling a WCF Web Api, or is there a way of generating a wrapper 'proxy'?

Thanks

Graham

Aug 4, 2011 at 5:34 PM

"Mine" has a TryGet/Post/Put/Delete to access the response and optionally the T if the response was success ;)

And there's IHttpEntityClient too (happy moqing! ;))

/kzu from Android