5

The object in the controller is not getting injected at run time.

Web.config:

    <sectionGroup name="spring">
        <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
        <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>

. . .

<!-- Spring Context Configuration -->
<spring>
    <context>
        <resource uri="config://spring/objects"/>
    </context>
    <objects configSource="App_Config\Spring.config" />
</spring>
<!-- End Spring Context Configuration -->

Spring.config:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

    <!-- Crest is the WCF service to be exposed to the client, could not use a singleton -->

    <object id="TestController" type="Project.Controllers.TestController, Project" singleton="false">
        <property name="ObjectA" ref="ObjectImpl"/>
    </object>

    <object id="ObjectImpl" type="Project.Code.Implementations.ClassA, Project" singleton="false" />

</objects>

TestController:

public class TestController: Controller
    {
        // this object will be injected by Spring.net at run time
        private ClassA ObjectA { get; set; }

Problem:

At runtime the ObjectA does not get injected and stays null, which cause null exception throughout the code.

Alternative: I can manually initialize the Spring object and get it's object using the following code.

        var ctx = ContextRegistry.GetContext();
        var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
Jun Zheng
  • 677
  • 1
  • 15
  • 31
  • 1
    You are missing some pieces of the puzzle. Please have a look on [here](http://foldingair.blogspot.hu/2010/01/springnet-and-aspmvc-getting-started.html) and also [here](http://www.springframework.net/doc-latest/reference/html/web-mvc.html) and [here](http://nuget.org/packages/Spring.Web.Mvc/1.3.2). – nemesv Sep 05 '12 at 19:06
  • @nemesv yes you are correct, it turns out I was missing a piece of the puzzle. Thanks! – Jun Zheng Sep 05 '12 at 19:13

2 Answers2

3

It turns out I was missing a pretty important part of Spring Implementation for MVC.

My solution to the problem was by adding a DependencyResolver which implements the IDependencyResolver.

DependencyResolver:

public class SpringDependencyResolver : IDependencyResolver
{
    private readonly IApplicationContext _context;

    public SpringDependencyResolver(IApplicationContext context)
    {
        _context = context;
    }

    public object GetService(Type serviceType)
    {
        var dictionary = _context.GetObjectsOfType(serviceType).GetEnumerator();

        dictionary.MoveNext();
        try
        {
            return dictionary.Value;
        }
        catch (InvalidOperationException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
            return _context.GetObjectsOfType(serviceType).Cast<object>();
    }
}

Global.asax.cs:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        DependencyResolver.SetResolver(new SpringDependencyResolver(ContextRegistry.GetContext()));
    }
Jun Zheng
  • 677
  • 1
  • 15
  • 31
  • This won't work becaus the context is in creation and `ContextRegistry.GetContext()` will throw an exception! – Tobias May 08 '17 at 12:31
1

You can implement your own IDependencyResolver, like you suggest in your answer. Please note however that since (iirc) version 1.3.1 Spring.NET has built-in support for asp.net mvc 2.0 and 3.0. Spring.net 2.0 (pre release available on NuGet) has built in support for version 4.0 too. Consider using these libraries instead.

You might be interested in comparing your SpringDependencyResolver to the the one provided by the Spring.net team.

Marijn
  • 10,367
  • 5
  • 59
  • 80
  • Thanks @Marijn for the advise. You are correct with the Spring.NET built-in support. But the problem in our case is that we are using MVC4 framework and Spring.net 2.0 is not yet ready for production release. So a custom implementation fits better for our situation. I'll have a look at the DependencyResolver [provided by Spring](https://fisheye.springsource.org/browse/spring-net/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs?hb=true), Thanks! – Jun Zheng Sep 06 '12 at 18:04
  • I tried to use the Spring-provided dependency resolver but in my Global.asax (method init, inheriting from MvcApplication) "DependencyResolver.SetResolver(new SpringDependencyResolver(ContextRegistry.GetContext()));" throws error saying context is still under creation. Any idea on what's wrong in my case? – usr-local-ΕΨΗΕΛΩΝ Dec 29 '12 at 17:46