How to use Castle.Windsor in an assembly loaded using reflection

前端 未结 3 2040
Happy的楠姐
Happy的楠姐 2021-01-07 00:26

Let\'s say I have a library Lib.dll, which uses Castle.Windsor to initialize its services.

I have a main application App.exe, which loads Lib.dll on runtime using re

相关标签:
3条回答
  • 2021-01-07 01:09

    You probably will consider loading the plugin in a separate AppDomain, with a different private path, look at AppDomainSetup. Of course there is a drawback that you neeed a separate app domain for your plugin, but sometimes this is considered a good practice.

    0 讨论(0)
  • 2021-01-07 01:11

    You can try to load the unresolved assemblies within your code by AssemblyResolve event

    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
         string typeToLoad = args.Name;
         string myPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
         return Assembly.LoadFile(...); //or return Assembly.GetExecutingAssembly() etc.
    };
    
    0 讨论(0)
  • 2021-01-07 01:12

    You probably can't do it in easy and elegant way if App.exe doesn't provide you Castle Windsor's container instance to configure your services.

    If it is not exposed directly, maybe you can access it using Service Locator? Or find it on your own using reflection on App.exe assembly?

    The best solution will be if code in App.exe calls specific method in your library (i.e. it can look for particular interface implementation, like IModuleInitializer or something, create an instance of it and call some kind of Initialize method passing container instance to your code).

    You could also think about extensibility frameworks like MEF, but that can be a bit overkill and make big influence on App.exe.

    0 讨论(0)
提交回复
热议问题