asp.net-web-api - 为什么在 OWIN 中使用带有 WebApi 自托管的简单注入(inject)器时会出现此 ActivationException?

标签 asp.net-web-api owin asp.net-web-api2 self-hosting simple-injector

我有一个非常简单的 Web Api v2.2 自托管在 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);
    }
}

当我在我的 Main() 上使用时:

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

但是,当我尝试执行最后一行 appBuilder.UseWebApi(config); 时,出现以下异常:

A first chance exception of type 'SimpleInjector.ActivationException' occurred in SimpleInjector.dll

Additional information: The given type IHostBufferPolicySelector is not a concrete type. Please use one of the other overloads to register this type.

完整堆栈:

SimpleInjector.ActivationException occurred _HResult=-2146233088
_message=The given type IHostBufferPolicySelector is not a concrete type. Please use one of the other overloads to register this type.
HResult=-2146233088 IsTransient=false Message=The given type IHostBufferPolicySelector is not a concrete type. Please use one of the other overloads to register this type. Source=SimpleInjector
StackTrace: at SimpleInjector.Advanced.DefaultConstructorResolutionBehavior.VerifyTypeIsConcrete(Type implementationType) InnerException:

问题不在于单个接口(interface),它看起来像 SimpleInjector 试图为每个单个接口(interface)找到一个绑定(bind);如果我为 IHostBufferPolicySelector 提供一个虚拟实现,它会抛出一些其他接口(interface),例如IExceptionHandler

有一个相关线程 HERE但我不确定它与 SimpleInjector 有何关系? 自主机是一个控制台应用程序,安装了以下软件包:

  • 简单注入(inject)器 ASP.NET Web API 集成 v2.61
  • 简单注入(inject)器执行上下文范围 v2.61
  • 简单注入(inject)器 v2.61
  • OWIN v1.0
  • Microsoft.Owin v2.0.2
  • Microsoft.Owin.Hosting v2.0.2
  • 微软 ASP.NET Web API 2.2 OWIN v5.2.2
  • Microsoft ASP.NET Web API 2.2 OWIN 自托管 v5.2.2

最佳答案

根据简单注入(inject)器:

The exceptions you are showing are 'first chance exceptions'. These exceptions are thrown by Simple Injector but they are caught and processed by Simple Injector and they won't bubble up the call stack. It is possible that you see them in some debugger output window, but they are quite normal and nothing to worry about.

关于asp.net-web-api - 为什么在 OWIN 中使用带有 WebApi 自托管的简单注入(inject)器时会出现此 ActivationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652851/

相关文章:

c# - 网络 API : Configure JSON serializer settings on action or controller level

azure - Swagger 无法在以 OWIN 身份运行的 Azure Web 应用程序上运行

c# - 在 Owin 自托管应用程序中使用授权服务文件

iis - SystemWeb Host : IAppBuilder. UseWebApi 忽略带有扩展名的路径

macos - Visual Studio for Mac 2017 RC 上的 ASP.NET Web API 2

c# - Web Api (MVC 6) Chunked body 没有正确终止 0-sized chunk

c# - Web API 中的内存缓存

c# - 将自定义 header 添加到 .Net 中的所有 Swagger 响应

c# - 如何通过现有的 WebAPI 验证我的 MVC5 应用程序?

c# - WebAPI 多个冲突路由 - 基于身份验证