DynamicObject and WCF support
Asked Answered
H

2

9

I was wondering if anyone has had any luck getting a DynamicObject to serialize and work with WCF?

Here’s my little test:

[DataContract]
class MyDynamicObject : DynamicObject
{
    [DataMember]
    private Dictionary<string, object> _attributes =
       new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string key = binder.Name;

        result = null;

        if (_attributes.ContainsKey(key))
            result = _attributes[key];

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _attributes.Add(binder.Name, value);

        return true;
    }
}

var dy = new MyDynamicObject();
var ser = new DataContractSerializer(typeof(MyDynamicObject));
var mem = new MemoryStream();
ser.WriteObject(mem, dy);

The error I get is:

System.Runtime.Serialization.InvalidDataContractException was unhandled Message=Type 'ElasticTest1.MyDynamicObject' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'System.Dynamic.DynamicObject' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

Any suggestions?

Hap answered 20/4, 2010 at 0:51 Comment(6)
The error message tells that you cannot do this unless you change System.Dynamic.DynamicObject which you can't because it's part of the .NET Framework. So you can't do this. What are you trying to achieve, maybe there's a different way to the same goal?Vile
Of course while you can't change DynamicObject it is open source (dlr.codeplex.com) so you could copy it and modify it.Yerkovich
I am trying to send a DynamicObject down the wire to a SL 4 client.Hap
(no, it isn't going to like that)Anabal
I'm curious as to why you chose to use dynamic objects to serialize out to SL client. What do you objects represent: data from a database, or something altogether different? What makes your data dynamic?Proof
The system is used for an entity based game engine. More info: forums.tigsource.com/index.php?topic=10112.0Hap
B
10

Solution for your problem

Implement custom IDynamicMetaObjectProvider

Bise answered 19/4, 2011 at 10:26 Comment(0)
F
3

Can you use something like Dictionary<TKey, TValue> to achieve this?

I am trying to solve a similar problem. My issue is that I have DTO's to transfer data between client and server. However, you should always have DTO's that are fine grained and flattened.

For example, if a client wants to get Customer's Name and ID and it is not interested in anything else, ideally, you should create a DTO that only has these 2 properties in it. If you were to transfer the same CustomerDTO for all methods, there is a lot of performance implications. You could be transferring a lot of redundant fields.

Fructify answered 18/2, 2011 at 15:28 Comment(1)
I gave up on DynamicObjects and went with DTOs flattened using AutoMapper. It works fairly well for what I need.Hap

© 2022 - 2024 — McMap. All rights reserved.