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

98 lines
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.AddCorsAccessor();
  64. services.AddSqlsugarSetup(App.Configuration);
  65. services.AddControllers()
  66. .AddInjectWithUnifyResult();
  67. services.AddMvcFilter<RequestAuditFiltercs>();
  68. }
  69. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  70. {
  71. if (env.IsDevelopment())
  72. {
  73. app.UseDeveloperExceptionPage();
  74. }
  75. app.UseHttpsRedirection();
  76. app.UseRouting();
  77. app.UseCorsAccessor();
  78. app.UseAuthentication();
  79. app.UseAuthorization();
  80. app.UseInject(string.Empty);
  81. app.UseEndpoints(endpoints =>
  82. {
  83. endpoints.MapControllers();
  84. });
  85. }
  86. }
  87. }