CS0006-Error when referencing a library-project (C#, MSBuild, .Net, .csproj, NETSDK1005) - Stack Overflow

I have a program and a library. the program has to use windows-dependency (WinForms or WPF ...) but the

I have a program and a library. the program has to use windows-dependency (WinForms or WPF ...) but the library should be platform-agnostic. I also want to be .Net-Version agnostic. So my library .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFrameworks>net481;net9.0</TargetFrameworks>
  </PropertyGroup>
</Project>

And the program .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net481-windows;net9.0-windows</TargetFrameworks>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\TestProjLib\TestProjLib.csproj" />
  </ItemGroup>
</Project>

The full solution with this test-setup can be found in my GitHub-Repository:

  • TestProjDep.sln
  • TestProjDep/**
  • TestProjLib/**

For .Net9.0 this system works and the program compiles just fine, but for .Net481 it fails with CS0009: Metafile : [...] not found. When copying the files to the needed place I get:
NETSDK1005: The Ressourcefile "[...]\TestProjLib\obj\project.assets.json" has no target for "net481-windows" [...] I already tried the following solutions:

  • Library: net481-windows instead of net481 works, but then the library is platform-dependend.
  • Library: using both net481 and net481-windows gives error: NETSDK1005

Is it a catch22 or is there a good stable solution ?

I have a program and a library. the program has to use windows-dependency (WinForms or WPF ...) but the library should be platform-agnostic. I also want to be .Net-Version agnostic. So my library .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFrameworks>net481;net9.0</TargetFrameworks>
  </PropertyGroup>
</Project>

And the program .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net481-windows;net9.0-windows</TargetFrameworks>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\TestProjLib\TestProjLib.csproj" />
  </ItemGroup>
</Project>

The full solution with this test-setup can be found in my GitHub-Repository:
https://github/joecare99/CSharp/tree/master/TestStatements

  • TestProjDep.sln
  • TestProjDep/**
  • TestProjLib/**

For .Net9.0 this system works and the program compiles just fine, but for .Net481 it fails with CS0009: Metafile : [...] not found. When copying the files to the needed place I get:
NETSDK1005: The Ressourcefile "[...]\TestProjLib\obj\project.assets.json" has no target for "net481-windows" [...] I already tried the following solutions:

  • Library: net481-windows instead of net481 works, but then the library is platform-dependend.
  • Library: using both net481 and net481-windows gives error: NETSDK1005

Is it a catch22 or is there a good stable solution ?

Share Improve this question asked Feb 1 at 10:48 Joe CareJoe Care 1288 bronze badges 10
  • 1 I don't think net481-windows is valid. It is not listed on learn.microsoft/en-us/dotnet/standard/frameworks. And since. Net 4.x is Windows only, it would not make much sense. – Klaus Gütter Commented Feb 1 at 11:00
  • !!! THANKS !!! That really helped, ! – Joe Care Commented Feb 1 at 13:08
  • 1 If the library is intended to be used with both .Net Framework and .Net, you should try to target netstandard2.0. – Jonathan Dodds Commented Feb 1 at 18:27
  • ... good point, but in this case, only the source should be independent, ... btw. I havent tested it but will this also work when the platform is "browser" ? – Joe Care Commented Feb 1 at 18:49
  • 1 @Jonathan Dodds: I just tested it and I was supprised, it really worked (even without a complaint/warning) so it's just a matter of taste. I personally like to do the libraries in the same -environment as the program. But it's good to know, that' it also works this way. Thanks, I learned a lot, today ... – Joe Care Commented Feb 2 at 16:46
 |  Show 5 more comments

1 Answer 1

Reset to default 1

As Klaus Gütter pointed out, net481-windows is not a valid MSBuild-target—even though it might work and the compiler seems to work normally?—it’s better to use net481 as the target. This applies to anyone who prefers the "new" .csproj project file type.

Side note: I was surprised to find out that I had to change the "Main" project and not the library project.

Solution program .csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFrameworks>net481;net9.0-windows</TargetFrameworks>
    <!--              ^ here  net481 instead of net481-windows -->                       
    <UseWindowsForms>true</UseWindowsForms>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\TestProjLib\TestProjLib.csproj" />
  </ItemGroup>
</Project>

This seems to work, after doing a lot of tests. More tests to do, but for now I am very thankful.

BTW: The example mentioned in the question has been reworked, and is working as expected.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741878758a4371224.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信