Hi Guys,
As part of the requirement of the Remarketing tool, I needed to create two services which needs to communicate with each other. One being the RDTSecurityService and the Other is the UserDALService. RDTSecurity Service consumes the UserDAL Service for some of it Authentication related functionalities. When I was working on it and after careful observation I found out that the Data sent from RDTSecurity Service to the UserDALService is getting disappeared. This is sounding weird, that how can the data disappear ?
Disappearing the data means the object sent from RDTSecurity Service which has a valid data is found missing(or you can say the object is getting reinitialized) when seen at the UserDALService level. After complete verification of the code, I had a look in the Proxy file of the UserDAL Service which is at the RDTSecurityService.
And it was looking something similar to this :
public partial class LoginCredentials : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private bool activeCenterFieldSpecified;
private string clientIPAddressField;
private bool isFromObeliskField;
private bool isFromObeliskFieldSpecified;
private int noOfDaysLeftForExpiryField;
private bool noOfDaysLeftForExpiryFieldSpecified;
private string passwordField;
private string serverIPAddressField;
private string sessionIDField;
private int userIDField;
private bool userIDFieldSpecified;
}
}
If you observe carefully that for few fields some extra variable declared as Boolean.
(Ex: private bool userIDFieldSpecified;)
This was not declared by me explicitly; but I found it in the proxy class. I was amazed how come these variables are created, and what could be the purpose.
WHO IS THE CULPRIT??
---------------------------
When I did some RND, I found that here, the WCF Data Contract Serializer is the Culprit.
{
Basically there are 3 types of Serializer :
- XmlSerializer
- DataContractSerializer
- NetDataContractSerializer
Each performs the same fundamental task i.e., mapping between .NET objects and XML Infosets.
}
DataContractSerializer is the default and is always used unless specified.
Why
--------
The answer is the schema that the WCF data contract Serializer generates by default. Because of the way its versioning model works, the Serializer generates all data members as optional elements.
The older web services stack, ASP.NET Web Services (“ASMX”), uses a different Serializer, the XmlSerializer, which maintains full schema and XML fidelity. The XmlSerializer maps all optional elements to two members: one represents the data itself, and one specifies whether or not the data is actually present – this is the “xxxSpecified” member.
These xxxSpecified members must be set to true to enable the serialization of the corresponding “actual data” members
Solution
--------------
1) You can prevent the xxxSpecified members from being generated by marking the data members as required instead of optional:
[DataContract]
public class LoginCredentials
{
[DataMember(IsRequired=true)]
public int UserID;
}
2) Change the generated proxy code on the client side. You could specify default values of “true” for the xxxSpecified members, or you could modify the property setter for each member (e.g. zipCodeFrom) to set the corresponding xxxSpecified member (e.g. zipCodeFromSpecified) to true whenever that property is set.
There are many circumstances in which modifying the generated proxy code is unacceptable – e.g. when you expect the service to change and thus expect to have to regenerate the proxy code often. In this situation, you can take advantage of the “partial class” feature to add a helper method to do the property setting work.
{ Inother words, we can find all the member which name xxxSpecified and set the value as true}
3) If you really need to avoid xxxSpecified with parameters, there is a workaround involving
[MessageContract(IsWrapped=false)] containing a data contract with required data members
Note:- This will affect the interoperability
4) Switching to XmlSerializer on the service side instead of the DataContractSerializer.
References :
------------
http://blogs.msdn.com/eugeneos/archive/2007/02/05/solving-the-disappearing-data-issue-when-using-add-web-reference-or-wsdl-exe-with-wcf-services.aspx