asp.net-mvc


WebApi Action filter called twice


My WebApi filter method OnActionExecuted is being called twice.
My filter (I make it as simple as possible):
public class NHibernateActionFilter : ActionFilterAttribute
{
// [Inject]
// public ISessionFactoryProvider sessionFactoryProvider { get; set; }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var a = 5;
var b = a;
//new BaseSessionProvider(sessionFactoryProvider).EndContextSession();
}
}
My setup:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
//http://stackoverflow.com/questions/9521040/how-to-add-global-asp-net-web-api-filters
FilterConfig.RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
}
public class FilterConfig
{
public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
filters.Add(new NHibernateActionFilter());
}
}
In debugger I catch OnActionExecuted twice with the same actionExecutedContext. Why?
UPD
Controller
public class BankSmsController : ApiController
{
[AcceptVerbs(HttpVerbs.Get)]
public int GetTest()
{
return 1;
}
}
I have a suspicion, that this strange behavior can be fixed by either overriding AllowMultiple property of filter and returning false, or applying AttributeUsage attribute with AllowMultiple set to false too (this influences on default implementation of AllowMultiple property of filter.
At least in our project this helped (we have filters injected via Autofac).
This might be caused due to a registration of a custom filter provider. When you do this, you need to unregister the default one. Otherwise, if you are getting the usual filters in your custom one, they will be registered twice and consequently, executed twice.
Code should be something like this:
// remove default action filter provider
var defaultFilterProvider = config.Services.GetFilterProviders().Single(provider => provider is ActionDescriptorFilterProvider);
config.Services.Remove(typeof(IFilterProvider), defaultFilterProvider);
// add custom filter provider
config.Services.Add(typeof(IFilterProvider), new CustomFilterProvider(container));
As have been said, AllowMultiple to false is a hack since .net is clever enough to only execute once a filter even if it has been registered several times. Also, there are scenarios where you do need this to be true.
For me, I had specified the filter twice. In my IOC config file I had
builder.Register(c => new SelectListFilter(c.Resolve<ClientManager>()))
.AsActionFilterFor<Controller>()
.InstancePerRequest();
.RegisterFilterProvider();
And then in filterConfig I had
filters.Add(DependencyResolver.Current.GetService<IActionFilter>());
I removed the line from filterConfig and everything was better.

Related Links

Error in Updating dbModel of entity framework
Database First with Multiple Tables with Foreign Keys in a Single View
How we can show “No Data Available” message in RDLC Reports via Resources files
ASP.NET_SessionId Request Cookie Not Secure?
how to use “Refresh token” in OAuth2
Render a partial view in another partial view in node js with express framework and jade template
Authorize(Roles = “Admins”) causes infinite loop in MVC5 Identity 2
Unable to add conditional first entry to selectlist drop-down menu
To extract the id of the field using the name
Remove the controller name in MVC
Multiple pages, single controller, how to set it up in mvc?
access [NotMapped] property with a custom getter/setter
Umbraco MVC-like routing
How to format action link pass parameter in mvc?
MVC drop down list is not picking up selected item?
MappingExpection NHibernate 4 .net 4.5 SPA

Categories

HOME
amazon-s3
opencv
mongodb
printing
openshift-origin
android-sqlite
load
compiler-errors
bolt-cms
sql-server-2012
google-cloud-dataproc
box
openvpn
wav
librsvg
sonata-admin
zend-expressive
nservicebus
googletest
rebus
afnetworking-2
prerender
w2ui
flow
formio
gyroscope
jmeter-plugins
rtp
centroid
desktop-app-converter
android-security
json-c
dokuwiki
ose
pdfstamper
texture-mapping
amazon-vpc
es6-promise
using
wp-api
powerpc
kendo-chart
oracle-aq
sql-insert
opal
clientscript
scoring
sql-mode
derbyjs
ammonite
typesetting
jcuda
android-touch-event
db2400
turbo-c
itamae
logstash-jdbc
amazon-dynamodb-streams
xnamespace
insert-update
nuitka
groups
facebook-comments
domo
ane
coldfusion-10
sejda
openocd
cng
symbian
session-replication
monkeyrunner
double-quotes
axlsx
wintersmith
theorem-proving
declaration
centering
mks
slick2d
gnat
bitrock
mpj-express
razor-2
dataformat
dotliquid
android-lru-cache
ducttape
couchrest
sugar.js
chromeless
google-goggles
memento
shebang
working-directory
noindex
openid4java
sdl.net
facebook-authentication
nstreecontroller
seam-conversation
program-transformation
posterous
geneva-framework
fogbugz-on-demand
variable-names
user-testing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App