C
C#6mo ago
monke

`OperatingSystem.IsAndroid()` Alternative for .NET Standard 2.0

I am moving a library from .NET 8 to .NET Standard 2.0 since I would like to use it in a legacy .NET Framework project as well. The only issue that I haven't been able to figure out a good way to resolve is OperatingSystem.IsAndroid(). This library includes an API client in it for our backend, so it gets used in both Windows desktop and an Android/iOS MAUI app and I use that to add a header so I know which platform the request came from. Right now I've replaced that with RuntimeInformation.OSDescription.ToLower().Contains("android"), but this feels extremely fragile to me so I'm a bit worried with keeping it that way long term. Are there any good (non 3rd party nuget package) alternatives or ways to use OperatingSystem.IsAndroid()? I had to add the System.Text.Json library to get those classes back and am just not sure if there is a similar one for this OperatingSystem class. The big issue I'm running into trying to find info on this is everything is recommending using RuntimeInformation.IsOSPlatform(OSPlatform.*), however this does not contain a platform for android.
34 Replies
Angius
Angius6mo ago
Could maybe use pragmas?
#if NET5_0_OR_GREATER
bool IsAndroid = OperatingSystem.IsAndroid();
#else
bool IsAndroid = false;
#endif
#if NET5_0_OR_GREATER
bool IsAndroid = OperatingSystem.IsAndroid();
#else
bool IsAndroid = false;
#endif
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius6mo ago
Alternatively, bite the bullet, update the legacy project, and never have to worry about .NET Fx existing
monke
monkeOP6mo ago
Thats not possible This legacy application was first written in 2000-somthing and started on Framework 2.0, its old and extremely fragile. Its slowly being replaced with a .NET 8 API, but it takes time
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
I think #if NET5_0_OR_GREATER would always be false at compile time, since its being built with standard 2.0
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
again, upgrading the legacy app from .NET framework is not possible
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
yeah I get that, not trying to do it in the framework app. Trying in .NET Standard
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius6mo ago
Standard is the lowest common denominator
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius6mo ago
If something does not exist on Fx, it does not exist on Standard 2.0
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
The link you sent says to use OperatingSystem.IsAndroid(), which doesn't exist in .net standard
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
Yeah I looked in that repo to see what this class did, coped it and it doesn't work https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs#L201
GitHub
runtime/src/libraries/System.Private.CoreLib/src/System/OperatingSy...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
Not sure what you mean by one level deeper, theres nothing to step into here
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
if android doesn't exist for .NET Standard 2.0, then why does it run fine on android? That seems a bit odd to me, for it to run there fine but not know about its existence?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
I have been working with .NET Framework stuff since about 2020 and started migrating parts of this app to .NET in 2022, this is my first time trying to do anything with .NET Standard since I've never had to share code between them before
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
Ok so I think I understand from your explanation
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic6mo ago
RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))
RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))
the other alternative is to not use netstandard2.0 and to multitarget and use #if instead
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic6mo ago
i mean, it's a netstandard2.0 libary, i think it is acceptable that the library considers multiple possible platforms that it can be used on
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
Yeah, let me try to give a better explanation
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
monke
monkeOP6mo ago
Currently we have: - Legacy monolithic ASP.NET application (.NET Framework) - Legacy Xamarin app - New API (.NET 8) - New API client library (.NET 8) - New MAUI app < -- Uses the New API client library - New Blazor Web (.NET 8) < -- Uses the New API client library And would like to add the API Client library to the legacy application so we can start moving more parts of it to the new API And in the API client library we add a header to all requests that say which platform the request is coming from, currently using the OperatingSystem.IsAndroid() method No problem at all, thank you for your help!
Want results from more Discord servers?
Add your server