5

Recently I started messing with the xproj projects in Visual Studio 2015. While building a web app, I have a class library located in a different solution that I'm able to reference using a global.json file. I love this feature because you can build it all in one shot as opposed to switching between Visual Studio instances.

I find out now Microsoft is moving away from these projects and going back to the csproj way of doing things. I'm all about keeping up with best practices, but I can't seem to figure out how to bring external projects into a solution in the same way you can do it with the global.json. I'm also embarrassed to admit I can't seem to think of how to google for this.

Is there a new, recommended way of doing this in Visual Studio 2017?

Chris Lees
  • 2,140
  • 3
  • 21
  • 41
  • 4
    I am probably missing something in your question, but you can just Right-click the solution in Solution Explorer, then choose Add -> Existing Project -> and navigate to the project you want to add. – Rufus L Mar 21 '17 at 19:38
  • @RufusL the more I think about it, this is probably the way to do it. It feels so much different from the way the global.json file has it set up, but I guess in theory it's the exact same thing as what you're proposing. – Chris Lees Mar 21 '17 at 20:05
  • 1
    If your project is outside of your solution, then it most likely makes sense *not* to directly reference it. If the project exists outside the solution, it likely means that it's intended to be consumed by other solutions besides yours, right? So if you end up needing to change that project, you probably change it for all the other solutions, potentially breaking them. The world has moved beyond direct references like this. This is why we have NuGet. Create NuGet packages from the project and reference them in your own solution. – mason Mar 21 '17 at 20:07
  • @mason NuGet makes things so much more complicated. I understand what you mean though. Not sure NuGet would be the way to go for us because this is a class library that is only used internally by a couple of projects. Any changes we make are easily fixed across the couple projects that might be effected. – Chris Lees Mar 21 '17 at 20:21
  • In my similar experience, not using NuGet was far more of a hassle. What's your fear of NuGet? – mason Mar 21 '17 at 20:23
  • @mason I really like the ability to code in both solutions at the same time and build once. Going the NuGet route I'd need two instances of Visual Studio making changes to the class library, compiling there, then changing the application and compiling there. Also we had a lot of weirdness happen on our Jenkins box around NuGet packages getting cached somehow and needing to manually clean out some super obscure folder location before the build would work again. – Chris Lees Mar 21 '17 at 20:29

1 Answers1

1

We can use the dotnet command line inteface to add an existing project...

to a project:

C:\temp\Bar> dotnet add .\Bar.csproj reference ..\Foo\Foo.csproj

to a solution:

C:\temp\Bar> dotnet sln .\Bar.sln add ..\Foo\Foo.csproj
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • Does this make it so the referenced projects files show up in the solution explorer like it would were I to open it's solution directly? The reason I like the global.json file is I can edit all of the files in both projects from one instance of Visual Studio and build them all in one shot. – Chris Lees Mar 21 '17 at 19:55
  • @ChrisLees Aha. You want to add it to the solution not to the project. – Shaun Luttin Mar 21 '17 at 20:01
  • Do you know if dotnet sln add... supports linking to xproj files? When I try it crashes with an exception complaining about not being able to find some sdk file:Unhandled Exception: Microsoft.Build.Exceptions.InvalidProjectFileException: The imported project "C:\Program Files\dotnet\sdk\1.0.0\Microsoft\VisualStudio\v15.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. – Mark Olbert Mar 29 '17 at 15:42