What is the best way to convert console app to Azure trigger

Viewed 3362

I have a console application that runs and monitors an Azure service bus queue. The function I have accepts a Message class and processes it. The app registers the function.

Now I need to convert it to run when a message comes into the message bus queue.

All of the examples I see show a static class with a static method with some parameters being decorated by attributes. It shows the message being sent as being a string.

But my method has a Message class being sent to it.

If I want to use that same app as the azure function app, what should I do?

2 Answers

Reuse the logic of the old app.

You can't, of course, use the console app itself as your Azure Functions app ... Azure Functions is its own runtime & execution framework. That said, you can probably reuse most of the logic from your console app with surprisingly few tweaks.

Since you say you are using the Message class, I assume you are using the .NET Core ServiceBus libraries. If so, you should use the Azure Functions v2 runtime. If you are using the older, full framework ServiceBus library with the BrokeredMessage class, you'll want to stick to the Functions v1 runtime.

The "bindings" in Azure Functions are pretty flexible and often know how to supply what you need. You are probably seeing examples like this:

[FunctionName("NewSbMessageArrivedFunction")]                    
public static void Run(
    [ServiceBusTrigger("someQueue")] string queueMessage, TraceWriter log)
{
    ...
}

You can actually just change the queueMessage argument in this example from a string to a Message (or a BrokeredMessage in v1), and the runtime will see the different signature and should populate your message object for you!

For more details on how this works and what other properties you can bind to for the ServiceBus trigger, check out the documentation on Azure Functions ServiceBus Bindings.

Related