Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/Notify/Features/Sms/Services/SmsService.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
namespace Notify.Features.Sms.Services;

public class SmsService(SmsDbContext dbContext, IServiceProvider serviceProvider)
public class SmsService(SmsDbContext dbContext, IServiceProvider serviceProvider , IEnumerable<ISmsProvider> smsProviders)
{
private readonly SmsDbContext _dbContext = dbContext;
private readonly IServiceProvider _serviceProvider = serviceProvider;

private readonly IEnumerable<ISmsProvider> _smsProviders = smsProviders;

public async Task SendAsync(Guid messageId, string mobile, string message, CancellationToken cancellationToken)
{

foreach (var providerName in SmsConfiguration.Providers)
foreach (var smsProvider in _smsProviders)
{
var provider = _serviceProvider.GetRequiredKeyedService<ISmsProvider>(providerName);

var referenceId = await provider.SendAsync(mobile, message, cancellationToken);
var referenceId = await smsProvider.SendAsync(mobile, message, cancellationToken);

if (string.IsNullOrEmpty(referenceId))
{
// try by sending with another provider
continue;
}

var smsTrace = SmsTrace.Create(mobile, message, messageId, referenceId, provider.Name);
var smsTrace = SmsTrace.Create(mobile, message, messageId, referenceId, smsProvider.Name);
await _dbContext.SmsTraces.AddAsync(smsTrace, cancellationToken);
await _dbContext.SaveChangesAsync(cancellationToken);

// end of try
break;
}

}

public async Task<SmsTraceStatus> InquiryAsync(SmsTrace message, CancellationToken cancellationToken = default)
{
var provider = _serviceProvider.GetRequiredKeyedService<ISmsProvider>(message.Provider);
var provider = _smsProviders.FirstOrDefault(a => a.Name == message.Provider);
if (provider == null)
throw new Exception($"sms provider {message.Provider} not found use correct sms provider");
return await provider.IquiryAsync(message.RefrenceId, cancellationToken);
}
}
15 changes: 12 additions & 3 deletions src/Notify/Features/Sms/SmsFeatureConfigure.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Notify.Features.Sms;
using System.Reflection;

namespace Notify.Features.Sms;

public static class SmsFeatureConfigure
{
Expand All @@ -7,8 +9,15 @@ public static IServiceCollection ConfigureSmsFeature(this IServiceCollection ser
services.AddHostedService<InquirySmsBackgroundService>();
services.AddScoped<SmsService>();

services.AddKeyedScoped<ISmsProvider, ProviderASmsProvider>("ProviderA");
services.AddKeyedScoped<ISmsProvider, ProviderBSmsProvider>("ProviderB");
// IoC of ISmsProvider
// This is dynamic DI based on ISmsProvider
var smsProviderType = typeof(ISmsProvider);
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => smsProviderType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
foreach (var type in types)
services.AddScoped(smsProviderType, type);
// end


var appSettings = configuration.Get<AppSettings>();

Expand Down