配置与扩展


1、配置文件

  通过文件可以配置多个订阅管理器,通过更换 default 的值即可实现无缝切换。配置文件如下:

  • .Net Framework 下的 app.config 或 web.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="fireasy">
      <section name="subscribers" type="Fireasy.Common.Subscribes.Configuration.SubscribeConfigurationSectionHandler, Fireasy.Common" />
    </sectionGroup>
  </configSections>
  <fireasy>
    <subscribers default="rabbitmq">
      <subscriber name="rabbitmq" type="Fireasy.RabbitMQ.SubscribeManager, Fireasy.RabbitMQ" />
        <config userName="guest" password="123" server="amqp://127.0.0.1:5672" />
      </subscriber>
      <subscriber name="demo" type="demo.SubscribeManager, demo" />
    </subscribers>
  </fireasy>
</configuration>
  • .Net Core 下的 appsettings.json 文件
{
  "fireasy": {
    "subscribers": {
      "default": "rabbitmq",
      "settings": {
        "rabbitmq": {
          "type": "Fireasy.RabbitMQ.SubscribeManager, Fireasy.RabbitMQ",
          "config": {
             "userName": "guest",
             "password": "123",
             "server": "amqp://127.0.0.1:5672"
          }
        },
        "demo": {
          "type": "demo.SubscribeManager, demo"
        }
      }
    }
  }
}

  SubscribeManagerFactory 工厂类可以根据配置创建一个 ISubscribeManager 实例。如下所示:

[TestMethod]
public void TestCreateManager()
{
    var manager1 = SubscribeManagerFactory.CreateManager();
    Assert.IsNotNull(manager1); // RabbitMQ

    var manager2 = SubscribeManagerFactory.CreateManager("demo");
    Assert.IsNotNull(manager2); // demo
}

  在使用 IOC 的环境中,通过构造注入或属性注入得到的 ISubscribeManager 的实例对应的是指向 default 的配置项。


2、第三方适配