基础服务api
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

99 lignes
3.5 KiB

  1. using BPA.MQTTClient;
  2. using BPA.SAAS.Manage.Application.System.Dtos;
  3. using BPA.SAAS.Manage.Core;
  4. using FluentValidation.AspNetCore;
  5. using Furion;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using MQTTnet.Client.Connecting;
  11. using MQTTnet.Client.Disconnecting;
  12. using Newtonsoft.Json;
  13. using System;
  14. using System.Threading.Tasks;
  15. namespace BPA.SAAS.Manage.Web.Core
  16. {
  17. public class Startup : AppStartup
  18. {
  19. public void ConfigureServices(IServiceCollection services)
  20. {
  21. services.AddConsoleFormatter();
  22. services.AddJwt<JwtHandler>();
  23. CosConfig.cosInfoOptions = new CosInfoOptions(App.Configuration["cos_config:AppId"],
  24. App.Configuration["cos_config:Region"], App.Configuration["cos_config:Bucket"],
  25. App.Configuration["cos_config:SecretId"], App.Configuration["cos_config:SecretKey"]);
  26. services.AddControllersWithViews()
  27. .AddFluentValidation(fv =>
  28. {
  29. fv.RegisterValidatorsFromAssemblies(App.Assemblies);
  30. })
  31. .AddNewtonsoftJson(options =>
  32. {
  33. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  34. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  35. });
  36. services.AddMqttClientHostedService(op =>
  37. {
  38. op.Port = int.Parse(App.Configuration["dev1_brokerHostSettings:Port"]);
  39. op.Server = App.Configuration["dev1_brokerHostSettings:Host"];
  40. op.UserName = App.Configuration["MqttClientSettings:UserName"];
  41. op.Password = App.Configuration["MqttClientSettings:Password"];
  42. op.mqttClientConnectedHandlerDelegate = new MqttClientConnectedHandlerDelegate(async e =>
  43. {
  44. Console.WriteLine("MQTT连接成功");
  45. });
  46. op.mqttClientDisconnectedHandlerDelegate = new MqttClientDisconnectedHandlerDelegate(async e =>
  47. {
  48. Console.WriteLine("MQTT断开连接");
  49. await Task.Delay(TimeSpan.FromSeconds(5));
  50. try
  51. {
  52. //var options = op.Server.GetService<IMqttClientOptions>();
  53. //await op.Server.GetService<IMqttClient>().ConnectAsync(options);
  54. }
  55. catch (global::System.Exception)
  56. {
  57. }
  58. });
  59. op.MqttApplicationMessageReceivedHandler = new MQTTnet.Client.Receiving.MqttApplicationMessageReceivedHandlerDelegate(async e =>
  60. {
  61. });
  62. });
  63. services.AddRemoteRequest();
  64. services.AddCorsAccessor();
  65. services.AddSqlsugarSetup(App.Configuration);
  66. services.AddControllers()
  67. .AddInjectWithUnifyResult();
  68. services.AddMvcFilter<RequestAuditFiltercs>();
  69. }
  70. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  71. {
  72. if (env.IsDevelopment())
  73. {
  74. app.UseDeveloperExceptionPage();
  75. }
  76. app.UseHttpsRedirection();
  77. app.UseRouting();
  78. app.UseCorsAccessor();
  79. app.UseAuthentication();
  80. app.UseAuthorization();
  81. app.UseInject(string.Empty);
  82. app.UseEndpoints(endpoints =>
  83. {
  84. endpoints.MapControllers();
  85. });
  86. }
  87. }
  88. }