3

I have a project that is called framework and I have modules projects which depends on this framework project.

The problem is: Some developers on my team will have this framework code and others won't. I want to know if there is a way that if Visual Studio doesn't find this reference, it will automatically get this frameworks libs from nuget package.

carlosza
  • 365
  • 2
  • 9
  • If it is referenced as a nuget package but the dlls are missing visual studio will automatically fetch the dlls upon building the project. There is a setting in visual studio for this behaviour Tools -> Options -> NuGet Package Manager -> General | "Package Restore" | "Allow NuGet to download missing packages" - is this what you are looking for? – Rand Random Aug 27 '20 at 14:34
  • @RandRandom so, my csproj is with a tag referencing my framework libs. How does visual studio know where and what version it should get if this reference is not found? the behavior expected: it should get a correctly nuget package somewhere with version specified, – carlosza Aug 27 '20 at 14:40

2 Answers2

5

You could use Conditions to include the project only if it does exist:

<ProjectReference Include="..\..\Framework\Framework.csproj" Condition="Exists('..\..\Framework\Framework.csproj')" />

<PackageReference Include="MyCompany.Framework" Version="1.0.0" Condition="!Exists('..\..\Framework\Framework.csproj')" />
Gene
  • 4,192
  • 5
  • 32
  • 56
3

You have two options to reference other projects:

Project References

This is typically done using a monorepo approach and most times all projects are part of the same solution.

If you want to reference projects from other repositories, Git sub modules may be an option https://git-scm.com/book/en/v2/Git-Tools-Submodules

Project references look like this in your my-project.csproj file:

<ProjectReference Include="..\..\Modules\Core\Core.csproj" />

NuGet Packages

Here you build your framework and publish it as NuGet package. Then you reference the package (not the project) when needed and Visual Studio will download the specified version from your NuGet repository.

Two options to setup your own NuGet repository:

You can also use NuGet packages from local file system How do I install a NuGet package .nupkg file locally? (personally I'd not recommend this for most scenarios)

Reference NuGet packages in your my-project.csproj file:

<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />

Or using visual studios package manager: https://learn.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio

Note that you have to add your NuGet repository feed first: https://learn.microsoft.com/en-us/azure/devops/artifacts/nuget/consume?view=azure-devops

See also

Christoph Lütjen
  • 5,403
  • 2
  • 24
  • 33
  • 1
    thank you for the answer. could I use them both at the same time? If don't find project references, use Nuget Packages? – carlosza Aug 27 '20 at 15:00
  • 1
    You cannot reference the same classes in two different ways - s. link below "see also". Why should some devs use project references and other NuGet? – Christoph Lütjen Aug 27 '20 at 15:13
  • One reason to have project references and packages as a fall back is so devs can debug the code locally, but when the build pipeline runs it will pull the nuget packages – PlantationGator Nov 04 '21 at 13:34
  • You can debug nuget packages too: Add DebugSymbols to lib project and disable "just my code". – Christoph Lütjen Nov 05 '21 at 10:30