2
votes

I have a SilverLight lib (dll) that implements classes I need to use in a .net project. I follewed the steps described in this answer, but I don't see how to use the proxy library to expose the classes I need. I added a references to the proxy in the .net project. The dll is for silverlight 5 and I'm using .net 4.0.

If I just add the dll as a reference, I can use the SL objects, but I can't serialize them.

Here's the problematic code:

    static string SerializeWithDCS(object obj)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
        MemoryStream ms = new MemoryStream();
        dcs.WriteObject(ms, obj);
        return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
    }

Serialization fails with:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization, Versio
n=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system ca
nnot find the file specified.                                                                         
File name: 'System.Runtime.Serialization, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea
7798e'                                                                                                
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int
32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)            
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeType
Handle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)                    
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Ty
pe[] genericMethodArguments)                                                                          
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, Me
tadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decora
tedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList deriv
edAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolea
n& isVarArg)                                                                                          
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 
decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)                   
   at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inheri
t)                                                                                                    
   at System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)                               
   at System.Runtime.Serialization.CollectionDataContract.IsCollectionOrTryCreate(Type type, Boolean t
ryCreate, DataContract& dataContract, Type& itemType, Boolean constructorRequired)                    
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id
, RuntimeTypeHandle typeHandle, Type type)                                                            
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidati
on(Int32 id, RuntimeTypeHandle typeHandle, Type type)    
1
You are probably experiencing the same problem as in this question and answer. Woudl it be an option to create a .NET library and include the Silverlight source code?Anders Gustafsson
Did you try to turn your Silverlight project into a Portable Class Library? visualstudiogallery.msdn.microsoft.com/… - Another alternative is the Project Linker: visualstudiogallery.msdn.microsoft.com/…herzmeister
@AndersGustafsson: I don't have the source.user1367401
I have no idea if this will work, but what if you try to use a serializer that is not available in Silverlight? In particular I am thinking of NetDataContractSerializer which according to the documentation should be able to serialize types to which the DataContractAttribute has been applied.Anders Gustafsson
Nah, silly idea! Tried it, and it didn't work either.Anders Gustafsson

1 Answers

0
votes

Write your own serialization using reflection.

That's the best solution I could come up with - may a better man provide a superior answer.

The problem indeed appears to be the attributes from System.Runtime.Serialization which differ between .NET and Silverlight. This was talked about in the question linked to by Anders Gustafsson.

I'm puzzled why I couldn't get this to work.

I tried creating an AppDomain and load all the necessary assemblies into it:

  1. A test dll posing as the third-party assembly,
  2. a Silverlight wrapper assembly that will do the serialization,
  3. all Silverlight 5 core assemblies and
  4. the Silverlight 5 variant of System.Runtime.Serialization.

Then I called the code in the wrapper assembly that would invoke the serialization - and I still got the exception.

It's puzzling - and maybe I just did something wrong.

I'd be very interested if someone more knowledgeable has something to say about this.

In any case, it is possible to write a custom serializer and I think this will solve the problem.