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

ss2ws0br  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(229)

为什么我会得到_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. ForegroundServiceManager,其中包含Start()方法。

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. Subscriber Service在async Task中做db操作和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();
    }
yws3nbqq

yws3nbqq1#

我搬家了

_subscriberService.Subscribe();

到ForegroundServiceManager与相关的DI和Boom!它工作得很顺利。
我也从IoTHubService中删除了不必要的DI。

相关问题