Establishing connection from IIS hosted app hangs

Hello. We are using self-hosted service using Topshelf and Nancy. The service communicates with in-memory Event Store database. When switching to IIS hosting (using Nancy.Hosting.Aspnet) the application hangs when entering IEventStoreConnection.ConnectAsync() method. It uses the same code that is being used for connection from self-hosted environment.
We also noticed that after ConnectAsync() call the Event Store logs the connection (External TCP connection accepted) although it doesn’t return.
Can IIS affect the client connection in any way?
Are there any known issues integrating with IIS?
Is there something we are missing?

The code used for the connection:
var eventStoreConnection = EventStoreConnection.Create(ConnectionSettings.Create().KeepReconnecting().KeepRetrying().Build(), url);
await eventStoreConnection.ConnectAsync();

Try EnableVerboseLogging then you can see what is happening with the connection (you can add a file or custom logger)

Thanks for that. We tried it however there is nothing that would indicate a problem. Extract from the log file:
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.StartConnectionMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: StartConnection.
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: DiscoverEndPoint.
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.EstablishTcpConnectionMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: EstablishTcpConnection to [127.0.0.1:1115].
TcpPackageConnection: connected to [127.0.0.1:1115, L127.0.0.1:23080, {2afc499f-3c62-4d50-9eb4-b66e4626ad9c}].
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.TcpConnectionEstablishedMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: TCP connection to [127.0.0.1:1115, L127.0.0.1:23080, {2afc499f-3c62-4d50-9eb4-b66e4626ad9c}] established…

EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.HandleTcpPackageMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: HandleTcpPackage connId 2afc499f-3c62-4d50-9eb4-b66e4626ad9c, package Authenticated, b34a836a-66a6-4e7d-b1bc-31536d26b9c0…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.HandleTcpPackageMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: HandleTcpPackage connId 2afc499f-3c62-4d50-9eb4-b66e4626ad9c, package HeartbeatResponseCommand, eb178b88-e03f-4a98-849c-014ffef3f5a8…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: enqueueing message EventStore.ClientAPI.Internal.HandleTcpPackageMessage…
EventStoreConnection ‘ES-080d90e9-80a8-4cc2-83da-4313608522ce’: HandleTcpPackage connId 2afc499f-3c62-4d50-9eb4-b66e4626ad9c, package HeartbeatRequestCommand, 6cd2ca24-1801-4777-821a-31ad0e5c42ce…
(…)

What do you see when you .Wait() instead of await? Any chance an (Aggregate)Exception is sinking somewhere?

We tried the below code and it worked. Any ideas why it doesn’t work with await eventStoreConnection.ConnectAsync() and why the problem only occurs when hosted under IIS? It doesn’t seem to make much sense.

var eventStoreConnection = EventStoreConnection.Create(ConnectionSettings.Create().KeepReconnecting().KeepRetrying().Build(), url);
var task = eventStoreConnection.ConnectAsync();
await task;
return eventStoreConnection;

I believe you are running into a SynchronizationContext issue. Try this:

var eventStoreConnection = EventStoreConnection.Create(ConnectionSettings.Create().KeepReconnecting().KeepRetrying().Build(), url);
await eventStoreConnection.ConnectAsync().ConfigureAwait(false);
return eventStoreConnection;

I tried it. When stepping into the code I’m able to continue to the return eventStoreConnection statement getting the impression that it works however the application still hangs. At the moment it looks like the only thing that works is storing the task in a variable and then awaiting it.