question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Azure.Messaging.EventHubs: System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'

See original GitHub issue

Describe the bug Executing the following code with version 5.2.0 of Azure.Messaging.EventHubs results in an exception:

image

var producer = new EventHubProducerClient(ConfigStuff.connectionString, ConfigStuff.eventHubName);
var partitions = await producer.GetPartitionIdsAsync();

Exception:

System.InvalidOperationException: Operation is not valid due to the current state of the object.

This exception only occurs for .NET 5.0

Expected behavior I would expect this code to work without throwing an error

Actual behavior (include Exception or Stack Trace)

Stacktrace:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.Azure.Amqp.Transport.TransportStream.Flush()
   at System.IO.Stream.<>c.<FlushAsync>b__39_0(Object state)
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Threading.Tasks.TaskToApm.End(IAsyncResult asyncResult)
   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 ---
   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 ---
   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.OpenManagementLinkAsync(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.AmqpClient.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Amqp.AmqpClient.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.EventHubConnection.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.EventHubConnection.GetPartitionIdsAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancellationToken)
   at Azure.Messaging.EventHubs.Producer.EventHubProducerClient.GetPartitionIdsAsync(CancellationToken cancellationToken)
   at EventSender.Program.Main(String[] args) in C:\XBDR\Tests\EventSender\EventSender\Program.cs:line 21

To Reproduce Steps to reproduce the behavior (include a code snippet, screenshot, or any additional information that might help us reproduce the issue)

  1. Install Azure.Messaging.EventHubs version 5.2.0 nuget package
  2. Run code as mentioned above

Environment:

  • Name and version of the Library package used: Azure.Messaging.EventHubs 5.2.0
  • Hosting platform or OS and .NET runtime version:
.NET runtimes installed:
 Microsoft.AspNetCore.All 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
 Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
 Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
 Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
 Microsoft.AspNetCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.NETCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
 Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
 Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • IDE and version:
Microsoft Visual Studio Enterprise 2019
Version 16.8.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
devedsecommented, Jan 7, 2021

I saw some previous issues regarding this were already closed, but the bug doesn’t seem to be resolved: https://github.com/Azure/azure-sdk-for-net/issues/14068 https://github.com/Azure/azure-sdk-for-net/issues/13899

Manually installing the latest version of Microsoft.Azure.Amqp resolves this issue:

Install-Package Microsoft.Azure.Amqp
0reactions
bqstonycommented, Dec 9, 2021

@jsquire I have also the error. Created a EventHub Trigger for IotHub from template in VS2022

#    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.2.0" />
#    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public class Function1
    {
        private static HttpClient client = new HttpClient();
        
        [FunctionName("Function1")]
        public void Run([IoTHubTrigger("messages/events", Connection = "EventHubConnectionString", ConsumerGroup = "iot-euw-iotdatastorage-prod-func")]EventData message, ILogger log)
        {
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
        }
    }
}

The error output ist:

For detailed output, run func with --verbose flag.
[2021-12-09T12:44:04.197Z] The listener for function 'Function1' was unable to start.
[2021-12-09T12:44:04.199Z] The listener for function 'Function1' was unable to start. Microsoft.Azure.EventHubs.Processor: Encountered error while fetching the list of EventHub PartitionIds. Microsoft.Azure.Amqp: Operation is not valid due to the current state of the object.
[2021-12-09T12:44:06.244Z] The listener for function 'Function1' was unable to start.
[2021-12-09T12:44:06.245Z] The listener for function 'Function1' was unable to start. Microsoft.Azure.EventHubs.Processor: Encountered error while fetching the list of EventHub PartitionIds. Microsoft.Azure.Amqp: Operation is not valid due to the current state of the object.
[2021-12-09T12:44:08.424Z] Host lock lease acquired by instance ID '00000000000000000000000001684FDD'.[2021-12-09T12:44:10.414Z] The listener for function 'Function1' was unable to start.

[2021-12-09T12:44:11.267Z] The listener for function 'Function1' was unable to start. Microsoft.Azure.EventHubs.Processor: Encountered error while fetching the list of EventHub PartitionIds. Microsoft.Azure.Amqp: Operation is not valid due to the current state of the object.

after adding the reference to <PackageReference Include="Microsoft.Azure.Amqp" Version="2.5.8" /> explizit as @devedse said, it works now

Read more comments on GitHub >

github_iconTop Results From Across the Web

operation is not valid due to the current state of the object. ...
Try updateing the azure-amqp library to the 2.4.8 version and see if it works. This link mentions the following: Workaround: TransportType= ...
Read more >
Event Hubs messaging exceptions - .NET (legacy)
This error can occur for one of two reasons: The load isn't evenly distributed across all partitions on the event hub, and one...
Read more >
How to fix 'Operation is not valid due to the current state of ...
I have a .NET Azure Function executed by Logic Apps triggered by an SQL connector "When an item is created V2". The function...
Read more >
The connection string used for an Event Hub client must ...
I am getting below error:- Azure.Messaging.EventHubs: The connection string used for an Event Hub client must specify the Event Hubs namespace host, ...
Read more >
error: Operation is not valid due to the current state of the object.
Hi, I get this error. Unexpected error: Operation is not valid due to the current state of the object. Details: Microsoft.Mashup.Evaluator.Interface.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found