C# and IoC transitive dependencies removed

人盡茶涼 提交于 2020-01-04 04:34:19

问题


I have a solution in which I use IoC (windsor). The projects in the solution are as follows:

  1. Interfaces - Holds all the interface contracts I'll use.
  2. IoC.Installers - Holds all the installers for my dependencies (has reference to impl. and interfaces)
  3. IoC - Holds a singleton class that holds the IoC container. The class performs the initialization process of the container.
  4. Console - The project that uses the IoC to resolve dependencies (has reference to interfaces an IoC)

The problem: Because the IoC project doesn't directly use the IoC.Installers project, it is omitted from the build process on the Console project, hence no installers are found during the initialization process.

The workaround: In the IoC project I added a static constructor that directly initiates an installer from the IoC.Installers project and uses it (I do a GetType() on the instance I make)

Problem with the workaround: I wanted to create some generic container holder that I could move from solution to solution without the need to fix my hack.

Is there a better way to force the IoC.Installers dll to be copied to the bin folder without the hack? The final aim is to create a nuget the wraps castlewindsor and tries to find all the installers in the solution and install them

I'm adding a link to a git repo in which I made a project that reproduces the problem (it contains the work around as well)

Thanks!


回答1:


As discussed in Is "Copy Local" transitive for project references?, your only options are

  1. Add a fake usage of the libraries in your IoC container library;
  2. Add a post-build XCOPY step to copy the libraries to the application output folder;

If you choose #2 and don't know how to do it, have a look at the MSBuild guide to understand how to edit MSBuild scripts (you'll have to edit the <Target Name="AfterBuild" /> element of your application's .csproj).




回答2:


Copy the IOC.Installers assembly manually in the console directory when you are building (eg post-build on success), and instead of installing from assemblies in the application, install from the assemblies in a directory:

container.Install(FromAssembly.InDirectory(new AssemblyFilter("dir/of/console")));

There is also a file mask parameter which can speed up the filtering:

container.Install(FromAssembly.InDirectory(new AssemblyFilter("dir/of/console", "*.installers.dll")));


来源:https://stackoverflow.com/questions/26827553/c-sharp-and-ioc-transitive-dependencies-removed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!