创建 CreateBatchAsync 并在 VPN 中连接时出现 Azure EventHub socketexception 10054
Azure EventHub socketexception 10054 when creating CreateBatchAsync and connected in VPN
我在执行下面的代码片段时遇到以下错误,当我连接到 VPN 时它会导致问题
当我不连接到 VPN 时,这段代码工作正常,任何人都可以帮助我解决这个问题的步骤是什么(tnc .servicebus.windows.net - 端口 5671|5672 - 连接到时状态为成功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();
}
}
}
}
这是实际的错误消息
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-sendevents\Program.cs:line 22
at _01_sendevents.Program.Main() in C:\Work\WFM Analysis\azurepoc-sendevents\Program.cs:line 35
at _01_sendevents.Program.<Main>()
您看到的异常表明生成器无法与事件中心服务通信。尽管识别了正确的端口,但您的 VPN 似乎没有路由请求。
大多数情况下,当我们看到此行为时,这是由于环境阻止了原始 TCP 流量。您可能想考虑尝试使用网络套接字看看是否有帮助。为此,您需要在创建客户端时指定一组选项。基本形式如下:
var options = new EventHubProducerClientOptions();
options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;
var producerClient = new EventHubProducerClient(
connectionString,
eventHubName,
options);
还有一个 sample 可用,其中有更详细的说明。
我在执行下面的代码片段时遇到以下错误,当我连接到 VPN 时它会导致问题 当我不连接到 VPN 时,这段代码工作正常,任何人都可以帮助我解决这个问题的步骤是什么(tnc .servicebus.windows.net - 端口 5671|5672 - 连接到时状态为成功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();
}
}
}
}
这是实际的错误消息
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-sendevents\Program.cs:line 22
at _01_sendevents.Program.Main() in C:\Work\WFM Analysis\azurepoc-sendevents\Program.cs:line 35
at _01_sendevents.Program.<Main>()
您看到的异常表明生成器无法与事件中心服务通信。尽管识别了正确的端口,但您的 VPN 似乎没有路由请求。
大多数情况下,当我们看到此行为时,这是由于环境阻止了原始 TCP 流量。您可能想考虑尝试使用网络套接字看看是否有帮助。为此,您需要在创建客户端时指定一组选项。基本形式如下:
var options = new EventHubProducerClientOptions();
options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;
var producerClient = new EventHubProducerClient(
connectionString,
eventHubName,
options);
还有一个 sample 可用,其中有更详细的说明。