You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.1 KiB
C#

10 months ago
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SlnMesnac.Config;
using SlnMesnac.Common;
using SlnMesnac.Quartz;
using SlnMesnac.Repository;
using SlnMesnac.Plc;
using SlnMesnac.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
10 months ago
using System.Text;
using System.Threading.Tasks;
using Autofac;
10 months ago
using Microsoft.Extensions.Configuration;
using SlnMesnac.Mqtt;
using SlnMesnac.Rfid;
using SlnMesnac.TouchSocket;
using TouchSocket.Sockets;
10 months ago
namespace SlnMesnac.WPF
{
/// <summary>
///
/// </summary>
public class Startup
{
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
[Obsolete]
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//注册AppConfig
10 months ago
services.AddSingleton<AppConfig>(provider =>
{
var configuration = provider.GetService<IConfiguration>();
return configuration.GetSection("AppConfig").Get<AppConfig>();
});
10 months ago
//注册ORM
10 months ago
services.AddSqlSugarSetup();
}
/// <summary>
/// AutoFac自动注入
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
//注入Repository
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(Repository<>));
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>
/// 自动注入接口实现
/// </summary>
/// <param name="builder"></param>
/// <param name="assembly"></param>
private static void RegisterImplementations(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();
foreach (var @interface in interfaces)
{
builder.RegisterType(type).As(@interface);
}
}
}
10 months ago
/// <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);
}
}
}
10 months ago
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//启用Serilog中间件
app.UseSerilogExtensions();
//初始化PLC中间件
app.UsePlcExtensions();
//初始化RFID中间件
app.UseRfidExtensions();
10 months ago
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}