none
Serialize generic contractor RRS feed

  • Question

  • I'm built a WCF, I add reference to my application and all OK.

    I provide an API that get list of commands, each command include different data, but in run time I get an error about deserialize :(

    This API: I'm tring to send bulk of commands inside one command, I saw that if I send lots of small commands (data) the system work verrrrry slow, so I come out with new command to collect few commands and send them in one shot. I'm dealing with serialize issue, whould be glad if you help me.

        #region BulkCommand
    
     [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    int Server_BulkCommands(BulkCMD_Structure bulkCommands);
    
    
        [Serializable]
        [DataContract(Name = "BulkCMD_Structure")]
        public class BulkCMD_Structure
        {
            [DataMember]
            public int DataArrayLength { get; set; }
            [DataMember]
            public BulkCMD_Data[] DataArray;
    //I tried also  public List<BulkCMD_Data> DataArray;
        }
    
        [Serializable]
        [DataContract(Name = "BulkCMD_Data")]
        public class BulkCMD_Data
        {
            [DataMember]
            public eWebSocketCMD CommandID { get; set; }
        }
        [Serializable]
        [DataContract(Name = "BulkWindowCMD_Data")]
        public class BulkWindowCMD_Data : BulkCMD_Data
        {
            [DataMember]
            public int Width { get; set; }
            [DataMember]
            public int Height { get; set; }
            [DataMember]
            public byte[] Image { get; set; }
        }
    
        [Serializable]
        [DataContract(Name = "BulkMouseStatusCMD_Data")]
        public class BulkMouseStatusCMD_Data : BulkCMD_Data
        {
            [DataMember]
            public int mousePositionX { get; set; }
            [DataMember]
            public int mousePositionY { get; set; }
        }
    
    
        #endregion

    Wednesday, January 9, 2013 5:33 PM

Answers

  • Find the solution:

        [KnownType(typeof(BulkCMD_Data))]
        [KnownType(typeof(BulkWindowCMD_Data))]
        [DataContract(Name = "BulkCMD_Structure")]
        public class BulkCMD_Structure
        {
            [DataMember]
            public int DataArrayLength { get; set; }
            [DataMember]
            public object[] DataArray;
        }
    

    • Marked as answer by Markos_King Thursday, January 10, 2013 9:04 AM
    Thursday, January 10, 2013 9:01 AM

All replies

  • The error:

    It say he don't know type "BulkWindowCMD_Data", but looking inside the reference.cs, I see this type/class!

     

    System.ServiceModel.FaultException was caught
      Message=The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:bulkCommands. The InnerException message was 'Element 'http://schemas.datacontract.org/2004/07/T_WCF_WebSocketServerNameSpace:BulkCMD_Data' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/T_WCF_WebSocketServerNameSpace:BulkWindowCMD_Data'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'BulkWindowCMD_Data' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.
      Source=mscorlib
      Action=http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault
      StackTrace:
        Server stack trace: 
           at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
           at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
           at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
        Exception rethrown at [0]: 
           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
           at WCF_WebSocketServer_WinLibNamespace.T_WcfServiceReference.IT_WCF_WebSocketServer.Server_BulkCommands(BulkCMD_Structure bulkCommands)
           at WCF_WebSocketServer_WinLibNamespace.T_WcfServiceReference.T_WCF_WebSocketServerClient.Server_BulkCommands(BulkCMD_Structure bulkCommands) in C:\T_WCF_WebSocketServer\WindowsLibs\Service References\T_WcfServiceReference\Reference.cs:line 1364
           at T_WCF_WebSocketService_WinLibrary.WCF_WebSocketService_Class.Server_BulkCommands(String[] targetToken, BulkCMD_Structure bulkCommands) in C:\T_WCF_WebSocketServer\WindowsLibs\WCF_WebSocketServer.cs:line 241
           at Transmiter.Manager.BulkWCF.FlushAll() in C:\BulkManagment\BulkWCF.cs:line 103
           at Transmiter.Manager.BulkWCF.AddCommand(Object command) in C:\BulkManagment\BulkWCF.cs:line 69
           at Transmiter.Manager.TransmitCommandsWCF.OnBulkCommandTransmition(Object objInfor) in C:\TransmitCommandsWCF.cs:line 336
      InnerException: 

    Thursday, January 10, 2013 7:17 AM
  • Hi Markos_King,

    first thing why you have applied Serializable attribute?

    Please remove that from all classes and try again.


    One good question is equivalent to ten best answers.

    Thursday, January 10, 2013 7:58 AM
  • Hi Shyam,

        Thanks for the respond, I remove the [Serializable] attribute, but get same exception.

    Thursday, January 10, 2013 8:16 AM
  • Yes, now fine. Can you tell what is eWebSocketCMD?

    Possible this class can not be serialized.That may be the reason.Can you tell what is eWebSocketCMD ?


    One good question is equivalent to ten best answers.

    Thursday, January 10, 2013 8:22 AM
  • here:

     [DataContract(Name = "eWebSocketCMD")]
        public enum eWebSocketCMD : int
        {
            [EnumMember]
            OPEN = 0,
            [EnumMember]
            PAINT,
            [EnumMember]
            WINDOW_CLOSED,
            [EnumMember]
            MOUSESTATUS,
        };

    The error that
    Thursday, January 10, 2013 8:25 AM
  • I changed the array to object [], and the error again that 'BulkWindowCMD_Data' unkown type :(

    There was an error while trying to deserialize parameter http://tempuri.org/:bulkCommands. The InnerException message was 'Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/T_WCF_WebSocketServerNameSpace:BulkWindowCMD_Data'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'BulkWindowCMD_Data' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details. 

    Thursday, January 10, 2013 8:28 AM
  • Find the solution:

        [KnownType(typeof(BulkCMD_Data))]
        [KnownType(typeof(BulkWindowCMD_Data))]
        [DataContract(Name = "BulkCMD_Structure")]
        public class BulkCMD_Structure
        {
            [DataMember]
            public int DataArrayLength { get; set; }
            [DataMember]
            public object[] DataArray;
        }
    

    • Marked as answer by Markos_King Thursday, January 10, 2013 9:04 AM
    Thursday, January 10, 2013 9:01 AM
  • Hi Markos_King,
    but from my side everything is fine.I tried to create the situation here also, but there is no problem. I was successfully able to add reference to that service and also able to consume it in Silverlight application.
    Which application is your client ?
    I am not getting why you were getting error?
    Which version of WCF are you using?

    One good question is equivalent to ten best answers.

    Thursday, January 10, 2013 9:28 AM
  • My client is Windows Form.

    What you mean WCF version? , I'm using .Net 4 , and host in IIS7.5

    As the error shows, the is "BulkWindowCMD_Data" an unknown type.

    I find information here:

    http://blogs.msdn.com/b/youssefm/archive/2009/06/05/introducing-a-new-datacontractserializer-feature-the-datacontractresolver.aspx

    But then in that article there is a Note: "Note that this overload currently only works for XmlDictionaryReader/XmlDictionaryWriter and not for Stream or XmlReader/Writer."

    So I keep search and find the answer: here:

    http://stackoverflow.com/questions/6153093/unexpected-type-serialization-exception

    Tried and all works.

    Thursday, January 10, 2013 9:42 AM
  • Anyway but from Silverlight application i was successfully able to add reference to service and also able to consume that.
    In VS 2010 , it is WCF 4.0 .

    One good question is equivalent to ten best answers.

    Thursday, January 10, 2013 9:50 AM