- 在后台业务服务类中使用
INotifyService发送通知
[WebApi, Service] // 构造函数注入INotifyService
class HomeService(Context context, INotifyService service) : ServiceBase(context)
{
public Task DoXXXXAsync()
{
// 往母版页发送系统通知
service.LayoutNotifyAsync("系统通知", "XXX服务已启动!");
}
}
KConsole组件使用示例
public class AppConstant
{
public const string AddLog = "AddLog"; // 定义SignalR方法名
}
class TestWorker(INotifyService service) : BackgroundService
{
private readonly TimeSpan _interval = TimeSpan.FromSeconds(1);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// 每秒往BizId为Test的控制台组件发送日志
await service.SendAsync(AppConstant.AddLog, new ConsoleLogInfo
{
BizId = "Test",
Type = ConsoleLogType.Info,
Content = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 测试日志信息"
}, stoppingToken);
await Task.Delay(_interval, stoppingToken);
}
}
}
- 在后端
AppServer中注册TestWorker
static class AppServer
{
internal static void AddApplicationWeb(this IServiceCollection services, Action<CoreOption> action)
{
services.AddHostedService<TestWorker>(); // 注册TestWorker
}
}
<KButton Type="@ButtonType.Primary" Name="日志" OnClick="OnLog" />
@code {
// 弹窗显示控制台日志组件,实时显示TestWorker发送的日志
private void OnLog()
{
var model = new DialogModel
{
Title = "控制台日志",
Width = 600,
Maximizable = true,
Content = b => b.Component<KConsole>()
.Set(c => c.BizId, "Test") // BizId:日志业务ID
.Set(c => c.MethodName, AppConstant.AddLog) // MethodName:SignalR方法名
.Build()
};
UI.ShowDialog(model);
}
}