PDA

View Full Version : Spring 1.1.0P3 can't instantiate a System.Windows.Forms.Form subclass


ironcladlou
03-14-2007, 02:19 AM
For some reason, Spring.Core (1.1.0P3 with .NET 2.0) can't instantiate my System.Windows.Forms.Form subclass. Every other object I've defined works perfectly. Whenever I try to instantiate the form, I receive the following nondescript error:

Error creating object with name 'MainForm' defined in 'config [objects]' : Initialization of object failed : Cannot instantiate Type [Aristocrat.UI.WinForms.BuddyForm] using ctor [Void .ctor()] : 'Exception has been thrown by the target of an invocation.'

The class itself is a simple subclass of System.Windows.Form. Here is the top of the class definition:


namespace Aristocrat.UI.WinForms {
public partial class BuddyForm : Form {
private Session session ;

public BuddyForm() {
...


Here is the program's entry point, Program.cs. The bold/red line is the line at which the exception is thrown:


namespace Aristocrat.UI.WinForms {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
IApplicationContext ctx = ContextRegistry.GetContext();
Form mainForm = (Form) ctx.GetObject("MainForm");
Application.Run(mainForm);
}
}
}


And finally, here is my app.config:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object id="MainForm" type="Aristocrat.UI.WinForms.BuddyForm, Aristocrat.UI.WinForms" singleton="false">
<property name="session" ref="MockSession" />
</object>

<object id="MockSession" type="Aristocrat.Core.Messaging.Impl.MockSession, Aristocrat.Core.Messaging" singleton="false"/>
<object id="AimSession" type="Aristocrat.Core.Messaging.Impl.AimSession, Aristocrat.Core.Messaging" singleton="false">
<property name="clientAuthKey" value="..." />
<property name="screenname" value="..." />
<property name="password" value="..." />
</object>
</objects>
</spring>
</configuration>


What gives? The only difference between the form class and my other classes is that the form class is defined as a partial class.

Bruno Baia
03-14-2007, 07:20 AM
Hi,

Look for inner exceptions to get more information about the problem.


Bruno

ironcladlou
03-14-2007, 06:24 PM
Hi,

Look for inner exceptions to get more information about the problem.

Bruno

The exception I posted is also the message from the innermost exception.

Aleks Seovic
03-15-2007, 04:31 PM
Can you doublecheck that 'Aristocrat.UI.WinForms' is indeed the name of the assembly your form class is in and that assembly is accessible?

Also, could you post the implementation of the default constructor -- error message leads me to believe that something in it might be throwing exception and causing the problem...

Partial class is definitely not the problem, I've used Spring.NET to instantiate and configure Form instances in the past, so it's definitely supported.

- Aleks

ironcladlou
03-16-2007, 12:55 AM
Can you doublecheck that 'Aristocrat.UI.WinForms' is indeed the name of the assembly your form class is in and that assembly is accessible?

Also, could you post the implementation of the default constructor -- error message leads me to believe that something in it might be throwing exception and causing the problem...

Partial class is definitely not the problem, I've used Spring.NET to instantiate and configure Form instances in the past, so it's definitely supported.

- Aleks

You led me to the answer! Turns out, I was wiring an event to an injected property before the injection occured. I moved the wiring out to the Load event of my form, which occurs after the entire form is initialized. Everything is now fine. Thanks! :D