Search code examples
c#azureazure-webjobsazure-webjobssdk

How do I have an Async function that writes out to a service bus queue?


Using the Azure WebJobs SDK, I want to create an async function that will receive ServiceBus queue input and write to a ServiceBus queue output. Async methods cannot have out parameters which, for examples on BlobStorage, appears to be worked around by having Streams and TextWriters instead. However, when I try to do the same with a ServiceBus parameter I receive an exception.

public static async void Transform(
    [ServiceBusTrigger("%InputQueue%")] String input,
    [ServiceBus("%OutputQueue%")] TextWriter output,
    TextWriter log)

Error indexing method 'FilterCurrentCpesToNewCpes'

Can't bind ServiceBus to type 'System.IO.TextWriter'.

I receive a similar message for Stream.


Solution

  • Since Async functions cannot have out parameters, you can bind to ICollector<T> or IAsyncCollector<T> and perform Add() operation to send a message. ICollector is defined in the WebJobs SDK.

    Following sample demonstrates this.

     public static async void Transform(
    [ServiceBusTrigger("%InputQueue%")] string input,
    [ServiceBus("%OutputQueue%")] IAsyncCollector<string> output,
    TextWriter log)
        {            
            await output.AddAsync(input);
        }