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

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

问题描述

我想解决项目中的所有依赖关系。我的项目包含3个层,分别是Web,Service和DataAccess。

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程序集,而不返回其他程序集。

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核心中项目的所有依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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