using AmrControl; using AmrControl.ADS; using AmrControl.Clients; using AmrControl.Common; using AmrControl.DB; using AmrControl.JGR; using AmrControl.mq; using AmrControl.services; using AmrControl.workstation; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using System.Runtime.InteropServices; using System.Text; var builder = WebApplication.CreateBuilder(args); var configuration = builder.Configuration; // Add services to the container. //builder.Services.AddRazorPages(); //添加signalR服务 //builder.Services.AddSignalR(); //添加控制器,并使用newtonsoftjson作为json序列化与反序列化工具 builder.Services.AddControllers().AddNewtonsoftJson(); builder.Services.AddControllers(); var apphelper = new AppHelper(builder.Configuration); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //AddSingleton :每次都是相同的实例 //builder.Services.AddSingleton(); //builder.Services.AddSingleton(); //var mqHost = builder.Configuration["mq:host"]; //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost)) //{ // builder.Services.AddSingleton(); //} //builder.Services.AddSingleton(); //builder.Services.AddSingleton(typeof(MyDbHandler), sp => { // //return new MyDbHandler(builder.Configuration.GetConnectionString("MySql")); // return new MyDbHandler(builder.Configuration.GetConnectionString("Sqlite")); //}); //builder.Services.AddSingleton(typeof(IJGR), sp => { // var scope = sp.CreateScope(); // var hubContext = scope.ServiceProvider.GetRequiredService>(); // var context = scope.ServiceProvider.GetRequiredService(); // var taskExecManager = scope.ServiceProvider.GetRequiredService(); // var registrySerivce = scope.ServiceProvider.GetRequiredService(); // return new JGRManager(hubContext, context, builder.Configuration, taskExecManager, registrySerivce); //}); //添加工位的采集控制 //builder.Services.AddSingleton(); builder.Services.AddSingleton(typeof(MsWorkstation), sp => { return new MsWorkstation(configuration, sp); }); builder.Services.AddSingleton(typeof(MqttClient), sp => { return new MqttClient(configuration, sp); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } //app.UseHttpsRedirection(); app.UseAuthorization(); //自定义网页入口 DefaultFilesOptions options = new DefaultFilesOptions(); //options.DefaultFileNames.Clear(); //options.DefaultFileNames.Add("/index.html"); //app.UseDefaultFiles(options); //允许静态文件 //app.UseStaticFiles(); //app.UseStaticFiles(new StaticFileOptions //{ // FileProvider = new PhysicalFileProvider( // Path.Combine(AppContext.BaseDirectory, "files")), // RequestPath = "" //}); //app.MapRazorPages(); //app.MapHub("/chatHub"); app.MapControllers(); //var hubContext = app.Services.GetService>(); ////初始化小车控制 //var jgr = builder.Services.BuildServiceProvider().GetRequiredService(); //if (jgr != null) //{ // jgr.Init(app.Services); // jgr.SetHub(hubContext); //} //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost)) //{ // var rabbitmqService = builder.Services.BuildServiceProvider().GetRequiredService(); // if(rabbitmqService != null) // { // rabbitmqService.Init(); // } //} //初始化sqlite数据库 //var sqlite = builder.Services.BuildServiceProvider().GetRequiredService(); //if(sqlite != null) //{ // sqlite.InitSqlite(); //} //int size = Marshal.SizeOf(); //初始化工位的采集控制 IServiceProvider provider = builder.Services.BuildServiceProvider(); var mqtt = provider.GetService(); if (null != mqtt) { mqtt.CreateClient(); } var ws = provider.GetService(); if (null != ws) { ws.Stop(); } //程序退出事件 AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; //app 执行 app.Run(); void CurrentDomain_ProcessExit(object? sender, EventArgs e) { AmrManager.ProcessExit = true; //throw new NotImplementedException(); } //var builder = Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => //{ // webBuilder.UseUrls("http://*:8000") // .UseStartup() // .UseKestrel(opt => opt.Limits.MaxRequestBodySize = null); //}) //.UseDefaultServiceProvider(options => //{ // options.ValidateScopes = false; //}); ////程序退出事件 //AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; //void CurrentDomain_ProcessExit(object? sender, EventArgs e) //{ // AmrManager.ProcessExit = true; // //throw new NotImplementedException(); //} //builder.Build().Run();