[英]How to use DI with Unity after registering DLLs dynamically
I register my Dlls dynamically with Unity我使用 Unity 动态注册我的 Dll
public static void RegisterTypes(IUnityContainer container)
{
string dependenciesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SpecificDll");
string[] dependencies = Directory.GetFiles(dependenciesPath);
Dictionary<Type, Type> pluginMappings = new Dictionary<Type, Type>();
//Load Dependency Assemblies
foreach (string fileName in dependencies)
{
//Get type interface and plugin type
....
}
foreach (var mapping in pluginMappings)
{
container.RegisterType(mapping.Key, mapping.Value);
}
}
I used to register my class like this way我曾经这样注册我的班级
container.RegisterType<IService, Service>(new HierarchicalLifetimeManager());
And use it like that并像那样使用它
public class MyController : ApiController
{
private Service _service;
public MyController(Service service)
{
_service = service;
}
}
But I don't know how to do when I load the assemblies dynamically.但是当我动态加载程序集时我不知道该怎么做。
Can you help me ?你能帮助我吗 ?
Thx谢谢
Finally, I created a third project with common interfaces.最后,我创建了具有通用接口的第三个项目。
In my Dll :在我的 Dll 中:
public class ServiceSpe : IServiceSpe
{
public string GetLabel()
{
return "hello world";
}
}
In my common project :在我的共同项目中:
public interface IServiceSpe
{
string GetLabel();
}
In my controller :在我的控制器中:
public MyController()
{
}
public MyController(IServiceSpe serviceSpe)
{
if (serviceSpe != null)
{
string result = serviceSpe.GetLabel();
}
}
If dependency contains class with interface "IServiceSpe" :如果依赖项包含具有接口“IServiceSpe”的类:
MyController(IServiceSpe serviceSpe) is called MyController(IServiceSpe serviceSpe) 被调用
else别的
MyController() is called MyController() 被调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.