I'm using ASP.NET Web API v2.0 to build an web api.
I need to make some of the controllers/actions available in CORS/JSONP, so I chose to use WebApiContrib.Formatting.Jsonp.
Because I'm not use Web API v2.1 yet, I can only use WebApiContrib v0.9.7.0.
If I add the JSONP formatter in Global.ascx.cs, it'll open all my controllers and actions for CORS/JSONP, so I wrote the Action Filter below to add and remove the formatter at specific times.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class EnableCorsAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var config = System.Web.Http.GlobalConfiguration.Configuration;
config.Formatters.Insert(0, new WebApiContrib.Formatting.Jsonp.JsonpMediaTypeFormatter(config.Formatters.JsonFormatter));
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
var config = System.Web.Http.GlobalConfiguration.Configuration;
config.Formatters.RemoveAt(0);
}
}
Now, my question is that will that code be thread safe if multiple requests are coming in?