3

I am following along in the .6 release of the WCF Web API chm file. I have built my service and everything works fine when I access it via IE. But when I create my console app, I don't understand how the client can know about the "contact" type. Sure I can add a reference, but how would some other client out there in the world know about the types?

List<Contact> contacts = resp.Content.ReadAs<List<Contact>>();

How would a client know about changes to the Contact class?Thanks.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
Terrence
  • 313
  • 2
  • 12

2 Answers2

3

Using the SOAP based WCF bindings, the client would normally generate a client off the WSDL, which would specify these custom types.

However as far as I know, in the REST based world of Web API, there is no facility for doing that. It is expected that the 3rd party customer / programmer making the client is given the data contract in some other form, and makes a compatible class.

In other words, there is not really an automatic way of doing that.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
1

Every property on your client type that matches a property (Name/Type) in the response type is mapped by ReadAs<T>.

If you have a string property "Name" on your response type and your client type, its value is being parsed.

You don't need a reference.

Update: If you don't want to work with a contacts type on the client side you could try something like this:

var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);

If your contact type on the server side had a property "Name" you should be able to do the following:

var name = json["Name"];

(Assuming your response was a single contact - in case of List<Contact> "json" would be of type JsonArray - you should get a clue... here is a sample showing usage of JsonValue and JsonArray).

Concerning "changes on contact type" please read this.

Community
  • 1
  • 1
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • I don't understand how the client can know about the "contact" type. So I will still need a Contact class defined in my client app correct? – Terrence Dec 27 '11 at 20:14