@@ -168,6 +168,57 @@ StructureFieldParameter structureFieldParameter
168
168
baseType [ structureFieldParameter . Name ] = null ;
169
169
EncodeDecodeComplexType ( EncoderContext , memoryStreamType , encoderType , jsonEncodingType , StructureType . Union , nodeId , emittedType ) ;
170
170
}
171
+
172
+ /// <summary>
173
+ /// Verify serialize/encode of a structured type initiated from outside of an IEncoder instance.
174
+ /// </summary>
175
+ [ Theory ]
176
+ [ Category ( "ComplexTypes" ) ]
177
+ public void ReEncodeComplexTypeScopedContext (
178
+ MemoryStreamType memoryStreamType ,
179
+ StructureType structureType
180
+ )
181
+ {
182
+ ExpandedNodeId nodeId ;
183
+ Type complexType ;
184
+ ( nodeId , complexType ) = TypeDictionary [ structureType ] ;
185
+ object emittedType = Activator . CreateInstance ( complexType ) ;
186
+ var baseType = emittedType as BaseComplexType ;
187
+ FillStructWithValues ( baseType , true ) ;
188
+
189
+ ExtensionObject extensionObject = new ExtensionObject ( emittedType ) ;
190
+
191
+ Opc . Ua . KeyValuePair keyValuePair = new Opc . Ua . KeyValuePair ( ) ;
192
+ keyValuePair . Key = "AKEY" ;
193
+ keyValuePair . Value = extensionObject ;
194
+
195
+ ServiceMessageContext localCtxt = ( ServiceMessageContext ) EncoderContext ;
196
+
197
+ // Serialize/Encode a Variant fails without a context available
198
+ Assert . Throws (
199
+ typeof ( Newtonsoft . Json . JsonSerializationException ) ,
200
+ ( ) => Newtonsoft . Json . JsonConvert . SerializeObject ( keyValuePair ) ) ;
201
+
202
+ // Serialize/Encode an ExtensionObject fails without a context available
203
+ var extObjToEncode = new ExtensionObject ( keyValuePair ) ;
204
+ Assert . Throws (
205
+ typeof ( Newtonsoft . Json . JsonSerializationException ) ,
206
+ ( ) => Newtonsoft . Json . JsonConvert . SerializeObject ( extObjToEncode ) ) ;
207
+
208
+ // Serialize/Encode a Variant succeeds with a context available
209
+ using ( MessageContextExtension . SetScopedContext ( localCtxt ) )
210
+ {
211
+ _ = Newtonsoft . Json . JsonConvert . SerializeObject ( keyValuePair ) ;
212
+ }
213
+
214
+ // Serialize/Encode an ExtensionObject succeeds with a context available
215
+ using ( MessageContextExtension . SetScopedContext ( localCtxt ) )
216
+ {
217
+ _ = Newtonsoft . Json . JsonConvert . SerializeObject ( extensionObject ) ;
218
+ }
219
+
220
+
221
+ }
171
222
#endregion Test Methods
172
223
}
173
224
}
0 commit comments