解决.net core中项目的所有依赖 [英] Resolve all dependencies of the project in .net core

查看:56
本文介绍了解决.net core中项目的所有依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解决我项目中的所有依赖项.我的项目由 Web、Service 和 DataAccess 3 层组成.

I want to resolve all the dependencies in my project. My project consists of 3 layers which are Web, Service, and DataAccess.

我以前在使用 AutoFac 时这样做过:

I have done this before while using AutoFac:

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("OrderForm")).ToArray();
            builder.RegisterAssemblyTypes(assemblies)
                .AsImplementedInterfaces()
                .InstancePerRequest();

现在我想在 .net core 中做同样的事情.我试过这个:

Now I want to do the same thing in .net core. I have tried this:

var allProviderTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace != null && t.Namespace.Contains("Providers"));

问题是这仅返回 web-assembly 而不是其他的.

The problem is that this only returns the web-assembly and not the other ones.

推荐答案

在你的 Startup.cs 中的 ConfigureServices 方法

In your Startup.cs in ConfigureServices method

  var container = new ContainerBuilder();
        container.Populate(services);

        return new AutofacServiceProvider(container.Build());

这篇关于解决.net core中项目的所有依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆