Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Azure Function with multiple output to a service bus

I saw the Microsoft documentation for Azure Service Bus output binding for Azure Functions version 3. When I want to send a message to a service bus as return of the function, I can use this code:

[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
{
    log.LogInformation($"C# function processed: {input.Text}");
    return input.Text;
}

My problem started when I want to have as an output 2 messages to different Service Bus. Is it possible to output binding more than one output? In the online editor you can add more than one output. How can I do that in the code?

In the documentation there is a section of Usage, it explains what I can use as output binding. They mention ICollector<T> or IAsyncCollector<T> but I'm not sure it is what I'm looking for.

Other question is what happens in an API that returns one value to the bus and another to the user?

like image 232
Enrico Rossini Avatar asked May 04 '20 12:05

Enrico Rossini


1 Answers

You could have two IAsyncCollector<T> output bindings:

[FunctionName("HttpTriggeredFunction")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [ServiceBus("queuename1", Connection = "ServiceBusConnectionString1")] IAsyncCollector<dynamic> outputServiceBus1,
    [ServiceBus("queuename2", Connection = "ServiceBusConnectionString2")] IAsyncCollector<dynamic> outputServiceBus2,
    ILogger log)
{
    await outputServiceBus1.AddAsync("Item1");

    await outputServiceBus2.AddAsync("Item2");

    return new OkObjectResult(null);
}

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "ServiceBusConnectionString1": "Endpoint=sb://sb1.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=",
        "ServiceBusConnectionString2": "Endpoint=sb://sb2.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey="
    }
}

Other question is what happens in an API that returns one value to the bus and another to the user?

In the above example, it is returning (adding) a message to the Service Bus and returning a IActionResult (HTTP response) to the user.

like image 69
Chris B Avatar answered Nov 12 '22 04:11

Chris B