[英]Detect client or server mode
I am building a Blazor application which can switch between client mode and server mode.我正在构建一个 Blazor 应用程序,它可以在客户端模式和服务器模式之间切换。 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?有没有好的方法来检查 Mono 是否正在运行?
Any suggestions?有什么建议么?
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.另请参阅有关 BlazorDualMode的说明,它允许您在两种模式下运行您的应用程序以及检查正在运行的模式。
Hope this helps.希望这可以帮助。
使用RuntimeInformation.OSDescription
RuntimeInformation.OSDescription == "web"
Using the JSRuntime.Current is not reliable given it is null during startup.使用 JSRuntime.Current 是不可靠的,因为它在启动期间为空。 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.我将这个类添加到 DI 容器,因此可以用它来驱动行为。
public class JsRuntimeLocation
{
public bool IsClientSide => HasMono;
public bool IsServerSide => !HasMono;
public bool HasMono => Type.GetType("Mono.Runtime") != null;
}
It's now:下雪了:
RuntimeInformation.ProcessArchitecture == Architecture.Wasm
这个答案是由微软在这里提供的。
RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.