.NET Maui Blazor 中的前台服务实现 - Android 应用

Foreground Service implementation in .NET Maui Blazor - Android app

提问人:N. OZER SENOL 提问时间:6/16/2023 最后编辑:Peter MortensenN. OZER SENOL 更新时间:6/19/2023 访问量:196

问:

为什么我得到 _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)
        {
            // Create a notification for the foreground service
            CreateNotificationChannel();
            var notification = CreateNotification();
            StartForeground(10001, notification);

            // Your service logic here
            // IOTHUB Listener Burda Baslar

            _subscriberService.Subscribe();

            return StartCommandResult.Sticky;
        }


        private void CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }

            var channelName = "PROXIWASH Servis";
            var channelDescription = "PROWIWASH Servis Kanali";
            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 Servis")
                .SetContentText("Servis Calisiyor")
                .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
            {
                // Get the ISubscriberService instance from the dependency injection container
                ISubscriberService subscriberService = _serviceProvider.GetRequiredService<ISubscriberService>();

                // Pass the subscriberService instance to the IoTHubService constructor
                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)
            {
                // Handle the exception
            }

        }
    }

3. 在 MauiProgram.cs 中注册服务

builder.Services.AddSingleton<IoTHubService>();
builder.Services.AddSingleton<IForegroundServiceManager,ForegroundServiceManager>();
builder.Services.AddSingleton<ISubscriberService, SubscriberService>();
builder.Services.AddSingleton<IOperasyonService, OperasyonService>();

4. 订阅者服务在异步任务中执行数据库操作和 MQTT 操作


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()
        {
            // doing some work with DB and MQTT server...

         return Task.CompletedTask;

        }
    }

5. 在 Razor 组件中注入 IForegroundService 以调用 Start()。

@inject IForegroundServiceManager _foregroundService

....
protected override void OnInitialized()
    {
       _foregroundService.Start();
    }
Android 异常 null foreground-service maui-blazor

评论


答:

0赞 N. OZER SENOL 6/16/2023 #1

我搬家了

_subscriberService.Subscribe(); 

到 ForegroundServiceManager 以及相关的 DI 和 Boom!它工作得很顺利。

我也从 IoTHubService 中删除了不必要的 DI 内容。