Search code examples
azureazure-functionsazureservicebusazure-eventhub

Error pushing data in an Azure Function to an Azure Event Hub


I'm consuming data from an Azure Service Bus and I want to push it to an Azure Event Hub.

I've created a function like so:

[FunctionName("IntegrationFunction")]
[return: EventHub("%Transactions:EVH:Transactions:Hub%", Connection = "Transactions:SB:Transactions")]
public Transaction Run(
    [ServiceBusTrigger(
        "%Transactions:SB:Transactions:ReceiveTopic%",
        "%Transactions:SB:Transactions:AnalyticsTopicSubscription%",
        Connection = "Transactions:SB:Transactions")]
    string mySbMsg,
    ILogger log)
{
    if (string.IsNullOrWhiteSpace(mySbMsg))
    {
        throw new ArgumentNullException(nameof(mySbMsg));
    }

    log.LogInformation($"Service bus topic trigger function processing message: {mySbMsg}");
    
    var retailTransaction = JsonConvert.DeserializeObject<RetailTransaction>(
        mySbMsg,
        JsonSerialisationUtils.SerialiserSettings);
    
    if (retailTransaction == null)
    {
        throw new JsonException("Deserialized transaction was null");
    }

    try
    {
        var transaction = retailTransaction.ToDto();
        log.LogInformation($"Transaction {transaction.TransactionNumber} processed.");
        return transaction;
    }
    catch (Exception e)
    {
        log.LogError(e, "Error mapping transaction.");
    }

    return null;
}

However, when the function is invoked, I receive the following error:

Put token failed. status-code: 404, status-description: The messaging entity 'sb://****.servicebus.windows.net/Transactions-EVH-INS-DEV-EUW' could not be found. To know more visit https://aka.ms/sbResourceMgrExceptions. TrackingId:**************, SystemTracker:****:Transactions-EVH-INS-DEV-EUW, Timestamp:2024-03-12T07:49:07.

Does that mean that the value of %Transactions:EVH:Transactions:Hub% is incorrect or something else? Why does the error mention service bus when I'm trying to publish to an event hub?

I've already read this article.


Solution

  • I am also getting the same error when I am trying to use the receiving eventhub name in place of topic name.

    Use correct syntax.

    This worked for me.

    My Code:

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    
    namespace FunctionApp12
    {
        public class Function1
        {
            [FunctionName("Function1")]
            [return: EventHub("receive",Connection ="hubconn")]
            public static string Run([ServiceBusTrigger("subtopic", "testsub", Connection = "conn")]string mySbMsg, ILogger log)
            {
                log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
                return mySbMsg;
           
            }
        }
    }
    

    INPUT:

    Hello Vivek
    

    OUTPUT: