Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

Cannot read an object from Microsoft Message Que ( MSMQ )

Hello,
I am trying to read an object from MSMQ and am receiving an error...It seems that the body of the message is null.

After line 124 is executed, i check the value of the "Body" property in the oMessage object and it says the following:

Body      <error: an exception of type: {System.InvalidOperationException} occurred>      System.Object


The following is my code:

      
1      [ System.Serializable ( ) ]                                                
2      public class TagInfo : System.Runtime.Serialization.ISerializable                                                 
3      {                                                
4                                                      
5            #region Variables                                          
6                                                      
7            public System.Guid SyncId = System.Guid.NewGuid ( ) ;                                          
8            public int NumericId ;                                          
9            public double Value ;                                          
10            public System.DateTime TimeStamp ;                                          
11                                                      
12            #endregion                                          
13                                                      
14            #region Constructors                                          
15                                                      
16            public TagInfo ( )                                          
17            {                                          
18            }                                          
19                                                      
20            public TagInfo (                                           
21                  System.Runtime.Serialization.SerializationInfo info                                    
22                  , System.Runtime.Serialization.StreamingContext context )                                    
23            {                                          
24                  this.SyncId = ( System.Guid ) info.GetValue ( "SyncId", typeof ( System.Guid ) ) ;                                    
25                  this.NumericId = System.Convert.ToInt32 ( info.GetValue ( "NumericId", typeof ( int ) ) ) ;                                    
26                  this.Value = System.Convert.ToDouble ( info.GetValue ( "Value", typeof ( double ) ) ) ;                                    
27                  this.TimeStamp = System.Convert.ToDateTime ( info.GetValue ( "TimeStamp", typeof ( System.DateTime ) ) ) ;                                    
28            }                                          
29                                                      
30                                                      
31            public TagInfo ( System.Guid syncId , int numericId                                          
32                        , double theValue , System.DateTime timeStamp )                              
33                  : this ( numericId , theValue , timeStamp )                                    
34            {                                          
35                  this.SyncId = syncId ;                                    
36            }                                          
37                                                      
38            public TagInfo ( int numericId                                          
39                  , double theValue , System.DateTime timeStamp )                                    
40            {                                          
41                  this.NumericId = numericId ;                                    
42                  this.Value = theValue ;                                    
43                  this.TimeStamp = timeStamp ;                                    
44            }                                          
45                                                      
46                                                      
47            #endregion                                          
48                                                      
49                                                      
50            #region ISerializable Members                                          
51                                                      
52            public void GetObjectData (                                           
53                  System.Runtime.Serialization.SerializationInfo info                                    
54                  , System.Runtime.Serialization.StreamingContext context )                                    
55            {                                          
56                  info.AddValue ( "SyncId", this.SyncId );                                    
57                  info.AddValue ( "NumericId", this.NumericId );                                    
58                  info.AddValue ( "Value", this.Value  );                                    
59                  info.AddValue ( "TimeStamp", this.TimeStamp );                                    
60            }                                          
61                                                      
62            #endregion                                          
63      }                                                
64                                                      
65      public void WriteToAndReadFromQue ( )                                                
66      {                                                
67            try                                          
68            {                                          
69                  // open the que                                    
70                                                      
71                  if ( System.Messaging.MessageQueue.Exists ( nsMe.constsQuePath ) )                                    
72                  {                                    
73                        this.moQue = new System.Messaging.MessageQueue ( nsMe.constsQuePath   ) ;                               
74                  }                                    
75                  else                                    
76                  {                                    
77                        this.moQue = System.Messaging.MessageQueue.Create ( nsMe.constsQuePath , false ) ;                              
78                  }                                    
79                                                      
80                  this.moQue.SynchronizingObject = this ;                                     
81                                                      
82                  // write to the que                                    
83                  System.Random oRandom = new System.Random ( System.DateTime.Now.Millisecond ) ;                                    
84                                                      
85                  double dValue = oRandom.NextDouble ( ) * 1000000.00 ;                                    
86                                                      
87                  nsTest.TagInfo oTag                                    
88                        #NAME?                              
89                        ( this.moQue.GetAllMessages ( ).Length + 1 , dValue , System.DateTime.Now )      ;                        
90                                                      
91                                                      
92                  System.Messaging.Message oMessage                                    
93                        = new System.Messaging.Message ( oTag  ) ;                              
94                                                      
95                  oMessage.Priority =                                     
96                        System.Messaging.MessagePriority.Highest ;                               
97                                                      
98                                                      
99                  this.moQue.Send ( oMessage , sName ) ;                                     
100                                                      
101                                                      
102                  this.moQue.Close ( ) ;                                    
103                                                      
104                  // read from the que                                    
105                                                      
106                  System.Data.DataTable oMessageTable                                     
107                                    = new System.Data.DataTable ( ) ;                   
108                                                      
109                  oMessageTable.Columns.Add ( "Name" ) ;                                     
110                  oMessageTable.Columns.Add ( "SyncId" , System.Type.GetType ( "System.Guid" ) ) ;                                    
111                  oMessageTable.Columns.Add ( "NumericId" , System.Type.GetType ( "System.Int32" ) ) ;                                     
112                  oMessageTable.Columns.Add ( "Value" , System.Type.GetType ( "System.Double" ) ) ;                                    
113                  oMessageTable.Columns.Add ( "TimeStamp" , System.Type.GetType ( "System.DateTime" ) ) ;                                    
114                  oMessageTable.Columns.Add ( "Priority" ) ;                                     
115                                                      
116                  System.Messaging.Message [ ] aoMessages ;                                     
117                                                      
118                  aoMessages = this.moQue.GetAllMessages ( ) ;                                     
119                                                      
120                  System.Messaging.Message oMessage = null ;                                    
121                                                      
122                  for ( int iIndex = 0 ; iIndex < aoMessages.Length ; iIndex++ )                                     
123                  {                                    
124                        oMessage = aoMessages [ iIndex ] ;                              
125                                                      
126                        oMessage.Formatter = this.GetMessageFormatter ( ) ;                              
127                                                      
128                        System.Diagnostics.Debug.WriteLine                               
129                              ( oMessage.Body.GetType ( ).ToString ( ) ) ;                        
130                                                      
131                        nsTest.TagInfo oTagInfo                              
132                              = this.ConvertMessageToTagInfo ( oMessage ) ;                         
133                                                      
134                        oMessageTable.Rows.Add       (                         
135                              new object [ ]{                         
136                                                        oMessage .Label ,
137                                                        oTagInfo.SyncId  ,
138                                                        oTagInfo.NumericId ,
139                                                        oTagInfo.Value ,
140                                                        oTagInfo.TimeStamp ,
141                                                        oMessage .Priority.ToString ( )
142                                                  }       
143                                          ) ;             
144                  }                                     
145                                                      
146                  moGrid.DataSource = oMessageTable ;                                     
147                                                      
148            }                                          
149            catch ( System.Exception ex )                                          
150            {                                          
151                  System.Diagnostics.Debug.Fail ( ex.Message ) ;                                    
152                  throw ex ;                                    
153            }                                          
154      }                                                

0
eddyevations
Asked:
eddyevations
1 Solution
 
Rodney HelsensCommented:
you may try specifying the .Formatter property on the Queue, I ran into a problem pulling objects from MSMQ and solved it by explicitly setting XmlFormatter

      
this.moQue.Formatter = new XmlMessageFormatter(new Type[]{typeof(TagInfo)});
 
0
 
eddyevationsAuthor Commented:
Thanks, I did have this code in there, i just forgot to post it as it was all in separate methods

System.Type [ ] aoTypes = { System.Type.GetType ( "nsTest.TagInfo" ) } ;
this.moQue.Formatter = new System.Messaging.XmlMessageFormatter ( aoTypes ) ;

I changed this to

System.Type [ ] aoTypes = { typeof (  nsTest.TagInfo ) } ;
this.moQue.Formatter = new System.Messaging.XmlMessageFormatter ( aoTypes ) ;

and it worked.

Thank you very much

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now