【问题标题】:Is It Possible to Implement Onion Architecture and DI with WebAPI on OWIN?是否可以在 OWIN 上使用 WebAPI 实现洋葱架构和 DI?
【发布时间】:2014-05-20 15:17:58
【问题描述】:

我正在尝试为 OWIN/Katana 上托管的 WebAPI 服务遵循洋葱架构。

我有一个这样的解决方案结构:

  • DependencyResolution:包含 OWIN 启动类和 IoC 设置
  • WebApi:Web API 控制器
  • 基础架构:接口实现
  • 核心:接口

我希望 DependencyResolution 项目为 WebApi 项目注入依赖项。 DependencyResolution 确实有一个构建任务要输出到 WebApi 项目的输出文件夹。

我遵循此处概述的方法,使用 Autofac 和 DotNetDoodle.Owin.Dependencies NuGet 包:

http://www.tugberkugurlu.com/archive/owin-dependencies--an-ioc-container-adapter-into-owin-pipeline

但是,在我的 Startup 类中注册服务时,对 RegisterApiControllers() 的调用将失败。 DependedencyResolution 程序集将首先执行,因此它无法获取包含 ApiContollers 的程序集(WebAPI 程序集):

public IContainer RegisterServices()
{
    ContainerBuilder builder = new ContainerBuilder();

    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    builder.RegisterType<MyRepo>()
           .As<IRepo>()
           .InstancePerLifetimeScope();

    return builder.Build();
}

求助于 Assembly.Load() 是这里唯一可行的选择,还是我应该放弃保持 DependencyResolution 项目隔离的想法,而只是从 WebApi 项目中引用它(似乎比 onony 少一点)?

【问题讨论】:

    标签: c# asp.net-web-api inversion-of-control owin onion-architecture


    【解决方案1】:

    您可以使用 Web API 程序集的名称来获取其实例:

    builder.RegisterApiControllers(Assembly.Load("WebApiAssembly"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 2012-05-13
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 2014-10-15
      相关资源
      最近更新 更多