.NET Core 3.0方案中的Blazor服务器和性能
什么是Blazor服务器? (What is Blazor Server?)
Blazor Server apps host Blazor components on the server and handle UI interactions over a real-time SignalR connection. As the user interacts with the app, the UI events are sent to the server over the connection to be handled by the various components that make up the app. When a component handles a UI event, it’s rendered based on its updated state. Blazor compares the newly rendered output with what was rendered previously and send the changes back to the browser and applies them to the DOM.
Blazor服务器应用程序在服务器上托管Blazor组件,并通过实时SignalR连接处理UI交互。 当用户与应用程序交互时,UI事件通过连接发送到服务器,由组成应用程序的各种组件处理。 当组件处理UI事件时,将根据其更新状态进行呈现。 Blazor将新渲染的输出与先前渲染的输出进行比较,然后将更改发送回浏览器,并将其应用于DOM。
Since Blazor Server apps run on .NET Core on the server, they enjoy all the benefits of running on .NET Core including great runtime performance and tooling. Blazor Server apps can leverage the full ecosystem of .NET Standard libraries without any browser imposed limitations.
由于Blazor Server应用程序在服务器上的.NET Core上运行,因此它们享受在.NET Core上运行的所有好处,包括出色的运行时性能和工具。 Blazor Server应用程序可以利用.NET Standard库的完整生态系统,而不受任何浏览器的限制。
我什么时候应该使用Blazor Server? (When should I use Blazor Server?)
Blazor Server enables you to add rich interactive UI to your .NET apps
Blazor Server使您能够立即将丰富的交互式UI添加到.NET应用程序中today without having to write JavaScript. If you need the interactivity of a single-page app in your .NET app, then Blazor Server is a great solution.,而无需编写JavaScript。 如果您需要.NET应用程序中单页应用程序的交互性,那么Blazor Server是一个很好的解决方案。
Blazor Server can be used to write completely new apps or to complement existing MVC and Razor Pages apps. There’s no need to rewrite existing app logic. Blazor is designed to work together with MVC and Razor Pages, not replace them. You can continue to use MVC and Razor Pages for your server-rendering needs while using Blazor for client-side UI interactions.
Blazor Server可用于编写全新的应用程序或补充现有的MVC和Razor Pages应用程序。 无需重写现有的应用逻辑。 Blazor旨在与MVC和Razor Pages一起使用,而不是替换它们。 在使用Blazor进行客户端UI交互时,您可以继续使用MVC和Razor页面来满足服务器渲染的需求。
Blazor Server works best for scenarios where you have a reliable low-latency network connection, which is normally achieved when the client and server are geographically on the same continent. Apps that require extremely high fidelity instant updates on every tiny mouse twitch, like real-time games or drawing apps, are not a good fit for Blazor Server. Because Blazor Server apps require an active network connection, offline scenarios are not supported.
Blazor Server最适用于您具有可靠的低延迟网络连接的方案,通常,这在客户端和服务器位于同一大陆的地理位置时可以实现。 需要对每个微小的鼠标轻触进行极高保真度即时更新的应用程序,例如实时游戏或绘图应用程序,都不适合Blazor Server。 由于Blazor服务器应用程序需要活动的网络连接,因此不支持脱机方案。
Blazor Server is also useful when you want to offload work from the client to the server. Blazor Server apps require only a small download to establish the connection with the server and to process UI interactions. All the hard work of running the app logic and rendering the UI is then done on the server. This means Blazor Server apps load fast even as the app functionality grows. Because the client side of a Blazor Server app is so thin, it’s a great solution for apps that need to run on low-powered devices.
当您要将工作从客户端转移到服务器时,Blazor Server也很有用。 Blazor Server应用程序仅需少量下载即可建立与服务器的连接并处理UI交互。 然后,在服务器上完成运行应用程序逻辑和呈现UI的所有艰苦工作。 这意味着即使应用程序功能增长,Blazor Server应用程序也可以快速加载。 由于Blazor Server应用程序的客户端非常薄,因此对于需要在低功耗设备上运行的应用程序而言,这是一个很好的解决方案。
大规模使用Blazor Server (Using Blazor Server at scale)
Blazor Server can scale from small internal line of business apps to large internet scale apps. While .NET Core 3.0 was still in preview we tested Blazor Server to see what its baseline scale characteristics look like. We put a Blazor Server app under load with active clients and monitored the latency of the user interactions. In our tests, a single
Blazor Server可以从小型内部业务应用程序扩展到大型Internet规模应用程序。 当.NET Core 3.0仍处于预览状态时,我们对Blazor Server进行了测试,以查看其基线比例特征。 我们将Blazor Server应用程序置于活动客户端的负载下,并监视了用户交互的延迟。 在我们的测试中,Azure上的单个Standard_D1_v2 instance on Azure (1 vCPU, 3.5 GB memory) could handle over 5,000 concurrent users without any degradation in latency. Standard_D1_v2实例(1个vCPU,3.5 GB内存)可以处理5,000多名并发用户,而不会降低延迟。
A
一个Standard_D3_V2 instance (4 vCPU, 14GB memory) handled well over 20,000 concurrent clients. The main bottleneck for handling further load was available memory. Will you see this level of scale in your own app? That will depend in large part on how much additional memory your app requires per user. But for many apps, we believe this level of scale out is quite reasonable. We also plan to post additional updates on improvements in Blazor Server scalability in the weeks ahead. So stay tuned!Standard_D3_V2实例(4个vCPU,14GB内存)可以很好地处理超过20,000个并发客户端。 处理更多负载的主要瓶颈是可用内存。 您会在自己的应用程序中看到这种规模的扩展吗? 这在很大程度上取决于每个用户您的应用程序需要多少额外的内存。 但是对于许多应用程序,我们认为这种水平扩展是相当合理的。 我们还计划在未来几周内发布有关Blazor Server可扩展性改进的其他更新。 敬请期待!
什么是Blazor WebAssembly? (What is Blazor WebAssembly?)
Blazor is a UI framework that can run in different environments. When you build UI components using Blazor, you get the flexibility to choose how and where they are hosted and run. As well as running your UI components on the server with Blazor Server, you can run those same components on the client with Blazor WebAssembly. This flexibility means you can adapt to your users’ needs and avoid the risk of being tied to a specific app hosting model.
Blazor是可以在不同环境中运行的UI框架。 使用Blazor构建UI组件时,您可以灵活选择托管和运行方式以及托管方式。 除了使用Blazor Server在服务器上运行UI组件外,还可以使用Blazor WebAssembly在客户端上运行这些相同的组件。 这种灵活性意味着您可以适应用户的需求,并避免与特定的应用程序托管模型绑定的风险。
Blazor WebAssembly apps host components in the browser using a WebAssembly-based .NET runtime. The components handle UI events and execute their rendering logic directly in the browser. Blazor WebAssembly apps use only open web standards to run .NET code client-side, without the need for any browser plugins or code transpilation. Just like with Blazor Server apps, the Blazor framework handles comparing the newly rendered output with what was rendered previous and updates the DOM accordingly, but with Blazor WebAssembly the UI rendering is handled client-side.
Blazor WebAssembly应用程序使用基于WebAssembly的.NET运行时在浏览器中托管组件。 这些组件处理UI事件并直接在浏览器中执行其呈现逻辑。 Blazor WebAssembly应用程序仅使用开放的Web标准来在客户端运行.NET代码,而无需任何浏览器插件或代码转换。 就像使用Blazor Server应用程序一样,Blazor框架处理将新呈现的输出与先前呈现的输出进行比较,并相应地更新DOM,但是使用Blazor WebAssembly时,UI呈现是在客户端处理的。
我什么时候应该使用Blazor WebAssembly? (When should I use Blazor WebAssembly?)
Blazor WebAssembly is still in preview and isn’t yet ready for production use yet. If you’re looking for a production ready solution, then Blazor Server is what we’d recommend.
Blazor WebAssembly仍在预览中,尚未准备好用于生产。 如果您正在寻找可用于生产环境的解决方案,那么我们建议使用Blazor Server。
Once Blazor WebAssembly ships (May 2020), it will enable running Razor components and .NET code in the browser on the user’s device. Blazor WebAssembly apps help offload work from the server to the client. A Blazor WebAssembly app can leverage the client device’s compute, memory, and storage resources, as well as other resources made available through standard browser APIs.
Blazor WebAssembly交付后(2020年5月),它将启用在用户设备上的浏览器中运行Razor组件和.NET代码的功能。 Blazor WebAssembly应用程序帮助将工作从服务器转移到客户端。 Blazor WebAssembly应用程序可以利用客户端设备的计算,内存和存储资源,以及通过标准浏览器API提供的其他资源。
Blazor WebAssembly apps don’t require the use of .NET on the server and can be used to build static sites. A Blazor WebAssembly app is just a bunch of static files that can be hosted using any static site hosting solution, like GitHub pages or Azure Static Website Hosting. When combined with a service worker, a Blazor WebAssembly app can function completely offline.
Blazor WebAssembly应用程序不需要在服务器上使用.NET,可用于构建静态网站。 Blazor WebAssembly应用程序只是一堆静态文件,可以使用任何静态网站托管解决方案来托管,例如GitHub页面或Azure静态网站托管。 与服务人员结合使用时,Blazor WebAssembly应用程序可以完全脱机运行。
When combined with .NET on the server, Blazor WebAssembly enables full stack web development. You can share code, leverage the .NET ecosystem, and reuse your existing .NET skills and infrastructure.
与服务器上的.NET结合使用时,Blazor WebAssembly可以进行完整堆栈的Web开发。 您可以共享代码,利用.NET生态系统,并重用现有的.NET技能和基础结构。
Including a .NET runtime with your web app does increase the app size, which will impact load time. While there are a variety of techniques to mitigate this (prerendering on the server, HTTP caching, IL linking, etc.), Blazor WebAssembly may not be the best choice for apps that are very sensitive to download size and load time.
在Web应用程序中包含.NET运行时会增加应用程序的大小,这会影响加载时间。 尽管有多种技术可以减轻这种情况(服务器上的渲染,HTTP缓存,IL链接等),但对于对下载大小和加载时间非常敏感的应用程序,Blazor WebAssembly可能不是最佳选择。
Blazor WebAssembly apps also require a browser that supports WebAssembly. WebAssembly is supported by all modern browsers, including mobile and desktop browsers. However, if you need to support older browsers without WebAssembly support then Blazor WebAssembly isn’t for you.
Blazor WebAssembly应用程序还需要支持WebAssembly的浏览器。 所有现代浏览器(包括移动和桌面浏览器)都支持WebAssembly。 但是,如果需要在不支持WebAssembly的情况下支持较旧的浏览器,则Blazor WebAssembly不适合您。
Blazor WebAssembly is optimized for UI rendering scenarios, but isn’t currently great for running CPU intensive workloads. Blazor WebAssembly apps today use a .NET IL interpreter to execute your .NET code, which doesn’t have the same performance as a native .NET runtime with JIT compilation. We’re working to better address this scenario in the future by adding support for compiling your .NET code directly to WebAssembly instead of using an interpreter.
Blazor WebAssembly已针对UI渲染方案进行了优化,但目前不适用于运行CPU密集型工作负载。 今天的Blazor WebAssembly应用程序使用.NET IL解释器来执行.NET代码,其性能与使用JIT编译的本机.NET运行时不同。 将来,我们将通过添加对将.NET代码直接编译到WebAssembly而不是使用解释器的支持,来更好地解决这种情况。
你以后可以改变主意 (You can change your mind later)
Regardless of whether you choose Blazor Server or Blazor WebAssembly, you can always change your mind later. All Blazor apps use a common component model, Razor components. The same components can be hosted in a Blazor Server app or a Blazor WebAssembly app. So if you start with one Blazor hosting model and then later decide you want to switch to a different one, doing so is very straight forward.
无论您选择Blazor Server还是Blazor WebAssembly,都可以在以后随时改变主意。 所有Blazor应用程序均使用通用组件模型Razor组件。 相同的组件可以托管在Blazor Server应用程序或Blazor WebAssembly应用程序中。 因此,如果您从一个Blazor托管模型开始,然后又决定要切换到另一个,则非常简单。
Blazor的下一步是什么? (What’s next for Blazor?)
After shipping Blazor WebAssembly, we plan to expand Blazor to support not just web apps, but also Progressive Web Apps (PWAs), hybrid apps, and even fully native apps.
在发布Blazor WebAssembly之后,我们计划扩展Blazor,使其不仅支持Web应用程序,还支持渐进式Web应用程序(PWA),混合应用程序甚至完全本机应用程序。
- Blazor PWAs: PWAs are web apps that leverage the latest web standards to provide a more native-like experience. PWAs can support offline scenarios, push notifications, and OS integrations, like support for pinning the app to your home screen or the Windows Start menu. Blazor PWA:PWA是利用最新Web标准来提供更原生体验的Web应用程序。 PWA可以支持脱机方案,推送通知和操作系统集成,例如支持将应用程序固定到主屏幕或Windows“开始”菜单。
-
Blazor Hybrid: Hybrid apps are native apps that use web technologies for the UI. Examples include Electron apps and mobile apps that render to a web view. Blazor Hybrid apps don’t run on WebAssembly, but instead use a native .NET runtime like .NET Core or Xamarin. You can find an experimental sample for using Blazor with Electron on GitHub.
Blazor混合动力:混合动力应用程序是将Web技术用于UI的本机应用程序。 示例包括可呈现为Web视图的Electron应用程序和移动应用程序。 Blazor Hybrid应用程序不在WebAssembly上运行,而是使用本机.NET运行时(如.NET Core或Xamarin)。 您可以在GitHub上找到将Blazor与Electron结合使用的实验样本 。
- Blazor Native: Blazor apps today render HTML, but the renderer can be replaced to render native controls instead. A Blazor Native app runs natively on the devices and uses a common UI abstraction to render native controls for that device. This is very similar to how frameworks like Xamarin Forms or React Native work today. Blazor本机:Blazor应用程序现在可以呈现HTML,但是可以替换呈现器以呈现本机控件。 Blazor本机应用程序在设备上本机运行,并使用通用的UI抽象来呈现该设备的本机控件。 这与Xamarin Forms或React Native等框架今天的工作方式非常相似。
These three efforts are all currently experimental. We expect to have official previews of support for Blazor PWAs and Blazor Hybrid apps using Electron in the .NET 5 time frame (Nov 2020). There isn’t a road map for Blazor Native support yet, but it’s an area we are actively investigating.
这三个努力目前都处于试验阶段。 我们预计将在.NET 5时间框架(2020年11月)中正式支持使用Electron的Blazor PWA和Blazor Hybrid应用程序的支持。 目前尚没有Blazor Native支持的路线图,但这是我们正在积极研究的领域。
摘要 (Summary)
With .NET Core 3.0, you can build rich interactive client-side UI
在.NET 3.0的核心,你可以today with Blazor Server. Blazor Server is a great way to add client-side functionality to your existing and new web apps using your existing .NET skills and assets. Blazor Server is built to scale for all your web app needs. Blazor WebAssembly is still in preview, but is expected to ship in May of next year. In the future we expect to continue to evolve Blazor to support PWAs, hybrid apps, and native apps. For now, we hope you’ll give Blazor Server a try 今天构建丰富的交互式客户端UI与Blazor服务器。 Blazor Server是使用现有.NET技能和资产将客户端功能添加到现有和新Web应用程序的好方法。 Blazor Server的构建旨在满足您所有Web应用程序的需求。 Blazor WebAssembly仍在预览中,但预计将于明年5月发布。 将来,我们希望Blazor继续发展以支持PWA,混合应用程序和本机应用程序。 现在,我们希望您可以by installing .NET Core 3.0!通过安装.NET Core 3.0来尝试Blazor Server!