question

AntonRoy-1681 avatar image
0 Votes"
AntonRoy-1681 asked Paul-5034 commented

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

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

dotnet-csharpdotnet-standard
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 commented

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://docs.microsoft.com/en-us/dotnet/standard/net-standard

Here's a good resource for background reading on .NET Standard & .NET:
https://docs.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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot
After asking the question, in true rubber ducking fashion I've been able to understand what I was doing wrong.
For some reason I was persuaded that .NET Framework was a superset of .NET 5. Of course, with this in mind, I wasn't understanding the answer I was reading.

And I've even stumbled upon the .Net 5 implementation of WinForm. This is an even simpler solution to my problem since it remove the need for a multi target.

0 Votes 0 ·

True, .NET naming/version conventions can be a bit bewildering if you're not familiar with the evolution - glad you were able to solve your problem.

1 Vote 1 ·