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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With