Blazor 服务器端与 Blazor WebAssembly 托管
我是 Blazor 新手,正在尝试了解不同托管模型之间的差异。
根据我的阅读,我了解到 Blazor 服务器端和 Blazor WebAssembly Hosted 都有服务器端代码,两者都使用 Signal R 与客户端通信。
那么它们之间有什么区别呢?这些的客户端部署在哪里?他们与Server的连接有什么区别?如果 Web 应用程序依次调用第 3 方 Web API,则调用如何路由?
我发现的一个区别在于项目结构。 Blazor 服务器端只有一个项目(带有数据文件夹)。 Blazor WebAssembly Hosted 有 3 个项目(.Server、.Client 和 .Shared)。
I am new to Blazor and am trying to understand the differences between different hosting models.
From what I read I understand Blazor Server side and Blazor WebAssembly Hosted have server side code, both use Signal R to communicate with the Client.
So what is the difference between them? Where is the client side of these deployed to? What difference is in their connection with Server? If the Web App in turn calls a 3rd party web API how is the call routed?
One difference I found was in the project structure. Blazor Server side has only one project (with a Data Folder). Blazor WebAssembly Hosted has 3 projects (.Server, .Client and .Shared).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要区别在于 .NET 代码的运行位置:对于 Blazor Server,它 100% 在服务器端;而对于 Blazor Server,它是 100% 在服务器端。对于托管 Blazor WASM 应用程序,.NET 代码在服务器和客户端上运行(尽管服务器也可以运行您想要的任何其他语言或框架)。
确实如此,但它看起来不同。
未必。 Blazor Server 需要 Signal R 来不断地与客户端通信和更新,但 Blazor WASM 更加灵活。来自文档:
同样,Blazor WASM 对于服务器端来说是不可知的。托管模型为您生成服务器端,但从技术上讲您可以使用您想要的任何内容。
Blazor Server 不会编译客户端:一旦与应用程序建立连接,它就会利用 Signal R 通过 Web 套接字(或其他不可用的技术)持续将更新推送到客户端。
Blazor WASM 是客户端:当您编译 WASM 项目时,您将得到类似于针对 React 应用程序运行 Webpack 的内容。 Blazor WASM 是一种前端技术,因此它可以作为静态网页的依赖项,也可以通过 Web-API 进行增强和服务,就像托管模型一样。
同样,Blazor Server 需要 Signal R,而 Blazor WASM 与技术无关:它可以使用 Signal R,但通常您需要的只是标准 HTTP 协议。
这是一个完全不同的问题,但我可以看到其中的困惑。您的 WebAPI 是一个完全独立的应用程序;如果您的 WASM 应用程序发出外部请求,那么它也不会变得更明智。
这些文档提供了以下见解(请注意,这并不区分 WASM 的两种模型,但它仍然适用):
与 Blazor 服务器相比:
The primary difference is where your .NET code is running: with Blazor Server, it's 100% server-side; with a hosted Blazor WASM application, .NET code is running on both the server and the client (though the server can run any other language or framework you want too).
True, but it looks different.
Not necessarily. Blazor Server needs Signal R to continuously communicate and update the client, but Blazor WASM is more flexible. From the docs:
Again, Blazor WASM is agnostic towards your server-side. The hosted model generates a server-side for you, but you could technically use whatever you want.
Blazor Server doesn't compile a client-side per-say: once a connection is made to the application, it leverages Signal R to continuously push updates to the client over a web socket (or other technology when that is not available).
Blazor WASM is the client side: when you compile a WASM project, you are getting something similar to running Webpack against a React application. Blazor WASM is a front-end technology, so it can be served as a dependency of a static web page, or it can be augmented and served by a web-api, like with the hosted model.
Again, Blazor Server requires Signal R, whereas Blazor WASM is technology agnostic: it can be made to use Signal R, but often all you will need is the standard HTTP protocol.
This is an entirely different question, but I can see the confusion. Your WebAPI is a totally separate application; your WASM application is none the wiser if it makes external requests.
The docs offer the following insights (note this does not distinguish the two models for WASM, but it still applies):
Versus Blazor Server:
Blazor Web Assembly 应用程序完全在客户端上运行。没有与服务器的 SignalR 连接:检查服务器
program
文件。服务器只是加载页面和 WASM 代码的托管平台,并提供应用程序使用的任何 API 控制器。这是一个标准的 DotNetCore Web 应用程序。对第三方的任何 API 调用都会直接从运行 Web Assembly 应用程序的浏览器发送到第三方 URL。
请参阅我为另一个类似的 Stack Overflow 问题编写的要点,但我现在找不到。它描述了 Web Assembly 托管模板中的各个部分。 https://gist.github.com/ShaunCurtis/0ed8d257dff4d8497b97c88e5b2b30d0
A Blazor Web Assembly application runs wholly on the Client. There is no SignalR connection to the server: check the server
program
file. The server is simply the hosting platform for the loading page and WASM code, and provides any API controllers that the application uses. It's a standard DotNetCore web application.Any API calls to third parties go directly from the browser running the Web Assembly application to the third party Urls.
See this gist I wrote for another similar Stack Overflow question that I now can't find. It describes what the various bits are in the Web Assembly hosted template. https://gist.github.com/ShaunCurtis/0ed8d257dff4d8497b97c88e5b2b30d0