.NET MAUI Blazor Android前台服务:_subscriberService.Subscribe()始终返回null
问题描述
在.NET MAUI Blazor Android应用中实现前台服务时,调用_subscriberService.Subscribe()始终返回null,排查所有代码后仍未定位问题。相关实现代码如下:
1. 创建服务
[Service] public class IoTHubService : Service { private readonly ISubscriberService _subscriberService; public IoTHubService(ISubscriberService subscriberService) { _subscriberService = subscriberService; } public IoTHubService() { } public override IBinder OnBind(Intent intent) { return null; } public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { // 创建前台服务通知 CreateNotificationChannel(); var notification = CreateNotification(); StartForeground(10001, notification); // 服务逻辑开始 // IOTHUB 监听器从此处启动 _subscriberService.Subscribe(); return StartCommandResult.Sticky; } private void CreateNotificationChannel() { if (Build.VERSION.SdkInt < BuildVersionCodes.O) { return; } var channelName = "PROXIWASH 服务"; var channelDescription = "PROWIWASH 服务通道"; var channel = new NotificationChannel("10000", channelName, NotificationImportance.Default) { Description = channelDescription }; var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); } private Notification CreateNotification() { var notificationBuilder = new NotificationCompat.Builder(this, "10000") .SetContentTitle("PROXIWASH 服务") .SetContentText("服务运行中") .SetSmallIcon(Resource.Drawable.caricon24x) .SetOngoing(true); return notificationBuilder.Build(); } }
2. 包含Start()方法的ForegroundServiceManager
public class ForegroundServiceManager : IForegroundServiceManager { private readonly IServiceProvider _serviceProvider; public ForegroundServiceManager(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public void Start() { try { // 从依赖注入容器获取ISubscriberService实例 ISubscriberService subscriberService = _serviceProvider.GetRequiredService<ISubscriberService>(); // 将subscriberService实例传递给IoTHubService构造函数 IoTHubService ioTHubService = new IoTHubService(subscriberService); var intent = new Intent(MauiApplication.Current.ApplicationContext, typeof(IoTHubService)); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { MauiApplication.Current.ApplicationContext.StartForegroundService(intent); } else { MauiApplication.Current.ApplicationContext.StartService(intent); } } catch (Exception ex) { // 处理异常 } } }
3. 在MauiProgram.cs中注册服务
builder.Services.AddSingleton<IoTHubService>(); builder.Services.AddSingleton<IForegroundServiceManager,ForegroundServiceManager>(); builder.Services.AddSingleton<ISubscriberService, SubscriberService>(); builder.Services.AddSingleton<IOperasyonService, OperasyonService>();
4. 执行数据库与MQTT异步操作的Subscriber Service
public class SubscriberService : ISubscriberService { private MqttFactory _mqttFactory; private IMqttClient _client; private IOperasyonService _operasyonService; public SubscriberService(IOperasyonService operasyonService) { _operasyonService = operasyonService; _mqttFactory = new MqttFactory(); } public async Task Subscribe() { // 执行数据库和MQTT服务器相关操作... return Task.CompletedTask; } }
5. 在Razor组件中注入IForegroundService并调用Start()
@inject IForegroundServiceManager _foregroundService .... protected override void OnInitialized() { _foregroundService.Start(); }
内容的提问来源于stack exchange,提问作者N. OZER SENOL
相关产品推荐
相关产品推荐

