2
votes

I have an application which embedes (via BuildAction: Embedded Resource) referenced assembly (called ClassLibrary1) inside itself and loads it on AppDomain.CurrentDomain.AssemblyResolve event. Main assembly defines a class Class1:

public class Class1
{        
    public Class2 MyField { get; set; }    
}

It has a property of type Class2 defined in ClassLibrary1. Definition of Class2:

public class Class2
{
    public int A { get; set; }
}

In the main method I`m creating a new XmlSerializer(typeof(Class1)):

    static void Main()
    {
        SubscribeAssemblyResolver();
        MainMethod();
    }

    private static void MainMethod()
    {
        XmlSerializer xs2 = new XmlSerializer(typeof(Class1));
        Class1 cl = new Class1();
    }

While executing a programm I get the following error:

Unable to generate a temporary class (result=1). error CS0012: The type 'ClassLibrary1.Class2' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c06f123f2868e8c8'. error CS0266: Cannot implicitly convert type 'object' to 'ClassLibrary1.Class2'. An explicit conversion exists (are you missing a cast?)

Any ideas?

The rest of the code:

    private static void SubscribeAssemblyResolver()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);            
    }

    static Dictionary<String, Assembly> _assemblies = new Dictionary<String, Assembly>(StringComparer.OrdinalIgnoreCase);

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        return ResolveAssembly(args.Name);
    }

    private static Assembly ResolveAssembly(string argsName)
    {
        Assembly dll;
        var name = "WindowsFormsApplication1.Libs." + new AssemblyName(argsName).Name + ".dll";
        if (!_assemblies.TryGetValue(name, out dll))
        {
            Assembly res = typeof(Program).Assembly;
            using (var input = res.GetManifestResourceStream(name))
            {
                if (input == null)
                {
                    //TODO: log
                    return null;
                }
                Byte[] assemblyData = new Byte[input.Length];
                input.Read(assemblyData, 0, assemblyData.Length);
                if (null == (dll = Assembly.Load(assemblyData)))
                {
                    //TODO: log
                    return null;
                }
                //TODO: log
                _assemblies[name] = dll;
                return dll;
            }
        }
        return dll;
    }

UPDATE: Created a BUG on the microsoft Connect site. You can also download a sample visual stuido 2010 solution (just expand Details fieldgroup) from there to reproduce it.

4
This bug is not limited to dynamically loaded assemblies. See Error creating XML Serializer in specific situation in VB. Unfortunately, XmlSerializer bugs are not being fixed unless they are security-critical.John Saunders
Thanks for the link, John! Unfortunatelly, specified solution in workarounds for the mentioned bug are not working in my case: XmlSerializer xs2 = new XmlSerializer(typeof(Class1), new Type[] { typeof(Class2) }); I still get the same errors.Alexzander
ok, perhaps the reason the workaround doesn't work for you is because of your dynamic loading. As an experiment, can you try it with static loading and 1) see if you still get the error, and 2) see if the workaround works for you with static loading. If the problem is the dynamic loading, then I suggest you enter a new Connect article and post the URL here.John Saunders
@John: there are no problems if it is loaded statically (e.g. ClassLibrary1.dll is located in the same folder as ConsoleApplication1 and is loaded via standard .net mechanism) I`ve created a new Connect bug and added a vs2010 solution, a link to it is in the post.Alexzander

4 Answers

0
votes

I've solved similar problem by saving assembly in temporary folder

    public static byte[] ReadFully(Stream input)
    {
        var buffer = new byte[16 * 1024];
        using (var ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

    public App()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            var assemblyName = new AssemblyName(args.Name);

            if (assemblyName.Name != "Omikad.Core")
                return null;

            var resourceName = "Terem." + assemblyName.Name + ".dll";

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                if (stream == null)
                    return null;

                var assemblyData = ReadFully(stream);
                var tmp = Path.Combine(Path.GetTempPath(), "Omikad.Core.dll");
                File.WriteAllBytes(tmp, assemblyData);
                return Assembly.LoadFrom(tmp);
            }
        };
    }
0
votes

Try to add atribute:

[XmlInclude(typeof(Class2))]
public class Class1
{        
   public Class2 MyField { get; set; }    
}
0
votes

As for now I`ve ended up with two somewhat bad solutions:

  1. While you can`t instanciate XmlSerializer for the type Class1, you still can instanciate it for the type Class2 from the main assembly. That does mean that if you move Class1 to ClassLibrary1 or Class2 to the main assembly - it will deserialize without errors. It works, but it is not possible to use this solution everywhere, plus it is ideologically wrong.
  2. Use ILMerge to merge those assemblies into one. But it only works for non-wpf stuff, plus you should manage the situation with the assemblies attributes (there could be conflicts).

And one very bad idea:

  1. Generate ClassLibrary1.XmlSerializer.dll with sgen.exe.
  2. Also embed it into the main assembly.
  3. Explicitly load it to the XmlSerializer cache calling one of it`s internal methods via reflection.

Although I had to use solution number one for now, I`m not satisfied with it, because it is too constraining.

0
votes

I'd try the XmlSerializer(Type, Type[]) constructor and provide Class2 as an additional type using the second parameter. I've few experience with the XmlSerializer, but for DataContractSerializer this does the trick.