3

I would like to know how I can share c# source codes between two (or more) .NET Core projects (commandline projects!).

As far as I understand, I can not link to source files in different directories in xproj/project.json based projects. I noticed that it now seems to be recommended to create nuget packages for everything. But is it really necessary for me to setup a private repository and create a nuget package only to be able to share some common source units?

VS2015 contains a template for .NET Core library which may be suitable for building a shared lib. Is it possible to link this lib to a project without a nuget package?

2 Answers 2

3

.NET Core Library is an excellent solution for you.

Do it the same way as in standard C# solution - just create the project and reference this project or add a reference to DLL file.

You don't need to use a Nuget, for your own purpose. Nuget packages could be useful to distribute your dll outside.


Clarification:

I miss one point - I'm using VS2015, but I have included Class Library project in my solution, and I'm referencing by project, not by DLL file, and this works fine in ASP.Net Core.

I also have a different project, where referencing DLL file directly working fine, but this is the previous version of ASP.NET app (not Core) - seems NET Core doesn't support this way like as the previous version (yet?).

Sorry for confusing you, sometimes it's too many technologies ;)


So could you just include ClassLibrary project into solution with your project and refer it as a project?

Sign up to request clarification or add additional context in comments.

3 Comments

Which version of VS do you use? I just tried what you suggest with VS2015 and get: .NET Core projects only support referencing .NET framework assemblies in this release. To reference other assemblies, they need to be included in a NuGet package and reference that package.
Sorry, I miss one point - just too many technologies ;) - Yes, I'm using VS2015, but I have included Class Library project in my solution, and I'm referencing by project, not by DLL file, and this works fine in ASP.Net Core. I also have a different project where I have few references to DLL files directly, but this is the previous version of ASP.NET app (not Core). So could you just include ClassLibrary project into your solution and refer it those project?
Ok, cross project references seem to work. This is an acceptable solution for my case.
1

I have achieved this by using source control to branch from my commonly used projects in each new solution, and again merging back to the master branch if I make any changes.

Alternatively, baring in mind that NuGet is only an archived collection of files, you could keep this NuGet package locally, or even create a Template for Visual Studio that has the common libraries by default.

There are a wide range of possibilities that are down to your preference, and current environment state (I.E: Able to setup Source Control, or a package repository).

1 Comment

The idea to use the source control system is interesting. Unfortunately I am looking specifically for a solution for small test/side projects where I want to share between 1 and 10 source files and sometimes don't even put the projects into source control! For larger projects I would prefer the local Nuget solution but for the small projects, I think, Lukasz Makowejs solution with cross-project references is the easiest for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.