执行以下代码片段时出现以下错误,并在 VPN 连接时导致问题 当我不连接到 VPN 时,这段代码工作正常,任何人都可以帮我解决纠正此问题的步骤(tnc .servicebus.windows.net -port 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\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>()
最佳答案
您看到的异常表明生产者无法与事件中心服务进行通信。尽管识别了正确的端口,但您的 VPN 似乎并未路由该请求。
大多数情况下,当我们看到此行为时,这是由于环境阻止原始 TCP 流量造成的。您可能需要考虑尝试使用网络套接字,看看是否有帮助。为此,您需要在创建客户端时指定一组选项。基本形式如下:
var options = new EventHubProducerClientOptions();
options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;
var producerClient = new EventHubProducerClient(
connectionString,
eventHubName,
options);
还有一个sample可用其中更详细地说明。
关于c# - 创建 CreateBatchAsync 并在 VPN 中连接时出现 Azure EventHub 套接字异常 10054,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64558111/