为什么PushServiceClient在BackgroundService内部引发异常?

我有一个后台服务,可以发送推送通知。一切正常,除了最后一种方法。

await pushClient.RequestPushMessageDeliveryAsync(subscription,notification,stoppingToken);

它引发异常

  

对象引用未设置为对象的实例

     在

  Lib.Net.Http.WebPush.Internals.UrlBase64Converter.FromUrlBase64String(String   输入)   Lib.Net.Http.WebPush.PushServiceclient.GetKeyingMaterial(PushSubscription   订阅,AsymmetricKeyParameter applicationServerPrivateKey,   字节[] applicationServerPublicKey)   Lib.Net.Http.WebPush.PushServiceclient.SetContent(HttpRequestMessage   pushMessageDeliveryRequest,PushSubscription订阅,PushMessage   讯息)   Lib.Net.Http.WebPush.PushServiceclient.PreparePushMessageDeliveryRequest(PushSubscription   订阅,PushMessage消息,VapidAuthentication身份验证,   VapidAuthenticationScheme authenticationScheme)位于   Lib.Net.Http.WebPush.PushServiceclient.d__36.MoveNext()   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  在   System.Runtime.CompilerServices.Taskawaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)

我认为这可能是因为我错误地实例化了PushServiceclient。如果是这样,怎么做对呢?

后台服务的完整类:

public class PushNotificationsProducer : BackgroundService
{
    private const int NOTIFICATION_FREQUENCY = 10 * 1000;
    public IOptions<PushNotificationsOptions> Options { get; }
    public IServiceProvider Services { get; }

    public PushNotificationsProducer(IOptions<PushNotificationsOptions> options,IServiceProvider services)
    {
        Options = options;
        Services = services;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            await Task.Delay(NOTIFICATION_FREQUENCY,stoppingToken);

            await SendNotifications(stoppingToken);
        }
    }

    private async Task SendNotifications(CancellationToken stoppingToken)
    {
        List<User> users;
        ApplicationDbContext db;
        using (var scope = Services.CreateScope())
        {
            db = scope.ServiceProvider
                    .GetRequiredService<ApplicationDbContext>();

            users = await db.Users.ToListAsync(stoppingToken);

            foreach (var user in users)
            {

                    var subscription = await db.AppPushSubscriptions.Where(x => x.UserId == user.Id).FirstOrDefaultAsync(stoppingToken);

                    // create notif
                    PushMessage notification = new AngularPushNotification
                    {
                        Title = "Some text!",Body = $"Some text.",Icon = "assets/icons/icon-96x96.png"
                    }.ToPushMessage();


                    PushServiceclient pushClient;
                    pushClient = scope.ServiceProvider
                            .GetRequiredService<PushServiceclient>();

                    pushClient.DefaultAuthentication = new VapidAuthentication(Options.Value.PublicKey,Options.Value.PrivateKey)
                    {
                        Subject = "https://example.net"
                    };

                    try
                    {
                        await pushClient.RequestPushMessageDeliveryAsync(subscription,stoppingToken);
                    }
                    catch (Exception e)
                    {
                        throw;
                    }           
            }
        }
    }
}
TJJ123456 回答:为什么PushServiceClient在BackgroundService内部引发异常?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3142105.html

大家都在问