Reputation: 1182
I'm developing a WebApi project on vscode with .Net Core target framework.. I have this scenario that some part of the code (db interactions and common utils) is going to be part of many other separate projects than this particular WebApi project. So I decided to code it separately in another folder as another project..
Yet I can't figure out how I am going to do this.. Whether as a project reference or an output file as dependency, doesn't matter, I couldn't figure out to use second project in the first one.. I just basically want to build a library compatible with .net core, and use it in many other .net core projects.
I need some guideline and best practice suggestions here.
UPDATE :
I think i've found a way and it seems to be working for now.
1- Adding <ProjectRefence>
to FirstProject.csproj file :
<ProjectReference Include="..\SecondProject\SecondProject.csproj"></ProjectReference>
2- Add a project.json
file to FirstProject and setting a dependency for SecondProject.
{
"dependencies": {
"SecondProject": {
"target": "project"
}
}
}
3- dotnet restore
and build.
4- Restart vscode for proper intellisence.
I was just too new to VS Code environment when I asked this question. I was expecting some IDE features for referencing projects to each other.
Upvotes: 0
Views: 3798
Reputation: 712
Depending on your OS / IDE / version of .Net Core, you may or may not need the CSPROJ update. I think you need them present with .Net Core 2+ / VS17+, and probably some other permutations of tooling.
You may need to expand your dependencies
block. I found that based on OS and IDE, sometimes I needed to specifically call out some of the dependencies; they didn't always carry over transitively. For example, I ended up with a block like this when referencing another project, to get things compiling:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"NETStandard.Library": "1.6.1",
...
"OtherProjectNameHere": { "target": "project" }
},
Your specific set of dependencies will vary.
You may also need to make adjustments to the frameworks
block in similar spirit.
A more rigorous option you can consider is to put your library / SecondProject
into its own repository, and work with it as a NuGet package. If this is a library you're going to use across many projects or many teams, you may benefit from a package reference rather than a project reference. You'd gain:
In short, libraries are products, too. You may want to treat it like one, if you're in a company / situation where that makes sense.
Upvotes: 1