Getting an instance of BusHealth using unity container

Viewed 176

How to get an instance of bushealth using Unity container IoC in Prism or without using a IoC container? Mass Transit relies on Microsoft Dependency Injection, however I am using Bus Factory to create an instance of the bus and its always not feasbile to use DI for legacy code.

_busControl = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                sbc.Host(host, h =>
                {
                    h.Username(username);
                    h.Password(password);
                    h.RequestedConnectionTimeout(timeout);
                });
                sbc.ReceiveEndpoint(e =>
                {
                    ...
                });
1 Answers

You can either create an instance of BusHealth passing the bus instance on the constructor or you can register IBusHealth as a service in your container, with the BusHealth implementation type (which will require IBus to also be registered, as it is a required constructor dependency).

Then you can use the IBusHealth interface in your application to check bus health.

var busHealth = new BusHealth("bus");

_busControl = Bus.Factory.CreateUsingRabbitMq(sbc =>
{
    sbc.ConnectBusObserver(busHealth);
    sbc.ConnectEndpointConfigurationObserver(busHealth);

    sbc.Host(host, h =>
    {
        h.Username(username);
        h.Password(password);
        h.RequestedConnectionTimeout(timeout);
    });
    sbc.ReceiveEndpoint(e =>
    {
        // ...
    });
});

Of course, this doesn't explain how the bus/health objects get into your container, but I think you get the idea.

Related