Constructor Dependency Injection WebApi Attributes

后端 未结 2 769
日久生厌
日久生厌 2020-11-27 21:56

I have been looking around for a non Parameter injection option for the WebApi attributes.

My question is simply whether this is actually possible using Structuremap

2条回答
  •  孤街浪徒
    2020-11-27 22:29

    I struggled with custom action filter providers, without getting it to work for my auth attributes. I also trying out various approaches with constructor and property injection, but did not find a solution that felt nice.

    I finally ended up injecting functions into my attributes. That way, unit tests can inject a function that returns a fake or mock, while the application can inject a function that resolves the dependency with the IoC container.

    I just wrote about this approach here: http://danielsaidi.com/blog/2015/09/11/asp-net-and-webapi-attributes-with-structuremap

    It works really well in my project and solves all problems I had with the other approaches.

提交回复
热议问题