Introductory dependency in an MVC controller using Spring.NET

An object in the controller is not entered 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 run time, ObjectA is not injected and remains null, which causes a complete exception in the code.

Alternative: I can manually initialize the Spring object and get it with the following code.

        var ctx = ContextRegistry.GetContext();
        var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
+5
source share
2 answers

Turns out I was missing a pretty important part of the Spring implementation for MVC.

My solution to the problem was to add a DependencyResolver that implements 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()));
    }
+3
source

IDependencyResolver, . , (iirc) 1.3.1 Spring.NET asp.net mvc 2.0 3.0. Spring.net 2.0 ( , NuGet) 4.0. .

SpringDependencyResolver , Spring.net.

+1

All Articles