❔ Losing bytes when delivering dynamic object via RabbitMQ

Hey there got a bit of a complex problem. Got this code:
public static object ToDynamicClass(this ExpandoObject expando)
{
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public);

foreach (var kvp in expando)
{
Type type = kvp.Value?.GetType() ?? typeof(object);
FieldBuilder field = typeBuilder.DefineField("_" + kvp.Key, type, FieldAttributes.Private);
PropertyBuilder property = typeBuilder.DefineProperty(kvp.Key, PropertyAttributes.HasDefault, type, null);

MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_" + kvp.Key, getSetAttr, type, Type.EmptyTypes);
ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
currGetIL.Emit(OpCodes.Ldarg_0);
currGetIL.Emit(OpCodes.Ldfld, field);
currGetIL.Emit(OpCodes.Ret);

//Some code removed because of character limit

property.SetGetMethod(currGetPropMthdBldr);
property.SetSetMethod(currSetPropMthdBldr);
}

Type objType = typeBuilder.CreateType();

object obj = Activator.CreateInstance(objType);
foreach (var kvp in expando)
{
objType.GetProperty(kvp.Key)?.SetValue(obj, kvp.Value);
}

return obj;
}
public static object ToDynamicClass(this ExpandoObject expando)
{
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public);

foreach (var kvp in expando)
{
Type type = kvp.Value?.GetType() ?? typeof(object);
FieldBuilder field = typeBuilder.DefineField("_" + kvp.Key, type, FieldAttributes.Private);
PropertyBuilder property = typeBuilder.DefineProperty(kvp.Key, PropertyAttributes.HasDefault, type, null);

MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_" + kvp.Key, getSetAttr, type, Type.EmptyTypes);
ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
currGetIL.Emit(OpCodes.Ldarg_0);
currGetIL.Emit(OpCodes.Ldfld, field);
currGetIL.Emit(OpCodes.Ret);

//Some code removed because of character limit

property.SetGetMethod(currGetPropMthdBldr);
property.SetSetMethod(currSetPropMthdBldr);
}

Type objType = typeBuilder.CreateType();

object obj = Activator.CreateInstance(objType);
foreach (var kvp in expando)
{
objType.GetProperty(kvp.Key)?.SetValue(obj, kvp.Value);
}

return obj;
}
Before when i tried to send the ExpandoObj itself all worked well. Now Im sending a lift of objects the above methods produces using rabbitmq. when i seralize i got a 1500 bytes messege size, when i recive the messege its only 120 bytes. Any ideas what could be wrong?
7 Replies
antimatter8189
antimatter8189OP15mo ago
In addition all the properties get lost, literally get "Dynamic Type" at the UI layer with nothing in it The data loss occurs during transit somehow
Omnissiah
Omnissiah15mo ago
what object are you using to deserialize this
antimatter8189
antimatter8189OP15mo ago
well its a BaseDTO that contains a List<objecT> and those are located in it
var converter = new ExpandoObjConverter;
var result = JsonConvert.DeseralizeObject<ExpandoObject>(json,converter);
rsultList.add(result)
dto.Result=resultList;
return DTO;
var converter = new ExpandoObjConverter;
var result = JsonConvert.DeseralizeObject<ExpandoObject>(json,converter);
rsultList.add(result)
dto.Result=resultList;
return DTO;
The above code works, but if i add a line of
result= ToDynamicClass(result)
result= ToDynamicClass(result)
and send that list, all breaks no c# gods to help out on this?
Omnissiah
Omnissiah15mo ago
have you tried using a dictionary instead of expando did you look at the serialized object in the rabbit ui did you try sending a message using rabbit ui to check deserialization of problematic fields
antimatter8189
antimatter8189OP15mo ago
expando uses dictionary under the hood, what u mean seralize in rabbitui? i reverted to using custom json converters, still working on it
Omnissiah
Omnissiah15mo ago
i mean reading (and eventually sending) the message from the queue in the ui
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server