Retrieve generic list in JSON format on client side - wcf

I send generic list of data by wcf service using REST. I understand that on server side it was serializing, and on client side deserializing. How can I get data before deserialization in JSON format to show it to user ?

Take a look at this page, and drop the automatic (de)serialization.

I use DownloadData method from WebClient class to invoke RESTful service and returned data was converted to string using Encoding class :)

Related

Wcf serialization and data format over the wire

I have an understanding on how the format of the data will be when its sent on wire and before its sent over wire to the client while using WCF. Please let me know if its right and correct me if I am wrong.
Case 1 :HttpBinding is used. When returning a WCF service call to the client:
When a method returns a dot net object, it will be first serialized into netdatacontract serializer format. This serialized data will be put inside a soap envelope .This soap will be converted into binary when its sent to network wire.On the client end riverse of these steps will be done.
Case 2 :NetTcpbinding. When returning a WCF service call to the client:
When a method returns a dot net object, it will be first serialized into netdatacontract serializer format. This data will not be converted into soap format. Instead, the serialized format is directly converted into binary while putting the data on the wire. On the client side, reverse of the above is done.
If the above statements are right ,
Is this why they say netbinding uses binary encoding and is optimized for performance ?

WCF WebAPI client does not know about server types

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.

In WCF, how do I convert a Datatable to a format that will output in JSON store format without classes

So here's the problem - I have a DataTable I want WCF (.NET 3.5) to send out in a JSON store format commonly used in ExtJS, etc - basically "Rows[{"Field1":value,"Field2":value},{...}]" but I cannot find the right structure to feed back to the Operation contract to send it out in this format.
So any ideas, or any further info needed.
Thanks, in advance!
AndyPC, unfortunately, you're out of luck.
If you're dealing with an object whose type is an IXmlSerializable, the WCF JSON serializer delegates to IXmlSerializable methods first, gets the serialized XML out of them, wraps the XML in a JSON string, and just passes that on. This is one of the major weaknesses of the WCF JSON model in .NET 3.5. I think the entity framework (WCF Data Services) technologies try to handle this more elegantly, but not sure. I'd recommend manually using the JSON serializer and crafting up a string or a manual serialization mechanism that does what you want...

How do I prevent WCF from auto-serializing byte-array as base-64 encoded string?

When invoking a service, my client code passes in data. One of the data members is a byte array, so WCF will automatically encode this into a base-64 string during serialization. The problem is, the data contract of the service is expecting a byte array, not a string, so deserialization of the data will result in an exception: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''
How do I work around this glitch?
I'm not sure I understand what you mean? What does the contract for your service look like? I very much doubt the problem is the base-64 serialization, unless your service is expecting the binary data encoded in hexbinary format instead.
Remember, you're using XML here, so binary data can't travel unencoded; it needs to be serialized into a text format that can be embedded in the SOAP envelope (unless you're using MTOM), and that's usually Base-64.
It can be caused by a mismatch between client and service. Did you try refreshing your client proxy (by updating the service reference for example)?
My solution was to manually alter the autogenerated data contracts from byte[] to int[]. The XML now passes validation because each element of the int array is put inside a separate element.
The drawback is having to manually alter the data contracts if you regenerate the files from WSDLs again.

How to send response using Json object

I am working on a project in which a server page is called through XMLHttp and now I want to retrieve response the called page in json object.
and I never used json so I don't have any idea about it so please tell me how I can make json object in my server side vb.net page.
You can use JSON.NET or use DataContractJsonSerializer built in .NET 3.5.
It isn't too clear what you need to do, as far as I can understand you want to send to the caller JSON responses from a webserver, using VB.NET. I suggest to take a look at JSON.NET project, which makes serializing and deserializing your .NET objects to JSON a breeze!