Error with CSLA + MEF + Serialization

Error with CSLA + MEF + Serialization

Old forum URL:

viniciusmaretti posted on Monday, May 28, 2012

I'm trying to import a class with mef and when it runs on server side it works well, but when i try to do this on client side(silverlight) i get this error :

(I've translated the error, sorry for any mistake)

The type 'Cra.DynamicRules.Minimo' with contract name 'Minimo:' is not expected. Consider using the DataContractResolver or add some statically unknown types to the known types using the KnownTypeAttribute or adding then to known type transmitted to the DataContractSerializer.

When i put the SerializableAttribute in my class i get an exception in run-time complaining that the base class is not marked as serializable. There comes my problem. I'm using CSLA framework on my base class and i can't add the attribute i need and just recompile it.

Can anyone definitely save me? I've been trying everything for one week. Thanks in advance.

JonnyBee replied on Monday, May 28, 2012

Is it a rule that you import by MEF?

I think we need to see a bit more of your code to see the actual problem.

It seems like the rule is set in a "member" variable/property of the BO as the DataContractSerializer tries to serialize the object. This should not happend because Rules should only be registered in AddBusinessRules independently on both .NET and SL and the BrokenRuleCollection should be the only "rule" collection to be transferred on the wire.


viniciusmaretti replied on Monday, May 28, 2012

Well, i`m not at work right now so i don`t have acess to codes at this moment.

But basically what i`m trying to do is load business rules with mef. 

Both SL and .NET sides have acess to my business rules repository by a DAL factory. I used the methods (SL) DataPortal.BeginExecute and (.NET)DataPortal.Execute to reach it, and then i get my business rules returning an MobileList<IBusinessRules>.

In the BO, on AddBusinessRules method i did :

foreach rule in list



Note : I have an assembly with the BusinessRules and then i`ve created an DyrectoryCatalog passing the  path of this assembly. Through a container i get the exported values of type IBusinessRules. 


Hope i was clear enough, if not, tomorrow i post some codes.


JonnyBee replied on Wednesday, May 30, 2012

Hmmm. that's not going to work.

BusinessRules is typically bound to a static IPropertyInfo object (PrimaryProperty,, InputProperties, AffectedProperties) and cannot be serialized. Actually - BusinessRules are not meant to be serialized and does not support Mobile formatter.

You should create your own "meta" information on rules (like a ReadOnly / ReadOnlyList) and then create BusinessRule instances with actual PropertyInfo's in <bo>.AddBusinessRules() method.

viniciusmaretti replied on Wednesday, May 30, 2012

I've done this way and seems that it will work.

Thank you very much!

Copyright (c) Marimer LLC