从类库引用不会被复制到正在运行的项目bin文件夹 [英] References from class library are not copied to running project bin folder

查看:459
本文介绍了从类库引用不会被复制到正在运行的项目bin文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库,重新presents我的逻辑层。该库我已​​经添加了的NuGet包Google.Apis.Analytics.v3 - 它安装的软件包和所有它的依赖

I have a class library that represents my logic layer. To that library I've added a nuget package for Google.Apis.Analytics.v3 - it installed the package and all it's dependencies.

我有一个使用该逻辑类库(常引用)的控制台应用程序。一切都编写和编译的罚款。

I have a console application that uses that logic class library (regular reference). everything is written and compiled fine.

问题是,在运行时它抛出一个异常,表明未找到Google.Apis.dll。此DLL是已下载的的NuGet的依赖。

The problem is that during runtime it threw an exception that Google.Apis.dll wasn't found. This DLL is a dependency that was downloaded with the nuget.

检查BIN文件夹中,我发现,在类库bin文件夹这个DLL是present,但在控制台应用程序BIN文件夹它不是(而其他相关的DLL是)。因此,这意味着不是哪里编译过程中复制所有引用。

Checking the BIN folders, I've found that in the class library bin folder this DLL was present, but in the console application BIN folder it wasn't (while other related DLLs were). So this means that the not all references where copied during compilation.

我在网上搜索,发现所有类型的解决方法并没有真正的工作(如手动编辑项目文件和拆卸该DLL定义一个真正的XML线)。

I've searched online, and found all kind of workarounds that didn't really work (like manually editing the project file and removing a true xml line on that dll definition).

我最终做的是添加相同的NuGet库到我的控制台应用程序 - 它的工作原理,但感觉有点脏,不应该是这样。我认为,在控制台应用程序是谁的应该从逻辑类库应该知道它的东西,而客户担心它得到它的服务客户。

What I ended up doing is adding the same nuget library to my console application - it works but feels a little dirty and not the way it should be. I think the console app is the client who's supposed to get it's services from that logic class library which should know it's stuff without the "client" worrying about it.

此外,该控制台应用程序是不是谁的会使用该服务的只有一个,我还计划在一个Web应用程序将使用该功能 - 所以我需要将相同的NuGet添加到Web应用程序,以及 - 再次,感觉有点乱......

Also, that console app is not the only one who's gonna use that service, I'm also planning on a web app that will use that functionality - so I will need to add the same nuget to that web app as well - again, feels a little messy...

难道仅仅是我吗?这是正确的方式去做?我在想编写一个WCF项目来处理这些功能 - 但似乎稍微开销只是在功能上,而且可能减缓我的工作流下来只是为了保持干净在我看来

Is it just me? is that the right way to go about it? I was thinking about writing a WCF project to handle that functionality - but that seems a little of a overhead for just on functionality, and probably slow my workflow down just to keep things "cleaner" in my opinion.

我只是过思考吗?

感谢

推荐答案

对于示例场景,让我们说我们有项目X,组件A和组件B.大会A引用程序集B,所以项目X具备参考A和B同时,项目X包括code的引用组件A(例如A.SomeFunction())。现在,您可以创建一个新的项目Y,它引用X项目。

Explanation

For a sample scenario let's say we have project X, assembly A, and assembly B. Assembly A references assembly B, so project X includes a reference to both A and B. Also, project X includes code that references assembly A (e.g. A.SomeFunction()). Now, you create a new project Y which references project X.

因此​​依赖链是这样的: Y => X => A => B

So the dependency chain looks like this: Y => X => A => B

的Visual Studio / MSBuild的尝试是聪明的,只有把引用过到项目Ÿ它检测到的被要求按项目X;它这样做是为了避免在项目Y.的问题是参考的污染,因为X项目实际上并不包含明确使用程序集B的任何code(例如B.SomeFunction()),VS / MSBuild的不检测B被要求用X,从而在不将其复制到项目Ÿ的bin目录下;它仅复制了X和A组件。

Visual Studio / MSBuild tries to be smart and only bring references over into project Y that it detects as being required by project X; it does this to avoid reference pollution in project Y. The problem is, since project X doesn't actually contain any code that explicitly uses assembly B (e.g. B.SomeFunction()), VS/MSBuild doesn't detect that B is required by X, and thus doesn't copy it over into project Y's bin directory; it only copies the X and A assemblies.

您有两个选项来解决这个问题,这两者将导致程序集B被复制到项目Ÿ的bin目录:

You have two options to solve this problem, both of which will result in assembly B being copied to project Y's bin directory:

  1. 添加引用程序集B项目年。
  2. 添加虚拟code到项目X中使用程序集B的文件。

我个人的一对夫妇的原因preFER选项2。

Personally I prefer option 2 for a couple reasons.

  1. 如果您在将来添加另一个项目,项目引用X,你不会记得要还包括引用程序集B(像你将不得不做的选择1)。
  2. 您可以有明确的意见说,为什么假code需要存在,而不是将其删除。所以,如果有人不意外删除code(说与重构工具,看起来未使用code),你可以轻松地从源头控制看出,code是必要的,以恢复它。如果您使用选项1,有人使用重构工具来清理未使用的引用,你没有任何意见;你只会看到一个参考从.csproj的文件中删除。

下面是哑code当我遇到这种情况,我通常会添加一个样本。

Here is a sample of the "dummy code" that I typically add when I encounter this situation.

    // DO NOT DELETE THIS CODE UNLESS WE NO LONGER REQUIRE ASSEMBLY A!!!
    private void DummyFunctionToMakeSureReferencesGetCopiedProperly_DO_NOT_DELETE_THIS_CODE()
    {
        // Assembly A is used by this file, and that assembly depends on assembly B,
        // but this project does not have any code that explicitly references assembly B. Therefore, when another project references
        // this project, this project's assembly and the assembly A get copied to the project's bin directory, but not
        // assembly B. So in order to get the required assembly B copied over, we add some dummy code here (that never
        // gets called) that references assembly B; this will flag VS/MSBuild to copy the required assembly B over as well.
        var dummyType = typeof(B.SomeClass);
    }

这篇关于从类库引用不会被复制到正在运行的项目bin文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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