Is it possible to receive a request with attributes and use the XmlSerializer to deserialize it and send a response back with just elements using the DataContractSerializer?
Also, if you receive a request with attributes, must you use the XmlSerializer to deserialize the content?
For the second question: if you have attributes, then you need to use the XmlSerializer - the DataContractSerializer doesn't support them.
For the first question: yes, it's possible. No, it's not easy. The selection of the serializer is done at the operation formatter level. WCF allows you to change the serializer per operation by using the [XmlSerializerFormat] or [DataContractFormat] (which is default), but that will bind the serializer to both request and responses.
If you really, really want to do it, you can create your own formatter (replace it using some operation behavior), then in that formatter you can choose how you serialize / deserialize the inputs / outputs (you can even use different serializers for each parameter), but you'll need to create one formatter, which isn't something too easy to do.
I've written some posts about message formatters and replacing serializers in Silverlight which can give you a hint on how to start about it. But if you can live with XmlSerializer only, that'll be a lot easier.
Related
What is the realtime use case for serialization in RestAssured?
Even though we can send request body(JSON) as String.
I tried googling but did not get satisfying results.
There are few advantages that come up as your code logic becames more complicated:
You might want to send the same object to different endpoint which might not support json but xml content type. So you can simply have one pojo and RestAssured would take care of all conversions.
Your object might change in runtime. So you will have to introduce changes to your string accordingly. This is quite an error-prone way. Serializer would assure that you send somewhat what is a proper json considering syntax stuff, escaping what needs to be escaped and so on.
There might be the case when you fetch object from one endpoint and send it to another. Hence you would be able to use class for deserialization and further serialization in runtime.
I am sending some SOAP headers to a WCF service and I am trying to catch them using behavior extension.
When the message is received I am going through its headers collection.
Each header is of MessageHeaderInfo type which does not have a value property.
How can I extract the value? It can be done with an ugly parsing (to remove the xml elements around the value) but it feels lame.
your help is most appriciated!
The way you normally use message headers is by defining a DataContract (or an XmlSerializable class) to represent your header, then use Message.Headers.GetHeader<T>() to retrieve it. That method will do the deserialization and hand you a strongly typed object you can use.
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...
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.
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.