c# - Using Custom IHttpActionInvoker in WebAPI for Exception Handling -


i'm trying add custom ihttpactioninvoker webapi application in order prevent need lots of repeated exception handling code in action methods.

there doesn't seem out there how other this article. after writing ihttpactioninvoker per article added code:

globalconfiguration.configuration.services.remove(typeof(ihttpactioninvoker), globalconfiguration.configuration.services.getactioninvoker());  globalconfiguration.configuration.services.add(typeof(ihttpactioninvoker), new myapicontrolleractioninvoker()); 

into method within global.asax file. when executing call api following exception raised @ remove() method:

the service type ihttpactioninvoker not supported 

i guess have 2 questions.

  1. considering there doesn't seen awful lot out there writing custom ihttpactioninvoker classes considered approach solve exception handling in webapi applications?

  2. does know why such exception when executing remove() method , how best fix particular issue?

i suffered same error describe when attempting remove service.

i discovered didn't need remove global config, appears if you've registered interface in container resolve first.

for example, i'm using simpleinjector , in global.asax have this:

container.register<ihttpactioninvoker , myapicontrolleractioninvoker >(); // register dependency resolver. globalconfiguration.configuration.dependencyresolver =    new simpleinjectorwebapidependencyresolver(container); 

at runtime, resolving myapicontrolleractioninvoker dependency when required.

you can perform exception handling in customer actioninvoker , dependencies set in constructor wired correctly. reason looking @ actioninvoker constructor injection, since injecting attributes appears require property injection.

also rather remove/insert, replace seems work. (in global.asax)

globalconfiguration.configuration.services.replace(typeof(ihttpactioninvoker), new myapicontrolleractioninvoker(fooservice)); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -