Spring.NET 1.1 on Mono
Just managed to make a patch to my Spring.Core.dll such that I could get the basic IoC examples working under mono. This will fix applications such as the demo MovieLister, and the fix is fairly simple.
When you run the Spring.NET MovieLister app under Mono you’ll receive a stack trace which looks like this:
Unhandled Exception: System.Configuration.ConfigurationErrorsException: Error instantiating context ’spring.root’. () () —> Spring.Objects.FatalObjectException: Cannot instantiate Type [Spring.Context.Support.XmlApplicationContext] using ctor [Void .ctor(String, Boolean, String[])] : ‘Exception has been thrown by the target of an invocation.’ —> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.NullReferenceException: Object reference not set to an instance of an object
at Mono.Xml.Schema.XmlSchemaValidatingReader.get_Prefix () [0x00000]
at System.Xml.XmlDocument.ReadAttributeNode (System.Xml.XmlReader reader) [0x00000]
at System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) [0x00000]
at System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) [0x00000]
at System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) [0x00000]
at Spring.Objects.Factory.Xml.XmlObjectDefinitionReader.LoadObjectDefinitions (IResource resource) [0x00000]
The exception is a result of Mono not implementing XmlSchema.Add. The following is my fix that you need to apply to Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionReader.cs.
Firstly, include the System namespace on Line 23
using System;
Next, on line 153, I make it such that if your application is running under mono, the schema isn’t validated against. Everything will perform as normal if you’re running under the .NET platform. Replace the line that reads:
reader = XmlUtils.CreateValidatingReader(stream, XmlParserRegistry.GetSchemas(),
new ValidationEventHandler(HandleValidation));
with:
// Enable validating reader when not running under mono
Type monoIdentifier = Type.GetType("Mono.Runtime");
if (monoIdentifier == null)
{
reader = XmlUtils.CreateValidatingReader(stream, XmlParserRegistry.GetSchemas(),
new ValidationEventHandler(HandleValidation));
}
else
{
reader = XmlReader.Create(stream);
}
It’s a simple fix, but means I can get my Spring.NET based application to run under mono! I’ve provided my compiled Spring.Core.dll for download. I won’t be supporting the change, so If you want to be able to make future edits, I recommend you download the latest version from the Spring.NET site and patch the source yourself.
Final note, this only seems to fix my troubles for Mono on Windows. Looking at addressing the Linux issues asap. If you’ve got Spring.NET playing nicely on Linux Mono, please leave a comment. *Update* - Seems to only be occurring on my Ubuntu Edgy Box. Seems to work fine on Fedora!
Comments(4)