使用autofac解决在其他项目中注册的组件的依赖性

问题描述

所以我有一个基础结构层(Project1),我在其中注册了所有模块,所以我使用的是Autofac,它像这样:

builder.RegisterType<MyConcreteClass>().As<IMyInterface>()
                .WithParameter(new TypedParameter(typeof(string),"myParameter1"))
                .WithParameter(new TypedParameter(typeof(string),"myParameter2"));

在另一个项目(Project2)中,我将调用一个服务MyService,该服务将调用MyConcreteClass,并且打算解决该问题,但不确定如何执行。首先,在第二个项目中找到了appsettings,它是一个dotnet核心项目,我知道我可以通过在此MyService中注入IConfiguration并随后添加类似以下内容来轻松获得appsettings:var myParam1 = Configuration["UsefulSettings:MyParameter1"]

两个问题:

  1. 我应该为Project1中的MyConcreteClass的c-tor解析参数吗,我在其中将MyConcreteClass注册为IMyInterface的实现(因此,基本上,我问是否应该同时注册并解析)?如果是这样,考虑到在Project2应用程序设置中找到了参数,我该怎么做?

  2. 如果我可以在Project2中解析MyConcreteClass(在Project1中注册后),该怎么做? 在Autofac文档中,我注意到他们使用了类似{https://autofaccn.readthedocs.io/en/latest/resolve/parameters.html)的内容

var service = scope.Resolve<IMyInterface>()

.WithParameter(new TypedParameter(typeof(string),Configuration["UsefulSettings:MyParameter1"]))

.WithParameter(new TypedParameter(typeof(string),Configuration["UsefulSettings:MyParameter2"])));

...但是谁是范围

那么,哪种方法更好,是1还是2,您能帮忙填补空白吗?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)