Created
April 16, 2014 15:29
-
-
Save aikmeng/10894201 to your computer and use it in GitHub Desktop.
Ninject and ASP.NET Filter Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExperimentWebApiNinject.Utilities.Interface | |
{ | |
public interface ILog | |
{ | |
void Write(string message); | |
} | |
public class Log : ILog | |
{ | |
public void Write(string message) | |
{ | |
Debug.WriteLine(message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExperimentWebApiNinject.ActionFilters | |
{ | |
public class MyActionAttribute : Attribute { } | |
public class MyActionFilter : AbstractActionFilter | |
{ | |
private readonly ILog log; | |
public MyActionFilter(ILog log) | |
{ | |
this.log = log; | |
} | |
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) | |
{ | |
log.Write("MyActionFilter: OnActionExecuting"); | |
base.OnActionExecuting(actionContext); | |
} | |
public override bool AllowMultiple | |
{ | |
get { return true; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExperimentWebApiNinject.ActionFilters | |
{ | |
public class MyControllerActionAttribute : Attribute { } | |
public class MyControllerActionFilter : AbstractActionFilter | |
{ | |
private readonly ILog log; | |
public MyControllerActionFilter(ILog log) | |
{ | |
this.log = log; | |
} | |
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) | |
{ | |
log.Write("MyControllerActionFilter: OnActionExecuting"); | |
base.OnActionExecuting(actionContext); | |
} | |
public override bool AllowMultiple | |
{ | |
get { return true; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExperimentWebApiNinject.ActionFilters | |
{ | |
public class MyGlobalActionFilter : AbstractActionFilter | |
{ | |
private readonly ILog log; | |
public MyGlobalActionFilter(ILog log) | |
{ | |
this.log = log; | |
} | |
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) | |
{ | |
log.Write("MyGlobalActionFilter: OnActionExecuting"); | |
base.OnActionExecuting(actionContext); | |
} | |
public override bool AllowMultiple | |
{ | |
get { return true; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Load your modules or register your services here! | |
/// </summary> | |
/// <param name="kernel">The kernel.</param> | |
private static void RegisterServices(IKernel kernel) | |
{ | |
kernel.Bind<ILog>().To<Log>(); | |
kernel.BindHttpFilter<MyGlobalActionFilter>(FilterScope.Global); | |
kernel.BindHttpFilter(x => new MyControllerActionFilter(x.Inject<ILog>()), FilterScope.Controller).WhenControllerHas<MyControllerActionAttribute>(); | |
kernel.BindHttpFilter(x => new MyActionFilter(x.Inject<ILog>()), FilterScope.Action).WhenActionMethodHas<MyActionAttribute>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExperimentWebApiNinject.Controllers | |
{ | |
[MyControllerAction] | |
public class ValuesController : ApiController | |
{ | |
private readonly ILog log; | |
public ValuesController(ILog log) | |
{ | |
this.log = log; | |
} | |
// GET api/values | |
[MyAction] | |
public IEnumerable<string> Get() | |
{ | |
log.Write("ValuesController: Get()"); | |
return new string[] { "value1", "value2" }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment