0

There's a git repository which consists of different projects and solutions. I have a project in ..\Utils\MyProject\ folder

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net45</TargetFramework>
  </PropertyGroup>
</Project>

and when I try to add a reference to a project (not created by me) in ..\Utils\OtherSolution\OtherProject folder, I'm getting the following:

Project `C:\Users\username\source\repos\gitRepo\Utils\OtherSolution\OtherProject\OtherProject.csproj` cannot be added due to incompatible targeted frameworks between the two projects. Review the project you are trying to add and verify that is compatible with the following targets:
    - net45

This project also targets 4.5:

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{FDFA263A-9C0E-410D-A0D7-D1F230EA95BB}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>OtherProject</RootNamespace>
    <AssemblyName>OtherProject</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <TargetFrameworkProfile />
  </PropertyGroup>

Why's that and how can I reference it?

DisplayMyName
  • 329
  • 3
  • 15
  • What .net versions do both projects have? e.g. net framework and .net standard or both .net framework? - also this looks like you're combining two executable projects (maybe 2 console projects?) - should you not want to have 1 console project with a reference to a class library project? – sommmen Oct 01 '19 at 13:55
  • Yes, I'm trying to combine two executable projects. Is that not possible? Ideally reference to a class library project would be perfect, however at this point I cannot change it. How do I check what versions both projects have? I thought I did in .csproj files? – DisplayMyName Oct 01 '19 at 14:09
  • That is possible - I was just curious as you normally do not do that. in visual studio press right click on the project, then select properties from the context menu and check the target framework box. They should be the same as in the project file - but mine differs a bit. Microsoft changed some things around though. – sommmen Oct 01 '19 at 14:18
  • Yes, there are some differences. 'MyProject', as VS shows, is targeting .NET Standard 1.0, while 'OtherProject' is .NET 4.5. My question is however, how does VS know? These settings should be seen on some configuration file, which I should be able to open, no? Also, I thought that .NET Standard 1.0 and .NET 4.5 are compatible? – DisplayMyName Oct 02 '19 at 07:07

1 Answers1

0

From the comments it became apparent that your setup is like so:

A -> B

A references B, where A is a .NET project and B is a .NET standard project.

Realize this: .NET is the full framework whilst .NET standard is only a subset of the full framework. This means that in your setup you're trying trying to reference a full framework from a project that only has a subset of the full framework. This cannot work.

The other way around is only possible:

C -> D Where C is a .NET standard project and D a .NET project.

This means in order to solve your problem, you should move all code you want to share in ..\Utils\OtherSolution\OtherProject to a .NET standard class library project and reference that in your ..\Utils\MyProject\ project. You simply cannot reference a .NET standard project from a .NET project.

You mentioned that the ..\Utils\OtherSolution\OtherProject is not created by you, which means that the project may be out of your control and not editable. In that case it becomes difficult. The only way to reference that project in this case is to make ..\Utils\MyProject\ a .NET project rather than .NET standard:

..\Utils\OtherSolution\OtherProject -> ..\Utils\MyProject\ Where both projects are .NET

Some additional sources:

.NET standard docs

A related question

sommmen
  • 6,570
  • 2
  • 30
  • 51
  • The thing is, even if I change MyProject so that it targets 4.5, and VS starts showing that, I still get the same error when trying the `dotnet add reference` command. – DisplayMyName Oct 02 '19 at 09:03
  • you're using the CLI? are you using .net core? – sommmen Oct 02 '19 at 09:20
  • also you need to create a new, normal .net project, just use visual studio not the add ref command. Those are all for .net core and standard and you need a regular .net project to reference another .net project – sommmen Oct 02 '19 at 09:22