简体   繁体   English

检测客户端或服务器模式

[英]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.

相关问题 WCF如何检测客户端和服务器是否在同一服务器中 - WCF how to detect if client and server are in same server 立即检测客户端与服务器套接字的断开连接 - Instantly detect client disconnection from server socket C#检测客户端与服务器的断开连接 - c# detect client disconnection from server 如何检测连接客户端是否被修改(服务器:C#,客户端:AS3) - How to detect if connecting client was modified (Server: C#, Client: AS3) C# 检测离线模式(SQL Server 连接) - C# detect offline mode (SQL Server connection) 如何在服务器端检测客户端的超时异常? - How to detect client's timeout exception on the server side? 在客户端的SQL Server 2012中检测断开的连接 - Detect broken connection in SQL Server 2012 on the client side SignalR - 用于检测客户端是否与集线器断开连接的服务器端方法? - SignalR - Server side method to detect if a client disconnects from a hub? 检测客户端是否使用 RAW 套接字向服务器发送数据包 - Detect if client is using RAW socket to send packet to server 检测到与不安全的服务器(NetworkStream)的SslStream(客户端)连接尝试? - Detect an SslStream (client) connection attempt to an insecure Server (NetworkStream)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM