简体   繁体   English

在没有dll的情况下在2个项目之间共享代码

[英]Sharing code between 2 projects without a dll

How can I have code-sharing between two projects without making a dll? 如何在不创建DLL的情况下在两个项目之间进行代码共享?

The issue is: I have a tool that syncs users & groups from LDAP to a database. 问题是:我有一个工具可以将用户和组从LDAP同步到数据库。

Now the tool is a windows service, but testing it as such is very difficult and time consuming. 现在该工具是一个Windows服务,但测试它是非常困难和耗时的。

Which is why I made a console application where I can test the LDAP syncing, and then just copy the respective sourcecode-files over to the service project. 这就是我制作一个控制台应用程序的原因,我可以在其中测试LDAP同步,然后将相应的源代码文件复制到服务项目中。

But... keeping the common files in sync is a bit of a problem. 但是......保持公共文件同步是一个问题。 I don't want to make a dll, because this probably creates me a problem with the 3rd project, a windows installer (for the service) where I have to use ExecutingAssembly path... 我不想制作一个DLL,因为这可能会让我遇到第3个项目的问题,一个Windows安装程序(用于服务)我必须使用ExecutingAssembly路径......

Is there a way to share the code without making a separate dll? 有没有办法分享代码而不需要单独的dll? Automagic statical linking, so to say ? Automagic静态链接,这么说呢?

How about adding a file as a link. 如何将文件添加为链接。

In Visual Studio right click on your console test app project -> select add existing file -> in the file add dialog navigate to files in your actual windows service project -> select the files you want to share -> and on add button select add as link option. 在Visual Studio中右键单击控制台测试应用程序项目 - >选择添加现有文件 - >在文件添加对话框中导航到实际Windows服务项目中的文件 - >选择要共享的文件 - >并在添加按钮上选择添加作为链接选项。

You can add a file to a project as a link. 您可以将文件作为链接添加到项目中。 On the Add Existing Item dialogue the Add button has a drop down on its right. 在“添加现有项目”对话框中,“添加”按钮右侧有一个下拉列表。 Use this to select "Add as Link": 使用此选项“添加为链接”:

替代文字

Put the file as a solution item and add as a link to each project. 将文件作为解决方案项,并添加为每个项目的链接。

How about hand-modify the project files to point to the same source file? 如何手动修改项目文件以指向相同的源文件?

Another option - put both projects in the same folder. 另一种选择 - 将两个项目放在同一个文件夹中。 Add a class to one, then in the other project add existing class and point to the class just created. 将类添加到一个,然后在另一个项目中添加现有类并指向刚创建的类。

You could: 你可以:

  • maintain the shared code in a separate project that produces a DLL and then use a tool such as ILMerge to turn the DLL & EXE into one assembly. 将共享代码保存在生成DLL的单独项目中,然后使用ILMerge等工具将DLL和EXE转换为一个程序集。
  • share the source-files between multiple projects, either by tweakiing your project files or doing something funky with your source-tree layout. 通过调整项目文件或使用源树布局做一些时髦的事情来共享多个项目之间的源文件。

All that said, the best approach would be to bite the bullet and store the shared code in a shared assembly (DLL). 总而言之,最好的方法是咬住子弹并将共享代码存储在共享程序集(DLL)中。 What happens when you decide to, for example, expose this code via a WCF service? 例如,当您决定通过WCF服务公开此代码时会发生什么? It starts getting more complicated then as you have 3 places that reference the same code files. 它开始变得更加复杂,因为你有3个位置引用相同的代码文件。 Don't just think about what makes your life easiest now, think about what'll make your life (and that of anyone else who has to maintain the code) easier in the future as well! 不要只考虑是什么让你现在的生活变得最简单,想想将来你的生活(以及其他任何需要维护代码的人)的生活也会变得更加容易! =) =)

Necromancing - As per Visual Studio 2017 : Necromancing - 根据Visual Studio 2017

You can create a shared project, and then reference the shared project in another project. 您可以创建共享项目,然后在另一个项目中引用共享项目。

It will use the framework-version and libraries from the project you reference the shared-project from. 它将使用您引用共享项目的项目中的框架版本和库。 You can also use the same shared project in multiple projects, provided you get no conflict. 如果没有冲突,您还可以在多个项目中使用相同的共享项目。

This is basically statical linking on a source-code level. 这基本上是源代码级别的静态链接。
This also works with HTML&JavaScript-files (specifically, it works with publishing), but with HTML & JS files, you will run into problems while debugging... 这也适用于HTML和JavaScript文件(特别是,它适用于发布),但对于HTML和JS文件,您将在调试时遇到问题...

It's under "Classical Windows Desktop", but you can also use it for .NET Core etc. 它位于“经典Windows桌面”下,但您也可以将它用于.NET Core等。

在此输入图像描述

I'm going to describe the setup we use to manage and test our Windows Service projects. 我将描述用于管理和测试Windows服务项目的设置。 While this doesn't answer the question of "sharing code without a DLL" (Unmesh's answer takes care of that), I think the OP probably doesn't realize how easy this is with a DLL. 虽然这没有回答“在没有DLL的情况下共享代码”的问题(Unmesh的答案会解决这个问题),但我认为OP可能没有意识到这对于 DLL 多么容易。 In any case, I'm hoping it will help someone . 无论如何,我希望它会帮助某人


Create a solution, LDAPSync. 创建一个解决方案LDAPSync。 Create three projects in this solution: 在此解决方案中创建三个项目:

  • LDAPSyncLib LDAPSyncLib
  • LDAPSyncSvc LDAPSyncSvc
  • LDAPSyncTest LDAPSyncTest

LDAPSyncLib is a DLL project that contains all of your business logic and main functionality. LDAPSyncLib是一个DLL项目,包含您的所有业务逻辑和主要功能。

LDAPSyncSvc is a Windows Service project that contains two classes, a service controller class that inherits from ServiceBase , and an Installer class for your service. LDAPSyncSvc是一个Windows服务项目,它包含两个类,一个继承自ServiceBase的服务控制器类,以及一个服务的Installer类。 This project has a "project reference" to LDAPSyncLib. 该项目具有LDAPSyncLib的“项目引用”。

LDAPSyncTest is either a GUI application (WinForms, WCF, etc.) or a console application, depending on your needs. LDAPSyncTest是GUI应用程序(WinForms,WCF等)或控制台应用程序,具体取决于您的需要。 This project also has a "project reference" to LDAPSyncLib. 该项目还具有LDAPSyncLib的“项目引用”。 Its sole purpose is to provide some interface which allows you to easily make the required calls into your business logic for testing purposes. 其唯一目的是提供一些界面,使您可以轻松地将所需的调用进入业务逻辑以进行测试。 In Visual Studio, set this as your "StartUp Project". 在Visual Studio中,将其设置为“StartUp Project”。

Now, when you run in debug via Visual Studio you will get a nice little GUI or command window that you can use to manually make test calls. 现在,当您通过Visual Studio运行调试时,您将获得一个很好的小GUI或命令窗口,您可以使用它来手动进行测试调用。 When you install it as a Windows Service, the LDAPSyncSvc project's controller class will take over and handle all of the necessary service requests (start, stop, pause, etc.) 当您将其安装为Windows服务时,LDAPSyncSvc项目的控制器类将接管并处理所有必要的服务请求(启动,停止,暂停等)

We have around 30 in-house Windows Service projects that we've been continuously managing, developing and testing for over a decade and this workflow has proved invaluable in quickly finding and fixing bugs when they arise. 我们有大约30个内部Windows服务项目,我们一直在不断管理,开发和测试十多年,这个工作流程在快速发现和修复错误时证明是非常宝贵的。 Best of luck with your project and I hope this helps some future Googlers. 祝你的项目好运,我希望这有助于未来的Google员工。

If you want to share functionality, you should use a DLL or similar. 如果要共享功能,则应使用DLL或类似功能。

Since what you want to share is the source , what you are essentially sharing is file sharing. 由于您要共享的是 ,因此您实际共享的是文件共享。 So you can do that by making your projects reference external sources or you can have your source control do this for you. 因此,您可以通过使项目引用外部源代码来实现,或者您可以让源代码管理器为您执行此操作。

If you are using Visual SourceSafe, you can make a link between two folders. 如果您使用的是Visual SourceSafe,则可以在两个文件夹之间建立链接。 VSS will make sure that they are treated as the same file. VSS将确保将它们视为同一文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM