none
NetDataContractSerialization throwing deserialization error

    Question

  • hi,

    I have methods which return interface and some methods which accepts interface as parameters. I am trying to use Net DataContractSerializer but I am getting following error...

    The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:id. The InnerException message was 'Error in line 1 position 120. XML 'Element' 'http://tempuri.org/:id' does not contain expected attribute 'http://schemas.microsoft.com/2003/10/Serialization/:Type'. The deserializer has no knowledge of which type to deserialize. Check that the type being serialized has the same contract as the type being deserialized.'. Please see InnerException for more details.

    Please help me how to resolve this error....
    If I use Netdatacontract attribute on Operation Contract, can i use DataContract and Datamember attribute on serializable class???

    Thanks in advance...
    Thursday, March 18, 2010 12:34 AM

Answers

All replies

  • No one got this type of error... Please someone help me with this error....
    Saturday, March 20, 2010 4:00 PM
  • have you shared the types at both ends?  NetDCS requires you to have the same type at both ends of the communication channel. 

    NetDataContractSerializer is an opt in serialization mechanism and so you have to mark your contracts with either [Serializable] or [DataContract]/[DM]


    Amit Sharma
    Saturday, March 20, 2010 4:53 PM
    Moderator
  • I marked the class with Datacontract and Datamember attributes.

    have you shared the types at both ends? ....

     

    I didn't understand this line.. Could you please eloborate.... how to share the types at both ends....

    Saturday, March 20, 2010 7:00 PM
  • Hi,

    From the description of NetDCS, sharing data types is a must.
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.aspx

    To share datacontract types between serialize and deserialize ends, you could move the datacontract into sepearate .net library project, then add reference of library project to wcf client and service project.

    Thanks,


    Mog Liang
    Monday, March 22, 2010 3:12 AM
  • Hi Mog Liang,

    I have the Datacontract code in seperate .net library and added the reference to both client and service. To eanble the Net Dcs I am using following code..

    public

    class NetDataContractFormat : Attribute, IOperationBehavior

    {

    public void AddBindingParameters(OperationDescription description,

    BindingParameterCollection parameters)

    {

    }

    public void ApplyClientBehavior(OperationDescription description,

    ClientOperation proxy)

    {

    ReplaceDataContractSerializerOperationBehavior(description);

    }

    public void ApplyDispatchBehavior(OperationDescription description,

    DispatchOperation dispatch)

    {

    ReplaceDataContractSerializerOperationBehavior(description);

    }

    public void Validate(OperationDescription description)

    {

    }

    private static void ReplaceDataContractSerializerOperationBehavior(

    OperationDescription description)

    {

    DataContractSerializerOperationBehavior dcsOperationBehavior =

    description.Behaviors.Find<

    DataContractSerializerOperationBehavior>();

    if (dcsOperationBehavior != null)

    {

    description.Behaviors.Remove(dcsOperationBehavior);

    description.Behaviors.Add(new

    NetDataContractSerializerOperationBehavior(description));

    }

    }

    public class NetDataContractSerializerOperationBehavior :

    DataContractSerializerOperationBehavior

    {

    public NetDataContractSerializerOperationBehavior(

    OperationDescription operationDescription) :

    base(operationDescription) { }

    public override XmlObjectSerializer CreateSerializer(

    Type type, string name, string ns, IList<Type> knownTypes)

    {

    return new NetDataContractSerializer();

    }

    public override XmlObjectSerializer CreateSerializer(Type type,

    XmlDictionaryString name, XmlDictionaryString ns,

    IList<Type> knownTypes)

    {

    return new NetDataContractSerializer();

    }

    }

    }

    If I use the above code I am getting deserialization error. I changed the above code and now the code looks like...

    public

    class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior

    {

    public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)

    : base(operationDescription)

    {

    }

    public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)

    {

    return new NetDataContractSerializer();

    }

    public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)

    {

    return new NetDataContractSerializer();

    }

    }

    public

    class NetDataContractSerializerElement : BehaviorExtensionElement

    {

    public override Type BehaviorType

    {

    get { return typeof(NetDataContractSerializerBehavior); }

    }

    protected override object CreateBehavior()

    {

    return new NetDataContractSerializerBehavior();

    }

    }

    public

    class NetDataContractSerializerBehavior : Attribute, IServiceBehavior, IEndpointBehavior

    {

    #region

    IServiceBehavior

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)

    {

    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)

    {

    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)

    {

    foreach (var endpoint in serviceDescription.Endpoints)

    this.RegisterContract(endpoint);

    }

    #endregion

    #region

    IEndpointBehavior

    public void Validate(ServiceEndpoint endpoint)

    {

    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)

    {

    this.RegisterContract(endpoint);

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)

    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)

    {

    }

    #endregion

    #region

    Support

    protected void RegisterContract(ServiceEndpoint endpoint)

    {

    foreach (OperationDescription desc in endpoint.Contract.Operations)

    {

    var dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();

    if (dcsOperationBehavior != null)

    {

    int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);

    desc.Behaviors.Remove(dcsOperationBehavior);

    desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));

    }

    }

    }

    #endregion

    }

    When I replaced the code with above code to enable NetDcs, I am getting Soccet exception and timed out exception.

    Please let me know is the above code which i am using is correct or not? I am using proxy generated by service reference. Please let me know what are the changes we need to make in config files (Both Server and Client config)...

    Please help me... I am stuck with this error....

    Thanks in advance...

    Monday, March 22, 2010 8:11 PM
  • Hi htanias,

    I wrote a sample to utilize the NetDCS, please download and have a test.

    http://cid-8d29fb569d8d732f.skydrive.live.com/self.aspx/.Public/NetDCS%5E_WCF%5E_1.zip

    Thanks,


    Mog Liang
    • Marked as answer by htanias Thursday, March 25, 2010 1:07 AM
    Wednesday, March 24, 2010 2:51 AM
  • Thanks Mog Liang.... I will try the way you implemented in the sample....
    Thursday, March 25, 2010 1:07 AM