How to change Wcf to use a different serializer? - wcf

By default WCF use DataContractSerialization so if we can change it then my question is how to change it and when should we need which serialization on wcf ?

You can use the XmlSerializerFormatAttribute attribute on your service contract to force WCF to use the XmlSerializer.

WCF has a nice feature that a method can return Message or a Stream (see Returning raw json (string) in wcf and How to set Json.Net as the default serializer for WCF REST service as examples). The corresponding code which you need to write can be more easy as if you will use more advance techniques Extending Encoders and Serializers. So it is very easy to implement Streaming Message Transfer for example or just returning JPG or Excel file as a result of some WCF method.

The default choice of DataContractSerializer is good for most purpose. You can also use the DataContractJsonSerializer specially for REST type services and if the client expects Json content type. The other option is XmlSerializer for interoperability purpose if you need more control over the generated XML. DataContractSerializer is more efficient than XmlSerializer.
In 3rd party options you can use protobuf-net from Google which is more efficient than DataContract Serializer.

Related

Can I use Json serialization in WCF service?

I am going develop a WPF windows based application. I want to work with Entity Framework Self Tracking Entities and WCF. I was wondering if using Json is possible/recommended? If yes, please assist me; is there any tutorial that can help?
You can use the DataContractJsonSerializer to serialize the messages. You will have to use a REST based service (WebHttpBinding) as SOAP mandates XML as the message payload.
You can tell WCF to use the DatcontractJsonSerializer on the service side by settings in the WebGet and WebInvoke attributes but on the client side you will have to manually use this serializer as REST doesn;t have a metadata standard and therefore you have to create the requests and manage responses in a more manual fashion
Here is a reasonable guide to using Json and REST support in WCF
However, what is your driver to using Json? WCF is much more geared to SOAP based interaction currently (although the WCF 4.5 WebApi is going to address that to quite a degree). As your client is WPF you don't seem to gain alot from using Json

How to tweak default JSON serializer in WCF REST

WCF REST service works great in a way that it will reply/accept JSON or XML depending on header.
I want to tweak built-in JSON serializer a little so it encodes/decodes Byte[] little different. More specifically, I want to use Base64 for that.
Is that any pointers/samples where I can set custom type serializer that will affect whole service?
The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx shows how to replace the default JSON serializer (DataContractJsonSerializer) with a custom one (in this case, JSON.NET).
I just succeeded swapping out the default DataContractJsonSerializer with JSON.NET serializer in my WCF REST services 4.0 using NETFx Json.NET MediaTypeFormatter . Also see JSON.NET Serializer for WCF REST Services for the solution to a problem I ran into to get this working.

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...

Best way to support "application/x-www-form-urlencoded" post data with WCF?

I'm building a WCF service based on a W3C specification which defines a RESTful web service endpoint that accepts "application/x-www-form-urlencoded" post data. WCF doesn't support this type of message encoding by default and I have found a number of different examples of creating a contract that looks like this:
XElement Query_Post(Stream postData);
And then within the implementation decoding the postData stream using the HttpUtility.ParseQueryString method.
Does anyone know of a more strongly typed way of supporting "application/x-www-form-urlencoded" in WCF?
I would like my operation contract to be:
XElement Query_Post(string query, string [] params);
The best way is to use Stream like Raw HTTP POST with WCF or what you are saying.
The reason is because WCF abstracts all the communication-level physical layout stuff out from the service code. Ideally, you would want to make a service that could turn into SOAP or REST just by flipping the switch.
To support it natively, you probably have to extend WebHttpBinding or make your own binding and implement custom encoder. This is symmetric to the output like the linked post says. You have to twist its arms to get WCF to output non-XML/JSON stuff.
The WCF REST Contrib library enables this functionality:
https://github.com/mikeobrien/WcfRestContrib
It includes a POX formatter and form url encoded formatter and allows you to easily create your own. Formatters are mapped to mime types and automatically selected to serialize/deserialize the entity body based on the content type and accept headers.

Can you use the DataContractSerializer outside of WCF?

From the reading I've done I'm under the impression the DataContractSerializer handles versionong issues by, if members in the request are not there it will set the default value, and if addional members are in the request but not in the definition the serializer will simply ignores these fields and not process them.
Firstly, is this assumption correct?
Secondly, could you use this DataContractSerializer instead of the XMLSerializer so you can add this versioning ability to old asmx web services? Basically, if you add new members to your web service schema request you won't need to send to every client? When you receive the request from the client you can Deserialize using the DataContractSerializer into your object.
Hope this makes sense
You can use the DataContractSerializer outside of WCF to manually deserialize and serialize object graphs. However, you cannot tell ASMX to use the serializer. You are much better of just replacing your ASMX services with WCF services.
I have used the DataContract Serializer to import xml files, it works fine.