简体   繁体   中英

Sharing code between 2 projects without a dll

How can I have code-sharing between two projects without making a dll?

The issue is: I have a tool that syncs users & groups from LDAP to a database.

Now the tool is a windows service, but testing it as such is very difficult and time consuming.

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.

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...

Is there a way to share the code without making a separate dll? Automagic statical linking, so to say ?

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.

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.
  • 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). What happens when you decide to, for example, expose this code via a WCF service? It starts getting more complicated then as you have 3 places that reference the same code files. 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 :

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...

It's under "Classical Windows Desktop", but you can also use it for .NET Core etc.

在此输入图像描述

I'm going to describe the setup we use to manage and test our Windows Service projects. 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. In any case, I'm hoping it will help someone .


Create a solution, LDAPSync. Create three projects in this solution:

  • LDAPSyncLib
  • LDAPSyncSvc
  • LDAPSyncTest

LDAPSyncLib is a DLL project that contains all of your business logic and main functionality.

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. This project has a "project reference" to LDAPSyncLib.

LDAPSyncTest is either a GUI application (WinForms, WCF, etc.) or a console application, depending on your needs. This project also has a "project reference" to 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".

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. 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.)

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. Best of luck with your project and I hope this helps some future Googlers.

If you want to share functionality, you should use a DLL or similar.

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. VSS will make sure that they are treated as the same file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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