Home > Back-end >  NServiceBus SqlServerTransport with .NET 6 Worker Service Issues
NServiceBus SqlServerTransport with .NET 6 Worker Service Issues

Time:06-30

I am attempting to use NServiceBus 7.7.3 and its SqlServerTransport with a .NET 6 worker service. The SqlServerTransport is using the NServiceBus.Transport.SqlServer 6.3.3 and Microsoft.Data.SqlClient 4.1.0 libraries.

When NServiceBus released version 7.x, it posted a note about a configuration change for SqlServerTransport and a code sample on using NServiceBus with a .NET worker service that I followed to create this code:

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService()
    .UseNServiceBus(context =>
    {
        var endpointConfiguration = new EndpointConfiguration("CRM");
        string connectionString = configuration.GetValue<string>("ConnectionStrings:CrmDatabase");
        SqlServerTransport transport = new SqlServerTransport(connectionString);
        endpointConfiguration.UseTransport(transport);

        //code for routing, defining critical error actions, etc.

        return endpointConfiguration;
    })
    .ConfigureServices(services =>
    {
     services.AddHostedService<GALSync.InterAction.Processes.CrmListener.Worker>();
    })
    .UseSerilog()
    .Build();

await host.RunAsync();
}

On the statement SqlServerTransport transport = new SqlServerTransport(connectionString); the instantiation new SqlServerTransport raises the error "SqlServerTransport does not contain a constructor that takes 1 arguments."

The statement endpointConfiguration.UseTransport(transport); raises the error "Argument 2: cannot convert from 'NServiceBus.SqlServerTransport' to 'System.Type'"

I updated the NServiceBus and NServiceBus.Transport.SqlServer libraries to 8.0.0-beta.3 and 7.0.0-beta 2, respectively, thinking there may be a .NET 6 issue with NServiceBus. The library updates removed both errors, but upon running, the code raised the "Either the connection string or connection factory has to be specified in the SQL Server transport configuration" error. The SQL connection string was tested and does connect to the SQL server allowing all SQL functionality to run perfectly.

I reverted back to the current production libraries and tried an older code configuration, which is slated to be deprecated:

endpointConfiguration.UseTransport<SqlServerTransport>().ConnectionString(connectionString);

Like the beta libraries, this did not have constructor errors and when running raised the connection string not specified error.

This may be a bug from using .NET 6 with NServiceBus, but I could also be missing something. Any assistance getting the NServiceBus SqlServerTransport working with a .NET 6 worker service would be greatly appreciated.

CodePudding user response:

I've got this exact scenario working on .Net 6. I'm using the "to be deprecated" version of

var transport = endpointConfiguration.UseTransport<SqlServerTransport>();
transport.ConnectionString(connectionString);

...otherwise, we're pretty much the same. I did look at the constructors for SqlServerTransport and indeed, there are only constructors that take two arguments. Take a look and see if either of those work for you. The main purpose of this answer was to confirm that it does indeed work with NET6

  • Related