elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.05k stars 1.1k forks source link

ServiceBus Sharing #2713

Open jamshally opened 2 years ago

jamshally commented 2 years ago

Summary

In an application that already uses the Rebus ServiceBus, is it possible to configure Elsa to use the same bus?

Motivation

  1. Resource usage: There is a processing cost to each instance of a Rebus ServiceBus - especially when a database transport is used (such as Postgres) as results in a connection for every instance
  2. Interoperability with Elsa: The ability to have a Rebus messages sent outside of an Elsa workflow trigger an Elsa workflow

Initial Attempt (and Difficulty)

One option would perhaps be to provide a custom IServiceBusFactory implementation. However the DI configuration in Elsa seems to lock in the concrete ServiceBusFactory class:

   // File: ElsaServiceCollectionExtensions.cs
   // Method: AddWorkflowsCore

   // Service Bus.
    services
        .AddSingleton<ServiceBusFactory>()
        .AddSingleton<IServiceBusFactory>(sp => sp.GetRequiredService<ServiceBusFactory>())

Questions

  1. Is there some existing way that an existing Rebus instance can be used with Elsa?
  2. If not: is this a capability that could be easily added (I'd be happy to have a go at submitting a pull request, but need some pointers about how to go about it)

Thanks

sfmskywalker commented 2 years ago

Replacing the IServiceBusFactory registration with a custom implementation is probably the way to go. You should be able to replace the registration like this:

services.Replace<IServiceBusFactory, MyCustomServiceBusFactory>

If that doesn't work for some reason, then we could refactor this bit such that you can configure a factory delegate form ElsaOptions - which is what we do for things like the persistence providers too.