several stories of one problem
I implemented a class (MyClass
) which has a timeout callback thing, that should be start right after response dispatched to the client. I can use action filter
to accomplish this, but i prefer not to use action filter
explicitly (because of user convenience). instead attach the Action Filter
when MyClass
instantiated in the Action Method body.
I have a situation which needs to fire a function delegate (used to track timeout thing) after action executed.
Consider a class, designed to run some code (after action method
executed) when an instance initiate on that action method
.
I need to execute some code after one specific action executed it's result (which i have it's action's name).
I know that using custom Action Filter
and implement it's OnActionExecuted
Method is the way to go, But what i'm trying to know is, how to attach that action filter
dynamically while action is executing.
I mean, how to attach an action filter
without subscribe it in Global.asax
nor nominate it above the Action Method
using [MyCustomActionFilter]
.
Edited:
Consider i have following class:
public class MyClass
{
public List<SomeDelegate> TimeoutCallbackFunction{get; set;}
public MyClass()
{
// I want to attach action filter here.
// user may forget to attach MyFilter filter to Action Method
// So that it would be easier for users if this handled by MyClass Constructor.
HttpContext.Current....... or something
}
}
Consider my action filter is:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
// Fetch newly added callback delegates from session.
// Run them all.
}
so in the controller i have following code:
//I want following filter attach to be removed.
//[MyFilter]
public ActionResult MyAction()
{
// This piece of code, should attach action method with 'MyFilter' filter
// And dismiss user from nominate it above of 'MyAction' action method
MyClass myObject=new MyClass();
}
by the way, by "User", i mean the whom uses my class and develop an mvc.asp base on my dlls.
my question is:
1- Is it even possible to attach an action filter
to the action that is currently executing? And if it is, how?
2- If it's not possible, then what other choice do i have to run some code right after Result Executed
?
3- If there is no any alternative, so what people do in situation like this? is there any standardize way to fire a timer callback?
Maybe action filters is not a good solution for your problem. However did you think about creating a simple flag somewhere and set it to true when you want to execute action filter functionality? For instance:
In your code you set the flag:
HttpContext.Current.Items["ExecuteMyActionFilter"] = true
And in your action filter:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if ((bool?) HttpContext.Current.Items["ExecuteMyActionFilter"] == true)
{
// Execute your code
}
}
You can use static variables, database or any state conditions to check if your action needs to be executed. Hope you got the idea!