Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using steeltoe connectors with EasyNetQ is awkward. #1309

Open
macsux opened this issue Jun 3, 2024 · 6 comments
Open

Using steeltoe connectors with EasyNetQ is awkward. #1309

macsux opened this issue Jun 3, 2024 · 6 comments
Labels
Component/Connectors Issues related to Steeltoe connectors Type/enhancement New feature or request

Comments

@macsux
Copy link
Contributor

macsux commented Jun 3, 2024

Problem

EasyNetQ is a popular package that provides high level abstrations for working with RabbitMQ. The way it wants to be configured is via its own configuration class that expects to be provided individual connection string elements of RabbitMQ (host, port, credentials, SSL settings, etc). Example:

builder.Services.RegisterEasyNetQ(svc =>
    {
        return new ConnectionConfiguration
        {
            Hosts = new List<HostConfiguration> { host },
            UserName = username,
            Password = password
        };
    });

Currently, connectors do not provide easy way to access individual elements needed to configure it. The closest is the ability to inject RabbitMQOptions that only exposes string ConnectionString property, requiring one to parse it manually.

Proposed solution

Register both IConnection and IConnectionFactory from RabbitMQ. If IConnectionFactory is available, it can be cast back to ConnectionFactory and all the elements copied over. Alternatively (or in addition), expose make RabbitMQConnectionStringBuilder public and expose individual well-known elements of connection string as strongly type properties.

@macsux macsux added the Type/enhancement New feature or request label Jun 3, 2024
@bart-vmware
Copy link
Member

Thanks, it would be nice to have built-in support for EasyNetQ, or at least have a sample demonstrating how it can be used.

It shouldn't be hard to use already though, because the format of the connection string is just a URI (see https://github.com/SteeltoeOSS/Samples/blob/latest/Connectors/src/RabbitMQ/Program.cs). So by parsing it into System.Uri from options, its host/port/username/password, etc can be obtained to set up ConnectionConfiguration.

There's also an overload that takes a connection string. Their IServiceResolver and IServiceRegister look similar to IServiceCollection and IServiceProvider, so maybe they can be used.

@bart-vmware bart-vmware added the Component/Connectors Issues related to Steeltoe connectors label Jun 4, 2024
@macsux
Copy link
Contributor Author

macsux commented Jun 4, 2024

Here's a sample app that uses RabbitMQ: https://github.com/macsux/pcf-ers-dotnetcore-demo/blob/9f21a82663be0a5d11b971cfb6e57cf2fe775698/src/CloudPlatformDemo/Program.cs#L169

As far as connection string overload, it requires knowledge of it's value during registration time. It's not available yet as it will only be made available as part of IOptions<RabbitMQOptions> after container is built.

@bart-vmware
Copy link
Member

I tried this from a unit test, seems to work:

builder.Services.RegisterEasyNetQ(serviceResolver =>
{
    var optionsMonitor = serviceResolver.Resolve<IOptionsMonitor<RabbitMQOptions>>();
    RabbitMQOptions options = optionsMonitor.Get(null);

    var parser = serviceResolver.Resolve<IConnectionStringParser>();
    return parser.Parse(options.ConnectionString);
});

await using WebApplication app = builder.Build();

var bus = app.Services.GetRequiredService<IBus>();

@bart-vmware
Copy link
Member

@macsux Would it help if Steeltoe provided an extension method AddEasyNetQ, similar to AddRabbitMQ, to obtain a connected IBus (instead of IConnection) instance?

@macsux
Copy link
Contributor Author

macsux commented Aug 16, 2024

@bart-vmware I think it would be fantastic addition, especially if it can be done reflectively without having to introduce a new package that adds dependency to EasyNetQ

@bart-vmware
Copy link
Member

Yes, that's what I had in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component/Connectors Issues related to Steeltoe connectors Type/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants