title: C# : OperationCanceledException : The operation was canceled
body: <p>Below I am trying to send data to eventhub, it works for few minutes and then the <em>OperationCanceledException</em> is thrown. Any hint on where I am wrong in using CancellationToken (if that is what I should be using)? or how I could fix this issue?</p>
  <pre><code>public async void send&lt;T&gt;(IEnumerable&lt;T&gt; list, string eventhubname)
  {                
       var token = new CancellationTokenSource();
       CancellationToken ct = token.Token;
       EventHubProducerClient producer = null;
  
       try
       {
  
          producer = new EventHubProducerClient(this._connectionString, eventhubname);
  
          var eventBatch = await producer.CreateBatchAsync(ct); **Line 148 here**
          foreach (T item in list)
          {
              eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(item.ToString())));
          }
         await producer.SendAsync(eventBatch);
         await producer.DisposeAsync();
       }
      catch (Exception ex)
      {
         //($&quot;Error While sending message to Event Hub: { ex.Message}&quot;, ex);
          if (producer != null)
          {
              await producer.DisposeAsync();
          }
          if (ct.IsCancellationRequested)
          {
          token.Dispose();
          throw new TaskCanceledException(ex.Message);
          }
          throw;
      }
  }
  </code></pre>
  <p>Following is the Exception stalk</p>
  <pre><code>
  System.OperationCanceledException: The operation was canceled.
     at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result)
     at Microsoft.Azure.Amqp.AmqpCbsLink.SendTokenAsyncResult.&lt;&gt;c__DisplayClass13_0.&lt;GetAsyncSteps&gt;b__3(SendTokenAsyncResult thisPtr, IAsyncResult r)
     at Microsoft.Azure.Amqp.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result)
     at Microsoft.Azure.Amqp.AmqpCbsLink.&lt;&gt;c__DisplayClass4_0.&lt;SendTokenAsync&gt;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.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.&lt;CreateSendingLinkAsync&gt;d__63.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.&lt;OpenProducerLinkAsync&gt;d__58.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Azure.Messaging.EventHubs.Amqp.AmqpProducer.&lt;CreateLinkAndEnsureProducerStateAsync&gt;d__32.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1.&lt;OnCreateAsync&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.Amqp.Singleton`1.&lt;GetOrCreateAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.Amqp.Singleton`1.&lt;GetOrCreateAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Azure.Messaging.EventHubs.Amqp.AmqpProducer.&lt;CreateBatchAsync&gt;d__29.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Azure.Messaging.EventHubs.Producer.EventHubProducerClient.&lt;CreateBatchAsync&gt;d__42.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at &lt;send&gt;d__20`1.MoveNext() in  line 148
  </code></pre>