Error starting endpoint in WebAPI application

Hi,

I’m using the following code to configure and start an endpoint inside the Application_Start() (in Global.asax.cs) of a WebAPI legacy application based on .NET Framework.

var endpointConfiguration = new EndpointConfiguration(endpointName);
endpointConfiguration.UsePersistence();
endpointConfiguration.EnableInstallers();
var transport = endpointConfiguration.UseTransport();
transport.ConnectionString(“host=localhost;”);
transport.UseConventionalRoutingTopology();
var endpointInstance = await NServiceBus.Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

However, when trying to Start, an exception is thrown (see below).

OperationInterruptedException: The AMQP operation was interrupted: AMQP close-reason, initiated by Library, code=541, text=‘Unexpected Exception’, classId=0, methodId=0, cause=System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. —> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

The same code works when the LearningTransport is used or when hosted in a console application.

Thank you in advance for any clues on how to solve this!

Adrian

The message is saying the connection to the RabbitMQ server was disconnected unexpectedly.

One place to check is the server logs on for RabbitMQ. See if anything is a similar time frame was logged that explains why RabbitMQ severed the connection. Often that shows what was wrong.

Otherwise, you need to check the network in-between, which is rarer but still sometimes the cause.

Hi,

the RabbitMQ logs show only the following lines:

2021-11-11 11:10:23.128000+02:00 [info] <0.11486.1> accepting AMQP connection <0.11486.1> ([::1]:27580 → [::1]:5672)
2021-11-11 11:10:33.075000+02:00 [error] <0.11486.1> closing AMQP connection <0.11486.1> ([::1]:27580 → [::1]:5672):
2021-11-11 11:10:33.075000+02:00 [error] <0.11486.1> {handshake_timeout,frame_header}

The application and the RabbitMQ server reside on the same machine and the antivirus and the firewall are deactivated.
The problem seems to be rather related to the fact that the code is hosted inside a .NET Framework WebAPI application. If I move it to a console application, then it works fine.

How are you starting NServiceBus in your WebAPI application?

It looks like something is preventing the AMQP handshake from completing.

Here is the initialization code:

public class WebApiApplication : System.Web.HttpApplication
{
	protected void Application_Start()
	{
		AreaRegistration.RegisterAllAreas();
		AutofacWebapiConfig.Initialize(GlobalConfiguration.Configuration);
		GlobalConfiguration.Configure(WebApiConfig.Register);
		FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
		RouteConfig.RegisterRoutes(RouteTable.Routes);
		BundleConfig.RegisterBundles(BundleTable.Bundles);

		EndpointInstance.InitializeAsync("WebAPI.Publisher").GetAwaiter().GetResult();
	}      
}
	
	
public static class EndpointInstance
{
	public static IEndpointInstance Endpoint { get; private set; }
	
	private static void SetInstance(IEndpointInstance endpoint)
	{
		if (Endpoint != null)
		{
			throw new Exception("Endpoint already set.");
		}
		Endpoint = endpoint;
	}

	public static async Task InitializeAsync(string endpointName)
	{
		var endpointConfiguration = new EndpointConfiguration(endpointName);
		endpointConfiguration.UsePersistence<InMemoryPersistence>();
		endpointConfiguration.EnableInstallers();
		
		var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
		transport.ConnectionString("host=localhost");
		transport.UseConventionalRoutingTopology();

		var endpointInstance = await NServiceBus.Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

		Console.WriteLine("Endpoint started.");
		
		SetInstance(endpointInstance);
	}

	public static async Task StopAsync()
	{
		await Endpoint.Stop().ConfigureAwait(false);
	}
}