Domanda

I'm building a MVC 4 application with Spring support.

My Web.config looks like following

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

...

<spring>
    <context type="Spring.Context.Support.MvcApplicationContext, Spring.Web.Mvc3, Version=1.3.2.40943, Culture=neutral, PublicKeyToken=65e474d141e25e07">
      <!--<resource uri="config://spring/objects"/>-->
    </context>

    <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
      <object type="Org.Zighinetto.MyNS.RepositoryHelper" singleton="true" id="Org.Zighinetto.RepositoryHelper">
        <property name="SessionFactory" ref="Org.Zighinetto.SessionFactory"/>
      </object>

      <object id="Org.Zighinetto.Ecommerce.NHibernateHelper" type="MvcTest.Utils.NHibernateHelper" singleton="true"/>
      <object id="Org.Zighinetto.Ecommerce.SessionFactory" type="NHibernate.ISessionFactory" factory-object="Org.Zighinetto.Ecommerce.NHibernateHelper" factory-method="CreateSessionFactory" />
    </objects>
</spring>

In my Controller I want to get a reference to that object that basically wraps all FNH DAOs (I called them Repositories...)

public CustomerController()
        {
            IApplicationContext ctx = new MvcApplicationContext();


            RepositoryHelper repoHelper = (RepositoryHelper)ctx.GetObject("Org.Zighinetto.RepositoryHelper");
            _customerRepository = repoHelper.CustomerRepository;
        }

GetObject call crashes with following exception

No object named 'Org.Zighinetto.RepositoryHelper' is defined : Cannot find definition for object [Org.Zighinetto.RepositoryHelper]

[NoSuchObjectDefinitionException: No object named 'Org.Zighinetto.RepositoryHelper' is defined : Cannot find definition for object [Org.Zighinetto.RepositoryHelper]]
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
   Spring.Context.Support.AbstractApplicationContext.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1538

Am I initializing Spring.net context bad? What can I do to obtain that object reference?

[Add]

ctx.GetObjectDefinitionNames() returns empty string. So no object has been defined Initializing AppContext with new MvcApplicationContext("~/Web.Config") doesn't change

È stato utile?

Soluzione

I fixed using IApplicationContext ctx = WebApplicationContext.GetRootContext()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top