System.OperationCanceledException 在 Xamarin 中从 Azure 事件中心的每个事件上抛出

System.OperationCanceledException Thrown On Every Event From Azure Event Hubs In Xamarin

每次用户将他们的信息发送给另一个用户时,我都试图从我的网络 API 发送一个事件,然后在 Xamarin 中侦听该事件,并在每次事件到来时查询数据库以获取更新我的问题是,每次我的 Xamarin 项目发生事件时,我似乎都会收到此错误。

Microsoft docs 似乎对我的情况没有太大帮助,因为它只说

An attempt is made to invoke an operation on an object that has already been closed, aborted, or disposed. In rare cases, the ambient transaction is already disposed.

这是我在 Xamarin 项目中初始化事件中心客户端的代码。

 public async void InitClient()
    {
        string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;
        BlobContainerClient storageClient = new BlobContainerClient(StorageConnectionString, StorageContainerName);
        eventProcessor = new EventProcessorClient(storageClient, consumerGroup, EventHubConnectionString, EventHubName);
        eventProcessor.ProcessEventAsync += EventProcessor_ProcessEventAsync; ;
        eventProcessor.ProcessErrorAsync += EventProcessor_ProcessErrorAsync;
        await eventProcessor.StartProcessingAsync();
        Console.WriteLine("Processing started");
    }

并且每条消息都在进程错误函数中结束,堆栈跟踪为

    at Microsoft.Azure.Amqp.ExceptionDispatcher.Throw (System.Exception exception) [0x00006] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult] (System.IAsyncResult result) [0x000f5] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.RequestResponseAmqpLink+RequestAsyncResult.End (System.IAsyncResult result) [0x00000] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.RequestResponseAmqpLink.EndRequest (System.IAsyncResult result) [0x00000] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AmqpCbsLink+SendTokenAsyncResult+<>c__DisplayClass13_0.<GetAsyncSteps>b__3 (Microsoft.Azure.Amqp.AmqpCbsLink+SendTokenAsyncResult thisPtr, System.IAsyncResult r) [0x0000c] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.IteratorAsyncResult`1[TIteratorAsyncResult].EnumerateSteps (Microsoft.Azure.Amqp.IteratorAsyncResult`1+CurrentThreadType[TIteratorAsyncResult] state) [0x00158] in <9cea9f83a18d449284cb86dea42f9d95>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Microsoft.Azure.Amqp.ExceptionDispatcher.Throw (System.Exception exception) [0x00006] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult] (System.IAsyncResult result) [0x000f5] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AsyncResult`1[TAsyncResult].End (System.IAsyncResult asyncResult) [0x00000] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AmqpCbsLink.EndSendToken (System.IAsyncResult result) [0x00000] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.AmqpCbsLink+<>c__DisplayClass4_0.<SendTokenAsync>b__1 (System.IAsyncResult a) [0x00000] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at (wrapper delegate-invoke) System.Func`2[System.IAsyncResult,System.DateTime].invoke_TResult_T(System.IAsyncResult)
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:534 
--- End of stack trace from previous location where exception was thrown ---

  at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.CreateReceivingLinkAsync (Microsoft.Azure.Amqp.AmqpConnection connection, System.Uri endpoint, Azure.Messaging.EventHubs.Consumer.EventPosition eventPosition, System.TimeSpan timeout, System.UInt32 prefetchCount, System.Nullable`1[T] ownerLevel, System.Boolean trackLastEnqueuedEventProperties, System.Threading.CancellationToken cancellationToken) [0x00431] in <2347303e556c4d659b494dac79430634>:0 
  at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.OpenConsumerLinkAsync (System.String consumerGroup, System.String partitionId, Azure.Messaging.EventHubs.Consumer.EventPosition eventPosition, System.TimeSpan timeout, System.UInt32 prefetchCount, System.Nullable`1[T] ownerLevel, System.Boolean trackLastEnqueuedEventProperties, System.Threading.CancellationToken cancellationToken) [0x001a3] in <2347303e556c4d659b494dac79430634>:0 
  at Azure.Messaging.EventHubs.Amqp.AmqpConsumer.CreateConsumerLinkAsync (System.String consumerGroup, System.String partitionId, Azure.Messaging.EventHubs.Consumer.EventPosition eventStartingPosition, System.UInt32 prefetchCount, System.Nullable`1[T] ownerLevel, System.Boolean trackLastEnqueuedEventProperties, System.TimeSpan timeout, System.Threading.CancellationToken cancellationToken) [0x000af] in <2347303e556c4d659b494dac79430634>:0 
  at Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1[T].OnCreateAsync (System.TimeSpan timeout) [0x00077] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.Singleton`1[TValue].GetOrCreateAsync (System.TimeSpan timeout) [0x00154] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Microsoft.Azure.Amqp.Singleton`1[TValue].GetOrCreateAsync (System.TimeSpan timeout) [0x001fc] in <9cea9f83a18d449284cb86dea42f9d95>:0 
  at Azure.Messaging.EventHubs.Amqp.AmqpConsumer.ReceiveAsync (System.Int32 maximumMessageCount, System.Nullable`1[T] maximumWaitTime, System.Threading.CancellationToken cancellationToken) [0x0019e] in <2347303e556c4d659b494dac79430634>:0 
  at Azure.Messaging.EventHubs.Amqp.AmqpConsumer.ReceiveAsync (System.Int32 maximumMessageCount, System.Nullable`1[T] maximumWaitTime, System.Threading.CancellationToken cancellationToken) [0x00591] in <2347303e556c4d659b494dac79430634>:0 
  at Azure.Messaging.EventHubs.Primitives.EventProcessor`1+<>c__DisplayClass61_0[TPartition].<CreatePartitionProcessor>g__performProcessing|1 () [0x0020c] in <2347303e556c4d659b494dac79430634>:0 
  at Azure.Messaging.EventHubs.Primitives.EventProcessor`1+<>c__DisplayClass61_0[TPartition].<CreatePartitionProcessor>g__performProcessing|1 () [0x0044f] in <2347303e556c4d659b494dac79430634>:0 

我只是在诊断可能的原因以及可能导致事件对象关闭的原因时遇到问题,或者如果我没有做正确的事情,我发现这非常令人困惑,因为我的代码直接反映出来来自 Microsoft Docs 教程。任何指向正确方向或导致可能出错的指示都将不胜感激。谢谢!

更新 我发现当我创建一个控制台应用程序来接收事件时,它可以完美地工作。在 Xamarin 中时会出现此问题。我不太确定是什么导致了 Xamarin 方面的问题。我在用户登录后和主页出现时启动客户端。

这看起来是网络堆栈中的某种问题。正在触发异常,因为将授权令牌发送到事件中心服务的操作超时。由于连接上的所有链接都共享授权,因此这很可能是第一次初始化连接。您可能想尝试使用 WebSockets 来查看是否是端口问题。 This sample 说明这样做。