UseNServiceBus on .Net7 SQS

I am setting up my NServiceBus client to use Sqs like this:

private static void NServiceBusBuilder()
{
var builder = WebApplication.CreateBuilder();

    builder.Host.UseNServiceBus(context =>
    {
        var endpointConfiguration = new EndpointConfiguration("samples_send_test_queue");
        var transport = new SqsTransport(
            new AmazonSQSClient(new AmazonSQSConfig()),
            new AmazonSimpleNotificationServiceClient());
        var routing = endpointConfiguration.UseTransport(transport);
        routing.RouteToEndpoint(typeof(RequestMessage), "samples_receive_test_queue");

        endpointConfiguration.SendOnly();
        endpointConfiguration.UseSerialization<XmlSerializer>();
        endpointConfiguration.EnableInstallers();
        return endpointConfiguration;
    });
}

When I try to use Endpoint.Start(endpointconfiguration) right after EnableInstallers the web api fails with an error. Here is the start code line Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

Error Message:
Web server failed to listen on port 8085

How do I debug this? Am I setting up everything correctly? Is it possible I do not have something correctly configured to connect to AWS? If so shouldn’t I get an error and not just crash? Thanks.

Good day @JackInTheBoxBen. Welcome to the Particular discussion group.

It looks like the error is unrelated to NServiceBus. We don’t set up a web server/listener, nor does the transport or the AWS SDK client, as far as I can tell.

Is this happening on your local machine or in AWS after having deployed the project?

If this is happening on your machine and considering that you’re setting up a WebApllication, look in the launchSettings.json file in the Properties folder of the Visual Studio project and verify if the assigned port is the same as mentioned in the error. If that’s the case, the port might be already in use by something else on the machine.

This is happening on my local PC. Here are my launch settings.
“http”: {
“commandName”: “Project”,
“launchBrowser”: true,
“launchUrl”: “swagger”,
“environmentVariables”: {
“ASPNETCORE_ENVIRONMENT”: “Development”,
“AWS_PROFILE”: “development”,
“AWS_REGION”: “us-west-2”
},
“dotnetRunMessages”: true,
“applicationUrl”: “http://localhost:8085
Yes the port in error is the same however I have tried other ports too. Lastly the problem only occurs when this line of code is included:
Endpoint.Start(endpointConfiguration).ConfigureAwait(false);
Somehow I don’t think I am connecting correctly. Does my code look correct? Thanks.

Everything looks legit to me.

The only missing bits from the launch settings are the export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables to connect to SQS.

I finally got this to work by using the following code. This method needs to be called before the web application host build.

private static void NServiceBusBuilder()
{
    var regionEndPoint = RegionEndpoint.GetBySystemName(builder.Configuration.GetSection("AWS:Region").Value);
    var accessKeyId = builder.Configuration.GetSection("AWS:AccessKeyId");
    var secretKey = builder.Configuration.GetSection("AWS:SecretKey");

    var awsConfig = new AmazonSQSConfig();
    awsConfig.RegionEndpoint = regionEndPoint;

    var credentials = new BasicAWSCredentials(accessKeyId.Value, secretKey.Value);

    builder.Host.UseNServiceBus(context =>
    {
        var endpointConfiguration = new EndpointConfiguration("samples_send_test_queue");
        var transport = new SqsTransport(
            new AmazonSQSClient(credentials, awsConfig),
            new AmazonSimpleNotificationServiceClient());
        var routing = endpointConfiguration.UseTransport(transport);
        routing.RouteToEndpoint(typeof(RequestMessage), "samples_receive_test_queue");
        endpointConfiguration.SendOnly();
        endpointConfiguration.UseSerialization<XmlSerializer>();
        endpointConfiguration.EnableInstallers();
        return endpointConfiguration;
    });
}

Note that all AWS configuration comes from a config file so way more flexible than using environment variables and this sets us up nicely to use a Kubernetes configmap and Kubernetes Secrets.

The program still blows up when I add the line
Endpoint.Start(endpointConfiguration).ConfigureAwait(false); right after the EnableInstallers line. Any ideas as to why or how I can debug this? Does Sqs allow creating message queues programmatically through EnableInstallers? Otherwise I have to manually create the queues to get this to work and not use that line. If it doesn’t work do I still even need the EnableInstallers function?

Would you be able to share the code with me?

I sent you an email. If you can give me a location I can post about an 8mb zip file I can send you all of the code. Thanks!

I replied with a couple of options.