5

I am building a Blazor application which can switch between client mode and server mode. Parts of the application work only in one or the other and need to have fallback code executed in that case.

Is there a good way to check for example if Mono is running or not?

Any suggestions?

  • did you solve server/client execution "auto-switch"? [Blazor in Internet Explorer](https://stackoverflow.com/questions/53478760/blazor-in-internet-explorer) Any case with JSInterOp you can ask with script you launched. – dani herrera Nov 28 '18 at 11:08
  • I have a JavaScript solution in place but I would rather have a solution which uses C# only. – Michael A. Volz aka Flynn Nov 28 '18 at 11:55

5 Answers5

4

using RuntimeInformation.OSDescription

RuntimeInformation.OSDescription == "web"
Simon
  • 33,714
  • 21
  • 133
  • 202
3

Perhaps this can help you:

// Mono WebAssembly is running.
if (JSRuntime.Current is MonoWebAssemblyJSRuntime mono)
{
}
else
{
}

See also the instructions about BlazorDualMode which allows you to run your app in both modes as well as checking which mode is running.

Hope this helps.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
enet
  • 41,195
  • 5
  • 76
  • 113
1

Using the JSRuntime.Current is not reliable given it is null during startup. The following should work at anytime.

Type.GetType("Mono.Runtime") != null;

I add this class to the DI Container and thus can drive behavior with it.

  public class JsRuntimeLocation
  {
    public bool IsClientSide => HasMono;
    public bool IsServerSide => !HasMono;
    public bool HasMono => Type.GetType("Mono.Runtime") != null;
  }
Steven T. Cramer
  • 1,508
  • 1
  • 18
  • 34
1

It's now:

RuntimeInformation.ProcessArchitecture == Architecture.Wasm
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
0

This answer was provided by Microsoft here.

RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY"))

Major
  • 5,948
  • 2
  • 45
  • 60