I am using MS WebAPI with Spring.NET for my DI, and making use of Sprint.NET AOP to mark methods as transactional.
When I mark a "Controller" action as transactional, I get the following error:
Unable to cast object of type 'CompositionAopProxy_13695853c76b40f8b9436e27afa947f0' to type 'TPMarketing.PayrollConsole.Web.Rest.Controllers.OrganisationsController'.","exceptionType":"System.InvalidCastException","stackTrace":" at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c_DisplayClass13.b_c(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c_DisplayClass5.b_4()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
Does this mean you can't use proxy based AOP with Web API Controllers?
(I have got a workaround, I have made my own "Transaction" attribute for that inherits from ActionFilterAttribute to just use in the web tier)
thanks, Jordan.
EDIT... I haven't had time to check out Marijns suggestion below yet, so here is my workaround for anyone interested. This is the body of the code from my Action Filter: (TransactionManager is the Spring.NET IPlatformTransactionManager). I am still using the normal Spring.NET Transaction attribute in my service layer, and this should play nicely with that.
public override void OnActionExecuting(HttpActionContext actionContext)
{
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.ReadOnly = ReadOnly;
actionContext.Request.Properties[TRANSACTION_KEY] = TransactionManager.GetTransaction(def);
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
ITransactionStatus ts = actionExecutedContext.Request.Properties[TRANSACTION_KEY] as ITransactionStatus;
if (ts.RollbackOnly || actionExecutedContext.Exception != null)
{
TransactionManager.Rollback(ts);
}
else
{
TransactionManager.Commit(ts);
}
}
public bool ReadOnly { get; set; }