Blazor Web 应用如何实现Auto模式
创始人
2024-11-05 13:35:29

本文介绍Blazor Web应用Auto交互呈现模式的实现方案,如下示例是基于 Known 框架来实现的,该解决方案共有3个项目,具体实现步骤如下:

1. 前后端共用项目

  • 创建前后端共用类库项目Sample,定义系统的实体类、数据模型、服务接口、常量、枚举等,项目工程文件内容如下:
              net8.0         enable       
  • 在该项目中添加示例服务接口,继承框架IService
//IService为框架定义的Api服务接口,用于标识该接口为前后端交互接口 //程序启动时,框架自动查找Assembly中的接口,根据接口定义WebApi路由 //该示例路由为:/Test/GetMessage public interface ITestService : IService {     Task GetMessageAsync(); } 

2. 客户端项目

  • 创建客户端项目Sample.Client,引用WebAssembly所需依赖,引用Castle依赖动态代理Http请求后端WebApi,项目工程文件内容如下:
              net8.0         enable         true         Default                                                                       
  • 添加拦截器HttpClientInterceptor.cs类文件,继承Castle.DynamicProxy.IAsyncInterceptor,实现Http动态代理
using Castle.DynamicProxy;  namespace Sample.Client;  // HttpInterceptor为框架封装的拦截器 public class HttpClientInterceptor(IServiceScopeFactory provider) : HttpInterceptor(provider), IAsyncInterceptor where T : class {     protected override async Task CreateClientAsync() {         var type = typeof(T);         var factory = await ServiceFactory.CreateAsync();         var client = factory.CreateClient(type.Name);         client.BaseAddress = new Uri(Config.HostUrl);         return client;     }      public void InterceptAsynchronous(IInvocation invocation) {         invocation.ReturnValue = SendAsync(invocation.Method, invocation.Arguments);     }      public void InterceptAsynchronous(IInvocation invocation) {         invocation.ReturnValue = SendAsync(invocation.Method, invocation.Arguments);     }      public void InterceptSynchronous(IInvocation invocation) { } } 
  • Program.cs文件中添加客户端配置
//使用Castle代理生成器创建Http代理类型 private static readonly ProxyGenerator Generator = new();  services.AddHttpClient(); //添加KnownClient,注入拦截器提供者 services.AddKnownClient(info => {     info.InterceptorType = type => typeof(HttpClientInterceptor<>).MakeGenericType(type);     info.InterceptorProvider = (type, interceptor) =>     {         return Generator.CreateInterfaceProxyWithoutTarget(type, ((IAsyncInterceptor)interceptor).ToInterceptor());     }; }); 
  • 添加测试页面组件Test.razor
@page "/test"  

@message

@code { //注入服务与Server模式注入没有区别 [Inject] private ITestService Service { get; set; } private string message; protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (firstRender) message = await Service.GetMessageAsync(); //这里的Service实例,会根据渲染模式自动切换 //SSR时,就是后端实现ITestService的实现类的实例 //CSR时,就是Castle代理生成器创建的代理类的实例 } }

3. 服务端项目

  • 创建服务端项目Sample.Web,项目工程文件内容如下:
              net8.0         enable                                   
  • 修改App.razor文件中的呈现模式
  @code {     private InteractiveAutoRenderMode InteractiveMode => new(false); } 
  • 添加TestService.cs实现服务接口
class  TestService : ITestService {     public Task GetMessageAsync() => Task.FromResult("test"); } 
  • Program.cs文件中添加服务端配置
//添加Known框架后端Core services.AddKnownCore(); //添加Known框架自动生成WebApi services.AddKnownWebApi(); //注入服务接口 services.AddScoped();  //使用Known框架静态文件和WebApi app.UseKnown(); 

4. 结语

本文示例代码仅作Auto模式实现方案的参考,具体功能实现,可查看 Known 框架的实例源码。

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...