平安校园
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

MqttComponent.cs 1.3 KiB

2 ay önce
123456789101112131415161718192021222324252627282930313233
  1. namespace SafeCampus.Plugin.Mqtt
  2. {
  3. /// <summary>
  4. /// mqtt组件
  5. /// </summary>
  6. public sealed class MqttComponent : IServiceComponent
  7. {
  8. /// <summary>
  9. /// ConfigureServices中不能解析服务,比如App.GetService(),尤其是不能在ConfigureServices中获取诸如缓存等数据进行初始化,应该在Configure中进行
  10. /// 服务都还没初始化完成,会导致内存中存在多份 IOC 容器!!
  11. /// 正确应该在 Configure 中,这个时候服务(IServiceCollection 已经完成 BuildServiceProvider() 操作了
  12. /// </summary>
  13. /// <param name="services"></param>
  14. /// <param name="componentContext"></param>
  15. public void Load(IServiceCollection services, ComponentContext componentContext)
  16. {
  17. Console.WriteLine("注册Mqtt插件");
  18. services.AddMqttClientManager();
  19. }
  20. }
  21. /// <summary>
  22. /// mqtt组件
  23. /// 模拟 Configure
  24. /// </summary>
  25. public sealed class MqttApplicationComponent : IApplicationComponent
  26. {
  27. public void Load(IApplicationBuilder app, IWebHostEnvironment env, ComponentContext componentContext)
  28. {
  29. App.GetService<IMqttClientManager>();//获取mqtt服务判断配置是否有问题
  30. }
  31. }
  32. }