Sharing a library between .NET5 and .NETFramework in a single .sln

Anton 21 Reputation points
2021-10-17T13:38:46.143+00:00

Hello
I'm making a server for a multiplayer game in C# that at some point I'd like to run on a linux machine.
But before deploying it in this fashion I'd like to make a windows launcher using WinForm.
I've put my game code in a .Net5 project and it use amongst other things "Microsoft.Extensions.Hosting".
I can't reference that project from my launcher project since it is a .NetFramework
From the multiple search hit I've found, I understand that my game library should target .NetStandard instead to be agnostic.
But I've not found how to do so in visual studio 2019. I've tried modifying the .csproj TargetFramework Node (since using the interface the option doesn't show) but then I get lots of errors (for instance not finding "Microsoft.Extensions.Hosting").

I've also seen the suggestion to make the library target multiple framework, but I don't understand how to setup this in vs2019.

I'm confused as to whether what I am doing is feasible, it feels like it should be easy, but may not?

Thanks
Anton

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,347 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,206 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-10-17T15:39:34.11+00:00

    I believe you should be able to make your library target .NET Standard and you should be able to reference it in a .NET & .NET Framework project.

    For example if I had a .NET 5 project & a .NET Framework 4.7.2 project then I'd need to choose a version of .NET Standard that supports a maximum of both of those frameworks & versions. In that case it'd be netstandard2.0. You can see a table of these minimum versions that each .NET Standard version supports here:
    https://learn.microsoft.com/en-us/dotnet/standard/net-standard

    Here's a good resource for background reading on .NET Standard & .NET:
    https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/cross-platform-targeting

    There's also a section of multi-targeting in case your library needs to access APIs that may differ between .NET 5 & .NET Framework. In which case you can configure your library to target both, then in your code you can use compiler directives to make sure that the correct API is called for the current target & that will build your project for each target.


0 additional answers

Sort by: Most helpful