-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSmsServiceExtensions.cs
62 lines (54 loc) · 1.87 KB
/
SmsServiceExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sugar.SMS
{
public static class SmsServiceExtensions
{
/// <summary>
/// 注册短信服务单例
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection AddSmsProvider(this IServiceCollection services, SmsOptions options)
{
if (options == null)
{
throw new ArgumentException(nameof(options));
}
//添加单例
services.AddSingleton(SmsFactory.Create(options));
return services;
}
/// <summary>
/// 注册验证码单例
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection AddSmsCodeProvider(this IServiceCollection services, SmsCodeOptions options)
{
if (options == null)
{
throw new ArgumentException(nameof(options));
}
var smsProvider = services
.FirstOrDefault(d => d.ServiceType == typeof(SmsProvider))
?.ImplementationInstance as SmsProvider;
if (smsProvider == null)
{
throw new InvalidOperationException("\"SmsCodeProvider\" must be after \"SmsProvider\".");
}
//添加单例
services.AddSingleton(SmsFactory.Create(smsProvider, options));
return services;
}
public static IServiceCollection AddSmsCodeProvider(this IServiceCollection services)
{
return services.AddSmsCodeProvider(new SmsCodeOptions());
}
}
}