首先我们需要创建一个MasaFramework
模板的项目,项目名称TokenDemo
,项目类型如图所示
删除Web/TokenDemo.Admin
项目,新建Masa Blazor Pro
项目模板
项目,项目位置在src/Web
项目
项目类型选择ServerAndWasm
,为了让我们支持俩种模式
创建完成以后的目录,然后在TokenDemo.Admin
中添加项目引用TokenDemo.Caller
配置EntityFrameworkCore
和Sqlite
修改TokenDemo.Service
项目的包依赖为预览版
Masa.Contrib.Data.Contracts
提供了数据过滤的能力, 但它不是必须的
然后会出现报错
LogMiddleware
将代码修改为以下代码
namespace TokenDemo.Service.Infrastructure.Middleware;
public class LogMiddleware : EventMiddleware
where TEvent : notnull, IEvent
{
private readonly ILogger> _logger;
public LogMiddleware(ILogger> logger)
{
_logger = logger;
}
public override async Task HandleAsync(TEvent action, EventHandlerDelegate next)
{
var typeName = action.GetType().FullName;
_logger.LogInformation("----- command {CommandType}", typeName);
await next();
}
}
ValidatorMiddleware
将代码修改为以下代码
namespace TokenDemo.Service.Infrastructure.Middleware;
public class ValidatorMiddleware : EventMiddleware
where TEvent : notnull, IEvent
{
private readonly ILogger> _logger;
private readonly IEnumerable> _validators;
public ValidatorMiddleware(IEnumerable> validators, ILogger> logger)
{
_validators = validators;
_logger = logger;
}
public override async Task HandleAsync(TEvent action, EventHandlerDelegate next)
{
var typeName = action.GetType().FullName;
_logger.LogInformation("----- Validating command {CommandType}", typeName);
var failures = _validators
.Select(v => v.Validate(action))
.SelectMany(result => result.Errors)
.Where(error => error != null)
.ToList();
if (failures.Any())
{
_logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, action, failures);
throw new ValidationException("Validation exception", failures);
}
await next();
}
}
OrderEventHandler
将代码修改为以下代码
namespace TokenDemo.Service.Infrastructure.Handlers;
public class OrderEventHandler
{
readonly IOrderRepository _orderRepository;
public OrderEventHandler(IOrderRepository orderRepository)
{
_orderRepository = orderRepository;
}
[EventHandler(Order = 1)]
public async Task HandleAsync(QueryOrderListEvent @event)
{
@event.Orders = await _orderRepository.GetListAsync();
}
}
public class OrderEventAfterHandler : IEventHandler
{
public async Task HandleAsync(QueryOrderListEvent @event, CancellationToken cancellationToken = new CancellationToken())
{
await Task.CompletedTask;
}
}
修改appsettings.json
添加Sqlite
地址
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=Catalog.db;"
}
}
修改Program.cs
代码
using TokenDemo.Service.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddAuthorization()
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = "";
options.RequireHttpsMetadata = false;
options.Audience = "";
});
builder.Services.AddMasaDbContext(dbContextBuilder =>
{
dbContextBuilder
.UseSqlite() //使用Sqlite数据库
.UseFilter(); //数据数据过滤
});
builder.Services.AddAutoInject();
var app = builder.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme. rnrn Enter 'Bearer' [space] and then your token in the text input below.rnrnExample: "Bearer xxxxxxxxxxxxxxx"",
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
})
.AddFluentValidationAutoValidation().AddFluentValidationClientsideAdapters()
.AddEventBus(eventBusBuilder =>
{
eventBusBuilder.UseMiddleware(typeof(ValidatorMiddleware));
eventBusBuilder.UseMiddleware(typeof(LogMiddleware));
})
.AddServices(builder);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.Run();
添加EFCore迁移依赖
all
runtime; build; native; contentfiles; analyzers; buildtransitive
all
runtime; build; native; contentfiles; analyzers; buildtransitive
在程序包管理器控制台
中输入 Add-Migration Init
生成Init
的迁移文件(如果出现 error NETSDK1082: Microsoft.AspNetCore.App 没有运行时包可用于指定的 RuntimeIdentifier“browser-wasm”
这个错误的话就先把TokenDemo.Admin.WebAssembly
项目移除)
使用Update-Database
生成Sqlitem
然后就可以看到项目中生成了Catalog.db
文件
启动TokenDemo.Service.Order
我们就可以看到Swagger的界面了
如何对接接口
打开TokenDemo.Caller
项目中的“CallersOrderCaller.cs`文件
修改BaseAdderss
为TokenDemo.Service.Order
的服务地址
打开TokenDemo.Service.Order
项目的ServicesOrderService.cs
文件并且修改代码
namespace TokenDemo.Service.Services;
public class OrderService : ServiceBase
{
public OrderService(IServiceCollection services) : base(services)
{
App.MapGet("/order/list", QueryList).Produces>()
.WithName("GetOrders");
}
public async Task QueryList(IEventBus eventBus)
{
var orderQueryEvent = new QueryOrderListEvent();
await eventBus.PublishAsync(orderQueryEvent);
return Results.Ok(orderQueryEvent.Orders);
}
}
然后在通过命令行启动TokenDemo.Service.Order
项目
打开TokenDemoAdmin
项目的PagesHomeIndex.razor
文件并且修改代码
@page "/"
@using TokenDemo.Caller.Callers
@inherits LayoutComponentBase
@inject NavigationManager Nav
@inject OrderCaller OrderCaller
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
Nav.NavigateTo(GlobalVariables.DefaultRoute,true);
var data = await OrderCaller.GetListAsync();
await base.OnAfterRenderAsync(firstRender);
}
}
并且在await base.OnAfterRenderAsync(firstRender);
这里打一个断点用于查看是否获取到消息
打开TokenDemo.Admin.Server
项目的Program.cs
,添加以下代码
builder.Services.AddCaller(typeof(TokenDemo.Caller.Callers.OrderCaller).Assembly);
然后启动TokenDemo.Admin.Server
项目,进入断点
得到结果
结尾
通过上文我们可以基本将MasaFramework
的使用掌握,前端和后端的接口也掌握了
当前是MasaFramework
的第三篇入门,我会继续学习MasaFramework
并且分享给大家
来自token的分享
MASA Framework
学习交流:737776595
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net