Sharing code between 2 projects without a dll

13,725

Solution 1

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.

Solution 2

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":

alt text

Put the file as a solution item and add as a link to each project.

Solution 3

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! =)

Solution 4

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.

Solution 5

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.

enter image description here

Share:
13,725

Related videos on Youtube

Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence & Web Software Developer Technologies I work with: Microsoft Reporting- & Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 & 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML & JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web & DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on April 10, 2020

Comments

  • Stefan Steiger
    Stefan Steiger about 4 years

    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 ?

    • CodesInChaos
      CodesInChaos over 13 years
      On vista and newer symlinks might be useful.
    • Rob
      Rob over 13 years
      @CodeInChaos, there's no way to store symlinks in sourcecontrol/backups easily - you are doing both of those, right? =)
    • CodesInChaos
      CodesInChaos over 13 years
      stackoverflow.com/questions/954560/… sounds like git stores sym-links as sym-links. No idea if it works on windows though since I don't use vista yet. And depending on the use-case it might be enough to add the sym-link to the ignore-file and manually create it for each coder. A bit hackish, but should work.
    • CodesInChaos
      CodesInChaos over 13 years
      But the VS "Add as Link" feature looks cleaner to me. Didn't know about that one.
  • Stefan Steiger
    Stefan Steiger over 13 years
    ILMerge is too unautomatic (unless there is an easy way to automatize it). I decided for 'something funky' (=add the file as a link). As to the last paragraph - all valid and good arguments, but I don't want a dll for the mentioned reasons. Plus I would have to compile the dll project each time I make a change, while this way, I can just press the run button. Don't know about sourcesafe, but in the worst case, the links are re-added fast.
  • Pauli Østerø
    Pauli Østerø over 13 years
    of course you can automate ILmerge in the post-build event, executing it from command line
  • SND
    SND over 11 years
    That sneeky dropdown triangle on the Add button is quite hard to spot! Very useful if you want a shared library say for Android and Windows with the same code, but different project wrappers.
  • Dan Bechard
    Dan Bechard over 8 years
    @StefanSteiger You do realize that pressing the run button = build & run, right? If there any outstanding changes, it will recompile the project and any dependent projects in the same solution before it runs.
  • pinki
    pinki over 7 years
    @StefanSteiger You could use Costura.Fody as an automized alternative: github.com/Fody/Costura
  • markonius
    markonius about 5 years
    How about if I wanna add a bunch of files from a directory tree? Can I add an entire directory recursively?
  • Pang
    Pang over 2 years
    Link in answer is dead - "Oops! That page can’t be found."