开发者

How to do Spring.NET DI injection in action filters (ASP.NET MVC 3)

Tried to search but no specific answers (I am very new in this)...

With Spring.NET supporting ASP.NET MVC 3, how can I do dependency injection inside action filters? Studied around but I have no clue at all! :/

On Spring.NET documentary, it appears to mention that this has all been taken care of, all that's needed to do is to just register the action filter into spring's context and spring will take care of all the necessary injection process.

http://www.springframework.net/doc-latest/reference/html/web-mvc3.html

However, I just can't get it to work. Studying around blogs and etc, there are mentions of using filter providers, or directly using ContextRegistry.GetContext(), but this doesn't appear to adhere to AOP..

Using Spring.Net to inject dependencies into ASP.NET MVC ActionFilters

Spring.Net & Attribute Injection

So what's the proper way to do this? I thought this is already covered by spring.net, but it appears that it isn't.

I referenced to Spring.Web.Mvc3 v4.0.30319, and I am running on ASP.NET MVC 3.

EDIT:

I tried this:

SpringMvcDependencyResolver dr = 
       new SpringMvcDependencyResolver(ContextRegistry.GetContext()); 

and then getting the service via dr.GetService<ITestService>(); This seems to make it work somewhat, but instead of getting the correct actual service implementation (on my Filters.xml, I wrote the property ref to TestService), it开发者_StackOverflow中文版 returns the Stub version of the service. Did I do it wrong? Or do I have to sift through and pick the right one or something via GetServices()?

EDIT2:

@Andreas: Sorry for the late reply. But, my Global.asax DOES have it in.

public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication

To test, I have this in my spring config:

<object type="Spring.Mvc3QuickStart.Filters.TestFilter, Spring.Mvc3QuickStart" singleton="false" >
  <property name="Message" value="This is the ActionFilter message" />
</object>

In my filter:

public class TestFilter : ActionFilterAttribute
{
    public string Message { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewBag.ActionMessage = Message;
        base.OnActionExecuting(filterContext);
    }
}

In my controller I have this:

ViewData["Message"] = Message;

So purely for this test project, the Filter should have the message injected. But the output isn't as expected.


The 1.3.2 release of Spring.Net.Mvc3 seams to be a bit buggy. In SpringMvcDependencyResolver the method public IEnumerable<object> GetServices(Type serviceType) will always fail. It should be return services.Values.Cast<object>(); instead of return services.Cast<object>();. The simplest fix is to override BuildDependencyResolver of SpringMvcApplication and register your own fixed SpringMvcDependencyResolver.

I created a complete solution hosted here: https://gist.github.com/1262927

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜