You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.2 KiB
69 lines
2.2 KiB
3 years ago
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
using Volo.Abp.DependencyInjection;
|
||
|
using Volo.Abp.Threading;
|
||
|
|
||
|
namespace Sanhe.Abp.Notifications;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 通知发布提供者抽象类
|
||
|
/// </summary>
|
||
|
public abstract class NotificationPublishProvider : INotificationPublishProvider, ITransientDependency
|
||
|
{
|
||
|
public abstract string Name { get; }
|
||
|
|
||
|
protected IServiceProvider ServiceProvider { get; }
|
||
|
|
||
|
protected readonly object ServiceProviderLock = new();
|
||
|
|
||
|
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
|
||
|
private ILoggerFactory _loggerFactory;
|
||
|
|
||
|
protected ILogger Logger => LazyLogger.Value;
|
||
|
private Lazy<ILogger> LazyLogger => new(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
|
||
|
|
||
|
|
||
|
protected TService LazyGetRequiredService<TService>(ref TService reference)
|
||
|
{
|
||
|
if (reference == null)
|
||
|
{
|
||
|
lock (ServiceProviderLock)
|
||
|
{
|
||
|
if (reference == null)
|
||
|
{
|
||
|
reference = ServiceProvider.GetRequiredService<TService>();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return reference;
|
||
|
}
|
||
|
|
||
|
public ICancellationTokenProvider CancellationTokenProvider { get; set; }
|
||
|
|
||
|
protected NotificationPublishProvider(IServiceProvider serviceProvider)
|
||
|
{
|
||
|
ServiceProvider = serviceProvider;
|
||
|
CancellationTokenProvider = NullCancellationTokenProvider.Instance;
|
||
|
}
|
||
|
|
||
|
public async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers)
|
||
|
{
|
||
|
await PublishAsync(notification, identifiers, CancellationTokenProvider.Token);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 重写实现通知发布
|
||
|
/// </summary>
|
||
|
/// <param name="notification"></param>
|
||
|
/// <param name="identifiers"></param>
|
||
|
/// <param name="cancellationToken"></param>
|
||
|
/// <returns></returns>
|
||
|
protected abstract Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers, CancellationToken cancellationToken = default);
|
||
|
}
|