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 ...
-
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.
-
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.
-
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!
Similar Threads
-
By Application Development in forum labview
Replies: 3
Last Post: 11-29-2007, 03:40 PM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 09-18-2006, 02:00 PM
-
By Application Development in forum DOTNET
Replies: 5
Last Post: 03-07-2005, 08:34 PM
-
By Application Development in forum Object
Replies: 3
Last Post: 03-01-2004, 05:54 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 06-27-2003, 06:51 PM