Here is the description of my problem. It occurs when using Spring.NET dependency injection:
- Create a new Asp.NET project in C# with Visual Studio 2010 SP1.
- Add a reference to Spring.Core (version 0.0.0.40704)
- Add a new Telerik report called Report1.cs (cancel the wizard).
- Add a new Telerik report called Report2.cs (cancel the wizard).
- Compile the solution.
- Add a new class in your project named Class1.
- Add a new class in your project named Class2.
- Add a new interface named IClass2 and make Class2 implement it.
- In Class1 add the following code:
public
int
GetValue(Class2 myObject)
{
return
1;
}
In the code behind of the Global.asax file add the following field and the following method:
private
static
IApplicationContext _applicationContext;
public
static
T GetInstance<T>(String objectName)
{
return
(T)_applicationContext.GetObject(objectName);
}
- Update the method Application_Start so that it looks like the following:
void
Application_Start(
object
sender, EventArgs e)
{
// Code that runs on application startup
_applicationContext = ContextRegistry.GetContext();
}
- Add the required Spring namespaces in the class (Spring.Context and Spring.Context.Support).
- The configuration of Spring.NET doesn't matter here as we will see later. Actually you don't even need to configure Spring.NET
- In Report1, in the detail section add a sub-report. Set its ReportSource property to point to Report2.
- In Report2, add an ObjectDataSource.
- Set the DataSource property of the ObjectDataSource to Class1.
- Set the DataMember property of the ObjectDataSource to GetValue.
- Keep the parameter value empty.
- In the code-behind of Report2, update the constructor so that it looks like this:
public
Report2()
{
//
// Required for telerik Reporting designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
if
(!DesignMode)
{
IClass2 myObject = Global.GetInstance<IClass2>(
"Class2"
);
objectDataSource1.Parameters[
"myObject"
].Value = myObject;
}
}
- If it is open, close the Report1 designer. Compile. Reopen the designer. You get the following errors:
- "Object reference not set to an instance of an object."
- "The variable 'report21' is either undeclared or was never assigned."
If you check the designer-generated code of Report1 you will notice that the variable report21 is correctly declared AND assigned.
Now, go back to Global.asax code-behind and update the GetInstance method so that it looks like this;
public
static
T GetInstance<T>(String objectName)
{
return
(T)(
object
)
new
Class2();
}
- If it is open, close the Report1 designer. Compile. Reopen the designer. It's working fine.
My understanding is that the property DesignMode does not work as expected when it is used from within a SubReport. The code in GetInstance should never be called from within the designer. The fact that I have an error when using Spring.NET (probably due to the fact that my config doesn't handle design-time mode correctly but it's another issue) means that this piece of code is actually called at design time. If I open Report2 directly, it is showing as expected.
My issue is the following: I cannot open a report that contains a subreport which constructor is used to handle parameter of type object which are retrieved through dependency injection with Spring.NET
Am I doing something wrong? Is there any fix or workaround for this issue?
Thanks a lot for your help