Why am I getting this ActivationException when using Simple Injector with WebApi Self Hosted in OWIN?

I have a very simple web Api v2.2 hosted in OWIN

public class StartUp { public void Configuration(IAppBuilder appBuilder, IConfigReader configReader) { var container = new Container(); container.Register<IConfigReader, ConfigReader>(); var config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); appBuilder.UseWebApi(config); } } 

When I then use on my Main() as:

 WebApp.Start(baseAddress, appBuilder => new StartUp().Configuration(appBuilder, new ConfigReader())); 

However, when I try to execute the last line of appBuilder.UseWebApi(config); I get the following exception:

The first random exception of type "SimpleInjector.ActivationException" occurred in SimpleInjector.dll

Additional Information: This type of IHostBufferPolicySelector is not a specific type. Please use one of the other overloads to register this type.

Full stack:

SimpleInjector.ActivationException occurred _HResult = -2146233088
_message = This type of IHostBufferPolicySelector is not a specific type. Please use one of the other overloads to register this type.
HResult = -2146233088 IsTransient = false Message = This type of IHostBufferPolicySelector is not a specific type. Please use one of the other overloads to register this type. Source = SimpleInjector
Stack Traces: in SimpleInjector.Advanced.DefaultConstructorResolutionBehavior.VerifyTypeIsConcrete (implementationType Type) InnerException:

The problem is not that one interface looks like this: SimpleInjector tries to find a binding for each individual interface; If I provided a dummy implementation for IHostBufferPolicySelector , it throws out some other interface, for example. IExceptionHandler etc.

There is a related thread HERE , but I'm not sure how it relates to SimpleInjector ? Self host is a console application with the following packages installed:

  • Simple Injector ASP.NET Web API Integration v2.61
  • Simple Injector Execution Context Coverage v2.61
  • Simple Injector v2.61
  • OWIN v1.0
  • Microsoft.Owin v2.0.2
  • Microsoft.Owin.Hosting v2.0.2
  • Microsoft ASP.NET Web API 2.2 OWIN v5.2.2
  • Microsoft ASP.NET Web API 2.2 OWIN Self Host v5.2.2
+5
source share
2 answers

It turned out that the exception only bubbles up when I have Common Language Runtime Exceptions set to thrown

When I uncheck the box, everything behaves as usual! which is strange! also wrapping it in Try-Catch (Exception) doesn't even catch it, which makes it even more interesting / werid!

+1
source

According to Simple Injector :

The exceptions you show are the "first random exceptions." These exceptions are thrown by a simple injector, but they are caught and handled by Simple Injector and they will not bubble in the call stack. You may see them in the debugger output window, but they are completely normal and nothing to worry about.

+3
source

Source: https://habr.com/ru/post/1205863/


All Articles