change - 完善AutoFac自动注入

pull/2/head
wenjy 9 months ago
parent 499167057f
commit 03e8d64680

@ -1,15 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace SlnMesnac.Common
{
public static class CommonSetup
{
public static void AddCommonSetup(this IServiceCollection services)
{
services.AddSingleton<StringChange>();
}
}
}

@ -1,15 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace SlnMesnac.Mqtt
{
public static class MqttSetup
{
public static void AddMqttSetup(this IServiceCollection services)
{
services.AddSingleton<MqttClient>();
}
}
}

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Builder;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Plc.Factory;
@ -6,22 +8,12 @@ namespace SlnMesnac.Plc
{
public static class PlcSetup
{
public static void AddPlcSetup(this IServiceCollection services)
{
services.AddSingleton<PlcPool>();
//services.AddSingleton<PlcAbsractFactory>();
services.AddSingleton<InovanceFactory>();
services.AddSingleton<MelsecBinaryFactory>();
services.AddSingleton<OmronNJFactory>();
services.AddSingleton<SiemensFactory>();
}
public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app)
{
var plcPool = app.ApplicationServices.GetService<PlcPool>();
plcPool.Init();
return app;
}
}
}

@ -6,13 +6,6 @@ namespace SlnMesnac.Rfid
{
public static class RfidSetup
{
public static void AddRfidSetup(this IServiceCollection services)
{
services.AddSingleton<RfidPool>();
services.AddSingleton<RflyFactory>();
}
public static IApplicationBuilder UseRfidExtensions(this IApplicationBuilder app)
{
var pool = app.ApplicationServices.GetService<RfidPool>();

@ -12,11 +12,6 @@ namespace SlnMesnac.TouchSocket
/// </summary>
public static class TouchSocketSetup
{
public static void AddTouchSocketSetup(this IServiceCollection services)
{
services.AddSingleton<TcpService>();//注册TouchSocket的服务
services.AddSingleton<TcpServer>();
}
public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app)
{

@ -17,6 +17,11 @@ using SlnMesnac.Common;
using SlnMesnac.Repository.service;
using SlnMesnac.Repository.service.Impl;
using SlnMesnac.Plc.Factory;
using SlnMesnac.Rfid.Factory;
using SlnMesnac.Rfid;
using SlnMesnac.TouchSocket;
using SlnMesnac.Mqtt;
using SlnMesnac.Business;
namespace SlnMesnac.WPF
{
@ -46,7 +51,6 @@ namespace SlnMesnac.WPF
var host = CreateHostBuilder(e.Args).Build();//生成宿主。
ServiceProvider = host.Services;
//var baseMaterialService = ServiceProvider.GetRequiredService<MelsecBinaryFactory>();
var appConfig = host.Services.GetService<AppConfig>();
var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/";

@ -19,6 +19,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlnMesnac.Business\SlnMesnac.Business.csproj" />
<ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" />
<ProjectReference Include="..\SlnMesnac.Config\SlnMesnac.Config.csproj" />
<ProjectReference Include="..\SlnMesnac.Model\SlnMesnac.Model.csproj" />

@ -19,6 +19,7 @@ using Microsoft.Extensions.Configuration;
using SlnMesnac.Mqtt;
using SlnMesnac.Rfid;
using SlnMesnac.TouchSocket;
using TouchSocket.Sockets;
namespace SlnMesnac.WPF
{
@ -44,32 +45,38 @@ namespace SlnMesnac.WPF
});
//注册通用类
services.AddCommonSetup();
//注册ORM
services.AddSqlSugarSetup();
//注册Quartz
//services.AddQuartzSetUp();
//注册PLC
services.AddPlcSetup();
//注册MQTT
services.AddMqttSetup();
//注册TouchSocket
services.AddTouchSocketSetup();
//注册RFID
services.AddRfidSetup();
}
/// <summary>
/// AutoFac自动注入
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
//注入Repository
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(Repository<>));
RegisterImplementations(builder, typeof(Repository<>).Assembly);
RegisterImplementations(builder, Assembly.LoadFrom("SlnMesnac.Repository.dll"));
//注入Plc
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Plc.dll"));
//注入Rfid
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Rfid.dll"));
//注入通用类
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Common.dll"));
//注入MQTT
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Mqtt.dll"));
//注入TouchSocket
builder.RegisterType(typeof(TcpService));
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.TouchSocket.dll"));
//注入业务类
RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Business.dll"));
}
/// <summary>
@ -95,6 +102,38 @@ namespace SlnMesnac.WPF
}
}
/// <summary>
/// 自动注入自定义类、抽象类
/// </summary>
/// <param name="builder"></param>
/// <param name="assembly"></param>
private static void RegisterType(ContainerBuilder builder, Assembly assembly)
{
var types = assembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericType)
.ToList();
foreach (var type in types)
{
var interfaces = type.GetInterfaces();
var baseType = type.BaseType;
#region 只注入抽象类 Delete By wenjy 2024-03-27
//if (baseType != null && baseType.IsAbstract && baseType == typeof(PlcAbsractFactory))
//{
// builder.RegisterType(type);
//}
#endregion
if (!typeof(Delegate).IsAssignableFrom(type)) //不注入委托事件
{
builder.RegisterType(type);
}
}
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>

@ -72,8 +72,6 @@ namespace SlnMesnac
});
//注册通用类
services.AddCommonSetup();
//注册SqlSugar
services.AddSqlSugarSetup();
@ -84,8 +82,6 @@ namespace SlnMesnac
//注册任务调度
services.AddQuartzSetUp();
//注册PLC
services.AddPlcSetup();
}
/// <summary>

Loading…
Cancel
Save