问题描述
当我在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,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流量。您可能要考虑尝试使用Web套接字,看看是否有帮助。为此,您在创建客户端时需要指定一组选项。基本形式如下:
var options= new EventHubProducerClientOptions();
options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;
var producerClient = new EventHubProducerClient(
connectionString,eventHubName,options);
还有一个sample,可以更详细地说明。