-
Notifications
You must be signed in to change notification settings - Fork 1
/
AspNetCoreExtensions.cs
43 lines (33 loc) · 1.63 KB
/
AspNetCoreExtensions.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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SmartIOT.Connector.Core;
using SmartIOT.Connector.Core.Factory;
namespace SmartIOT.Connector.DependencyInjection;
public static class AspNetCoreExtensions
{
public static IServiceCollection AddSmartIOTConnector(this IServiceCollection services, Action<SmartIotConnectorBuilder> configure)
{
var builder = new SmartIotConnectorBuilder();
configure?.Invoke(builder);
ArgumentNullException.ThrowIfNull(builder.Configuration);
// add main stuffs
services.AddSingleton<SmartIotConnectorBuilder>(builder);
services.AddSingleton<SmartIotConnectorConfiguration>(builder.Configuration);
services.AddSingleton<SmartIotConnector>(builder.Build);
// expose more things on DI
services.AddSingleton<IConnectorFactory>(_ => builder.ConnectorFactory);
services.AddSingleton<IDeviceDriverFactory>(_ => builder.DeviceDriverFactory);
services.AddSingleton<ISchedulerFactory>(_ => builder.SchedulerFactory);
services.AddSingleton<ITimeService>(_ => builder.TimeService);
// add hosted service
services.AddSingleton<SmartIotConnectorHostedService>();
services.AddHostedService(sp => sp.GetRequiredService<SmartIotConnectorHostedService>());
return services;
}
public static IApplicationBuilder UseSmartIOTConnector(this IApplicationBuilder app, Action<SmartIotConnector> configure)
{
var connector = app.ApplicationServices.GetRequiredService<SmartIotConnector>();
configure.Invoke(connector);
return app;
}
}