XmlSerializer fails on object loaded from an activated assembly - XML SOAP

This is a discussion on XmlSerializer fails on object loaded from an activated assembly - XML SOAP ; I'm loading a plugin assembly using Activator.CreateInstanceFrom, and inside this assembly is a settings class which gets serialized to XML. The general code flow is as follows: ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName, type); object plugin = pluginHandle.Unwrap(); .... // Settings is ...

+ Reply to Thread
Results 1 to 3 of 3

XmlSerializer fails on object loaded from an activated assembly

  1. Default XmlSerializer fails on object loaded from an activated assembly

    I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
    inside this assembly is a settings class which gets serialized to XML.

    The general code flow is as follows:

    ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
    type);
    object plugin = pluginHandle.Unwrap();
    ....

    // Settings is from the plugin Assembly loaded by the Activator
    XmlSerializer x = new XmlSerializer(typeof(Settings));
    x.Serialize(settingsFile, _settings);
    // blows up here with this exception:

    System.InvalidOperationException: There was an error generating the
    XML document. ---> System.InvalidCastException: Unable to cast object
    of type '*.Settings' to type '*.Settings'.
    at
    Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSettings.Write3_Settings(Object
    o)


    Which is confusing considering the types are identical. However, when
    I add the following XML to App.Config, the error goes away:

    <system.diagnostics>
    <switches>
    <add name="XmlSerialization.Compilation" value="4" />
    </switches>
    </system.diagnostics>

    What am I missing w.r.t. Activator.CreateInstanceFrom and
    XmlSerializer?

    Thanks.


  2. Default Re: XmlSerializer fails on object loaded from an activated assembly

    On Apr 9, 10:53 am, christopher.watf... wrote:
    > I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
    > inside this assembly is a settings class which gets serialized to XML.
    >
    > The general code flow is as follows:
    >
    > ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
    > type);
    > object plugin = pluginHandle.Unwrap();
    > ...
    >
    > // Settings is from the plugin Assembly loaded by the Activator
    > XmlSerializer x = new XmlSerializer(typeof(Settings));
    > x.Serialize(settingsFile, _settings);
    > // blows up here with this exception:
    >
    > System.InvalidOperationException: There was an error generating the
    > XML document. ---> System.InvalidCastException: Unable to cast object
    > of type '*.Settings' to type '*.Settings'.
    > at
    > Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSettings.Write3_Settings(Object
    > o)
    >
    > Which is confusing considering the types are identical. However, when
    > I add the following XML to App.Config, the error goes away:
    >
    > <system.diagnostics>
    > <switches>
    > <add name="XmlSerialization.Compilation" value="4" />
    > </switches>
    > </system.diagnostics>
    >
    > What am I missing w.r.t. Activator.CreateInstanceFrom and
    > XmlSerializer?
    >
    > Thanks.


    Moreover, *any* type from a loaded assembly (Assembly.Load,
    Assembly.LoadFile, Assembly.LoadFrom, Activator.CreateInstanceFrom)
    fails to be serialized/deserialized.

    Assembly asm = Assembly.LoadFrom(Path.GetFullPath(assemblyName));
    object plugin = asm.CreateInstance(type);
    ((IPlugin)plugin).WriteSettings(textWriter);

    ....

    public class XmlTest { public string Hello = "Hello World!"; }

    public void WriteSettings(TextWriter settingsWriter)
    {
    XmlSerializer x = new XmlSerializer(typeof(XmlTest));
    x.Serialize(settingsWriter, new XmlTest());
    }


    Once again, if I use the diagnostic switches, everything works fine.


  3. Default Re: XmlSerializer fails on object loaded from an activated assembly

    On Apr 9, 11:26 am, christopher.watf... wrote:
    > On Apr 9, 10:53 am, christopher.watf... wrote:
    >
    >
    >
    > > I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
    > > inside this assembly is a settings class which gets serialized to XML.

    >
    > > The general code flow is as follows:

    >
    > > ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
    > > type);
    > > object plugin = pluginHandle.Unwrap();
    > > ...

    >
    > > // Settings is from the plugin Assembly loaded by the Activator
    > > XmlSerializer x = new XmlSerializer(typeof(Settings));
    > > x.Serialize(settingsFile, _settings);
    > > // blows up here with this exception:

    >
    > > System.InvalidOperationException: There was an error generating the
    > > XML document. ---> System.InvalidCastException: Unable to cast object
    > > of type '*.Settings' to type '*.Settings'.
    > > at
    > > Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSettings.Write3_Settings(Object
    > > o)

    >
    > > Which is confusing considering the types are identical. However, when
    > > I add the following XML to App.Config, the error goes away:

    >
    > > <system.diagnostics>
    > > <switches>
    > > <add name="XmlSerialization.Compilation" value="4" />
    > > </switches>
    > > </system.diagnostics>

    >
    > > What am I missing w.r.t. Activator.CreateInstanceFrom and
    > > XmlSerializer?

    >
    > > Thanks.

    >
    > Moreover, *any* type from a loaded assembly (Assembly.Load,
    > Assembly.LoadFile, Assembly.LoadFrom, Activator.CreateInstanceFrom)
    > fails to be serialized/deserialized.
    >
    > Assembly asm = Assembly.LoadFrom(Path.GetFullPath(assemblyName));
    > object plugin = asm.CreateInstance(type);
    > ((IPlugin)plugin).WriteSettings(textWriter);
    >
    > ...
    >
    > public class XmlTest { public string Hello = "Hello World!"; }
    >
    > public void WriteSettings(TextWriter settingsWriter)
    > {
    > XmlSerializer x = new XmlSerializer(typeof(XmlTest));
    > x.Serialize(settingsWriter, new XmlTest());
    >
    > }
    >
    > Once again, if I use the diagnostic switches, everything works fine.


    I have found a solution which, while not perfect, works. The assembly
    seemingly cannot be loaded from outside the ApplicationBase or
    CodeBase or PrivateBinPath. Moving the plugin DLL's to a path
    underneath the working directory solved things.

    DOES NOT WORK:
    \MyExecutable\bin\Debug\MyExecutable.exe
    \MyPlugin\bin\Debug\MyPlugin.dll

    WORKS:
    \MyExecutable\bin\Debug\MyExecutable.exe
    \MyExecutable\bin\Debug\Plugins\MyPlugin.dll

    However, I'm not entirely sure why the XmlSerializer fails to see
    Assemblies loaded from elsewhere. Especially when the diagnostic
    switches make it work!


+ Reply to Thread

Similar Threads

  1. .NET Assembly loaded from NULL in LabVIEW 8.5
    By Application Development in forum labview
    Replies: 3
    Last Post: 11-29-2007, 03:40 PM
  2. Assembly will be loaded from ?
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 09-18-2006, 02:00 PM
  3. Replies: 5
    Last Post: 03-07-2005, 08:34 PM
  4. How to check if object already activated?
    By Application Development in forum Object
    Replies: 3
    Last Post: 03-01-2004, 05:54 AM
  5. Replies: 0
    Last Post: 06-27-2003, 06:51 PM