首页 文章

是否可以在OWIN上使用WebAPI实现洋葱架构和DI?

提问于
浏览
5

我正在尝试遵循洋葱架构来获取在OWIN / Katana上托管的WebAPI服务 .

Onion Architecture

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

  • 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()的调用将失败 . DepenedencyResolution程序集将是第一个执行,因此它将无法获取包含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项目中引用它(看起来有点不那么洋葱)?

1 回答

  • 1

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

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

相关问题