Search code examples
azure-eventhub

Azure event hub error when debugging with Visual Studio


I have created a azure function with event hub trigger and keep getting this error "The listener for function 'Transfers.OrdersFunction was unable to start. System.Private.CoreLib: One or more errors occurred. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)." when I run debugger. I've followed this code: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=in-process%2Cfunctionsv2%2Cextensionv5&pivots=programming-language-csharp and I am using Visual Studio 2022


Solution

  • Usually, this problem is related to your dev machine firewall blocking ports 5671 and 5672. Check this out:

    https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-protocol-guide

    If you can’t open those tpc ports, there is a trick for that. Add this to the end of your connection string: TransportType=AmqpWebSockets

    But could be how you added the connection string to the code. You must make sure you get the connection string for a specific event hub in a namespace:

    https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-get-connection-string#connection-string-for-a-specific-event-hub-in-a-namespace

    When you apply that to the code, make sure you remove the "EntityPath=" part.

    For example , consider the connections string is :

    "Endpoint=sb://myhub.servicebus.windows.net/;SharedAccessKeyName=mypolicy;SharedAccessKey=bc34534534543l+4wyWi5jbrQ=;EntityPath=myhub;"

    remove the entity path and add it to the "local.settings.json" as you connection string setting

     {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "MyConnSetting": "Endpoint=sb://myhub.servicebus.windows.net/;SharedAccessKeyName=mypolicy;SharedAccessKey=bc34534534543l+4wyWi5jbrQ=;TransportType=AmqpWebSockets"
      }
    }
    

    than add the connection string setting and entity path value like this:

    [Function("Function1")]
        public void Run([EventHubTrigger("myhub", Connection = "MyConnSetting")] string[] input)
        {
            _logger.LogInformation($"First Event Hubs triggered message: {input[0]}");
        }
    

    Remember this is just a way to force the trigger to use websockets. once you deploy you can set it to use AMQP. Hope this help!