Search code examples
c#vpnazure-eventhubsocketexception

Azure EventHub socketexception 10054 when creating CreateBatchAsync and connected in VPN


I am getting below error when executing below code snippet and its causing issue when i connected in VPN And this piece of code is working fine when i don't connect to VPN, could anyone help me out what are the steps to correct this issue (tnc .servicebus.windows.net -port 5671|5672 - status is success when connected to VPN)

using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
namespace _01_sendevents
{
    class Program
    {
        private const string connectionString = "Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=34567SDFGHJ4567vbnm#$%^&*";
        private const string eventHubName = "eventhubname";
        static async Task Main()
        {
            await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
            {
                using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
                Random rnd = new Random();
                int randomNumber = rnd.Next(1, 7);
                for (int i = 0; i <= randomNumber; i++)
                {
                    eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(string.Format("Event-{0}", i.ToString()))));
                }

                await producerClient.SendAsync(eventBatch);
                Console.WriteLine("A batch of {0} events has been published.",randomNumber.ToString());
                Console.ReadLine();
            }
        }
    }
}

Here you go with actual error message

Unhandled exception. System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   at Microsoft.Azure.Amqp.Transport.TransportStream.EndRead(IAsyncResult asyncResult)
   at Microsoft.Azure.Amqp.Transport.TransportStream.<>c__DisplayClass22_0.<ReadAsync>b__1(IAsyncResult a)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at Microsoft.Azure.Amqp.Transport.TlsTransport.HandleOpenComplete(IAsyncResult result, Boolean syncComplete)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Azure.Amqp.ExceptionDispatcher.Throw(Exception exception)
   at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.Azure.Amqp.AmqpObject.OpenAsyncResult.End(IAsyncResult result)
   at Microsoft.Azure.Amqp.AmqpObject.EndOpen(IAsyncResult result)
   at Microsoft.Azure.Amqp.Transport.TlsTransportInitiator.HandleTransportOpened(IAsyncResult result)
   at Microsoft.Azure.Amqp.Transport.TlsTransportInitiator.OnTransportOpened(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.CreateAndOpenConnectionAsync(Version amqpVersion, Uri serviceEndpoint, EventHubsTransportType transportType, IWebProxy proxy, String scopeIdentifier, TimeSpan timeout)
   at Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1.OnCreateAsync(TimeSpan timeout)
   at Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync(TimeSpan timeout)
   at Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync(TimeSpan timeout)
   at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.OpenProducerLinkAsync(String partitionId, TimeSpan timeout, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateLinkAndEnsureProducerStateAsync(String partitionId, TimeSpan timeout, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateLinkAndEnsureProducerStateAsync(String partitionId, TimeSpan timeout, CancellationToken cancellationToken)
   at Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1.OnCreateAsync(TimeSpan timeout)
   at Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync(TimeSpan timeout)
   at Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync(TimeSpan timeout)
   at Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateBatchAsync(CreateBatchOptions options, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateBatchAsync(CreateBatchOptions options, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Producer.EventHubProducerClient.CreateBatchAsync(CreateBatchOptions options, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Producer.EventHubProducerClient.CreateBatchAsync(CancellationToken cancellationToken)
   at _01_sendevents.Program.Main() in C:\Work\WFM Analysis\azurepoc\01-sendevents\Program.cs:line 22
   at _01_sendevents.Program.Main() in C:\Work\WFM Analysis\azurepoc\01-sendevents\Program.cs:line 35
   at _01_sendevents.Program.<Main>()

Solution

  • The exception that you're seeing indicates that the producer is unable to communicate with the Event Hubs service. Despite identifying the correct ports, it appears that your VPN is not routing the request.

    Most often, when we see this behavior, it is due to an environment blocking raw TCP traffic. You may want to consider trying with web sockets to see if that helps. To do so, you'll need to specify a set of options when creating your client. The basic form looks like:

    var options = new EventHubProducerClientOptions();
    options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;
    
    var producerClient = new EventHubProducerClient(
        connectionString, 
        eventHubName, 
        options);
    

    There is also a sample available which illustrates in more detail.