基础服务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.
 
 

97 lines
3.4 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. }
  68. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  69. {
  70. if (env.IsDevelopment())
  71. {
  72. app.UseDeveloperExceptionPage();
  73. }
  74. app.UseHttpsRedirection();
  75. app.UseRouting();
  76. app.UseCorsAccessor();
  77. app.UseAuthentication();
  78. app.UseAuthorization();
  79. app.UseInject(string.Empty);
  80. app.UseEndpoints(endpoints =>
  81. {
  82. endpoints.MapControllers();
  83. });
  84. }
  85. }
  86. }