0
votes

I have multiple .net core projects. My project structure is multiple independent library projects each is having different nuget package references and a main web project is having reference to these library projects.

My problem here is in my main web project i have to give the same nuget references again. if i have not given nuget references i am getting error. Is there any way i could just refer library not the nuget package reference in web project? Is there anyway just adding libraries should automatically import nuget references? How can i avoid adding the same nuget references in main project?

============================================================== I think my above question was not clear let me put this with an example

  • Solution1

    LibProject1 ->Nuget1Reference ->Nuget2Reference
    LibProject2 ->Nuget3Refernce

  • Solution2

    ->Lib1ProjectReference
    ->Lib2ProjectReference

In this structure Solution2 project is getting runtime error as it is missing nuget libraries. I have to explicitly give the same Nuget reference in Solution2 which i have already given in my library solution. How can I avoid giving the reference again in my solution2. It should have automatically get all the referenced libraries from both lib1 and lib2 to my solution2.

2
it is easier to deal with nuget actually, if you have multiple projects that use same library, just add it to both. Nuget is smart enough to download library only once and handle project references for you. You would need your both project reference this library anyway, it won't compile otherwise. - Yegor Androsov
is there any option in visual studio for this? - user1843136
right click on your project, "Manage nuget packages". You will see nugets which are already installed. you can upgrade and downgrade their versions, remove, search Nuget library etc. - Yegor Androsov
if you right click your solution icon, you will see option to manage packages for your whole solution. you will be given list of your projects and all nuget packages across it. it is really helpful in case you want to ensure all project have the same library version - Yegor Androsov
That's only supported in .NET Core SDK based projects. If your projects are still in the old format, then what you observed is by design. - Lex Li

2 Answers

0
votes

It sounds like you are creating your own NuGet packages, and then referencing those NuGet packages from the main project. This is the correct way to do this.

While the packager may do this for you, the best way to control the creation of your NuGet packages it to create a .NuSpec file for each NuGet package you are creating. You specify the files that need to be in the package, as well as the dependencies.

Specifying dependencies in the .NuSpec file you can specifically say what NuGet packages it needs, and what versions will work with it. When you reference it from the main project, it will know what NuGet packages it needs to be able to do the work.

You can read all about the NuSpec file at Microsoft: https://docs.microsoft.com/en-us/nuget/reference/nuspec

0
votes

Is there any way i could just refer library not the nuget package reference in web project? Is there anyway just adding libraries should automatically import nuget references? How can i avoid adding the same nuget references in main project?

It seems to me that your actual issue is msbuild doesn't copy those nuget references to MVC project when it should do that.

When MVC project add project reference to Library project, and Library project references some nuget packages, the expected behavior is the referenced nuget assemblies should exist in MVC's output folder during build. In your situation, they're not there, which causes the runtime error...

Please try a rebuild of MVC project, if that not work, then add this script to your project file(xx.csproj) and rebuild MVC project again:

  <PropertyGroup>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

Let me know if it helps or not :)