我正在尝试连接我的Web Api项目,以便将Castle Windsor用于IoC
我现在正试图将依赖注入到DelegatingHandler和ActionFilterAttribute中
我试图在常规ASP.Net MVC中复制用于过滤器的技术,但它们似乎不适用于Web Api
谁有人设法让这个工作?
我不确定Web Api中的相关扩展点是什么
我已经看到了这个建议
config.MessageHandlers.Add(_myContainer.Resolve<IApiUsageLogger>());
Run Code Online (Sandbox Code Playgroud)
但不确定是否有更好的方法.我更愿意利用创建这些处理程序/过滤器的机制
因为这有很多处理程序的服务位置的味道.是否有一个点可以创建所有处理程序?
有任何想法吗?
c# dependency-injection castle-windsor inversion-of-control asp.net-web-api
在ASP.NET MVC 2上,我有一个ActionFilterAttribute调用[Transaction],在执行操作之前启动一个NHibernate事务,然后提交或回滚它,具体取决于是否抛出异常.该ISession实例HttpRequestScoped()由Autofac注入.它看起来像这样,效果很好:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class TransactionAttribute : ActionFilterAttribute
{
private ITransaction transaction;
public TransactionAttribute()
{
this.Order = 0;
}
public ISession Session
{
get;
set;
}
public override void OnActionExecuted(
ActionExecutedContext filterContext)
{
if (this.Session != null && this.transaction != null)
{
try
{
if (this.transaction.IsActive)
{
if (filterContext.Exception == null)
{
this.transaction.Commit();
}
else
{
this.transaction.Rollback();
}
}
}
finally
{
this.transaction.Dispose();
this.transaction …Run Code Online (Sandbox Code Playgroud)