Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference a csproj from same solution as xproj

I have a solution with the following projects:

MySolution.sln
    - MySolution.Client.csproj
    - MySolution.Service.csproj
    - MySolution.Models.csproj
    - MySolution.Server.xproj

MySolution.Models is a simple class library which contains shared code that is referenced by MySolution.Client and MySolution.Service - and I would like to reference it in MySolution.Server.

The GUI in VS 2015 RC1 lets me add the reference by right clicking References -> Add Reference. I then see all my projects under Projects -> Solution.

I select MySolution.Models and click Ok, after which I receive the following error in the output log:

Errors in ...PathToSolution\MySolution.Server\project.json
    Unable to locate MySolution.Models >= 1.0.0-*

It really feels like this should work, since the GUI allows me to add the reference without any hiccups.

like image 710
Inrego Avatar asked Jun 02 '15 00:06

Inrego


People also ask

How to Add a project Reference?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference. For more information, see How to: Add or remove references.

How to Add project Reference in Visual Studio code?

Right click on project --> Add Reference --> Select the reference project from the list.

Can I use csproj and xproj at the same time?

Luckily, with Core 1.0 you can use both csproj and xproj at the same time by having different solution files. Another major obstacle is the project.json file. This file is new for most developers working with .NET Core, but it isn’t actually new (it is also being utilized by universal Windows apps).

What is a xproj file in Visual Studio 2015?

The first .NET Core projects created in Visual Studio 2015 were represented by an .xproj file (similar to .csproj) and the dependencies (NuGet package references) stored in a project.json file. This approach is now deprecated.

How to add a project reference to a component file?

You can add project references only to the projects in your solution. Component Files refers to COM component files. I don't remember ever adding a project reference through the Browse button You can add the project to your solution and then reference it from there. You can also check out this question.

What are the biggest obstacles to using csproj?

Another major obstacle is the project.json file. This file is new for most developers working with .NET Core, but it isn’t actually new (it is also being utilized by universal Windows apps). If you add a project.json file to your application folder, your csproj will start trying to use it.


Video Answer


1 Answers

So the first thing to understand is DNX projects have no understanding of traditional .net projects. They don't read or parse csproj files. This is done to keep them cross platform and cross IDE compatible (csproj is a distinctly windows and VS specific thing).

When you add a reference to a "legacy" (I use legacy to mean a .net 4.x csproj based project) behind the scenes the IDE will run dnu wrap but it looks like in your case something broke.

The following should be done automatically.

  1. In solution root global.json a folder "wrap" should be added to the projects property.
  2. A folder off the root named "wrap" will be created if it doesn't exist.
  3. A /wrap/project.json will be created/updated with a path to the assembly (dll).
  4. Add a reference to the assembly and version to the referencing project's project.json file.

So first thing to check is make sure you have a "wrap" folder and wrap reference in projects property of solution.json. If you don't then likely something "broke". Try removing the reference rebuilding and adding the reference back. Check the build output window for any errors (VS is still RC so there are something error which probably should be halting that are not).

Look for a project.json in the wrap folder. It should look something like this:

{
  "version": "1.0.0-*",
  "frameworks": {
    "net452": {
      "wrappedProject": "../../LegacyClassLibrary/LegacyClassLibrary.csproj",
      "bin": {
        "assembly": "../../LegacyClassLibrary/obj/{configuration}/LegacyClassLibrary.dll",
        "pdb": "../../LegacyClassLibrary/obj/{configuration}/LegacyClassLibrary.pdb"
      }
    }
  }
}

Note the framework version. If there is a mismatch then it will fail resolving the dependencies. For example if your MySolution.Models targets .Net 4.6 and thus when wrapped has a dnx46 framework reference but your MySolution.Server project has a reference to dnx452 (in the project.json for MySolution.Server) then it will fail when resolving the dependency to MySolution.Models.

The you quoted could probably be improved. It means that it could not resolve the dependency due to one of the following reasons

  • It could not find a MySolution.Models assembly (either source code or compiled dll) based on the paths it uses (starting from projects parameter in global.json).
  • It found a MySolution.Models assembly (either source code or compiled) BUT it was an invalid version. Check version in Models project vs the reference in Server project.json.
  • It found a MySolution.Models assembly but it can't resolve framework dependencies (i.e. Models requires dnx46 but Server only targets dnx452).

In my experience the third one if the most common. For the DNX templates in VS 2015 RC the default full framework being targeted is dnx452 (or is it dnx451?). New csproj projects will be 4.6 (dnx46) by default and existing projects could be just about anything.

An alternative solution: I have found the following alternative to result in easier dependency management. If MySolution.Models will only be used by DNX projects then just convert it to a DNX project move it into the source folder and reference it directly. It will be part of the source compilation and you gain the benefits of dynamic compilation.

If MySolution.Models will be referenced by both DNX and legacy (csproj) projects then you can create a side-by-side xproj and project.json files for Models. They will be ignored by the legacy project. In essence you have both a legacy and DNX project using the same source files. You can then just like above reference it directly. Keep in mind the folder structure if the models folder is not under /src (and it probably isn't if this was an existing project) then you will either need to move it or add a reference to the folder in global.json. That sounded more confusing that it really is. Just keep in mind for a DNX project the global.json defines the relative paths to where DNX can find source code. The DNX also can resolve dependencies by nuget or searching the GAC but that is beyond what you are trying to do.

like image 137
Gerald Davis Avatar answered Oct 03 '22 19:10

Gerald Davis