I've created an Azure Function that is triggered any time a new message is added to an Azure ServiceBus queue. With this code it works fine:
#r "Newtonsoft.Json"
#load "..\shared\person.csx"
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public static void Run(string message, TraceWriter log)
{
var person = JsonConvert.DeserializeObject<Person>(message,
new JsonSerializerSettings() {ContractResolver = new CamelCasePropertyNamesContractResolver()});
log.Verbose($"From DeserializeObject: {person.FirstName} {person.LastName}");
}
I've seen that I can also bind the message to a POCO like that:
public static void Run(Person message, TraceWriter log)
{
log.Verbose($"From DeserializeObject: {message.FirstName} {message.LastName}");
}
Now I would like to bind the message to a BrokeredMessage
because I need to have access to the properties of the message.
Right-click on the project and click on project -> Click on Add -> Select New Azure Function. Select Azure function and give it a name and click on Add button. Now select Service Bus Queue Trigger and specify queue name and click on Add button. That's it -- our Service Bus trigger function is created.
Edit New SDK supports the servicebus sdk using #r directive
#r "Microsoft.ServiceBus"
using Microsoft.ServiceBus.Messaging;
public static void Run(BrokeredMessage msg, TraceWriter log)
{
log.Info($"C# ServiceBus queue trigger function processed message: {msg}");
}
Old version
Only two steps:
I've create a project.json
file to add a reference to the WindowsAzure.ServiceBus
Nuget package (see SO Post):
{
"frameworks": {
"net46":{
"dependencies": {
"WindowsAzure.ServiceBus": "2.7.6"
}
}
}
}
I've added a reference to the brokered message:
using Microsoft.ServiceBus.Messaging;
public static void Run(BrokeredMessage message, TraceWriter log)
{
log.Verbose("Function has been triggered !!!");
}
I tried Thomas' solution and it seems that this does not work anymore.
The documentation states:
In addition, the following assemblies are special cased and may be referenced by simplename (e.g. #r "AssemblyName"):
- ...
- Microsoft.ServiceBus
So without touching the project.json file the following works:
#r "Microsoft.ServiceBus"
using Microsoft.ServiceBus.Messaging;
public static void Run(BrokeredMessage msg, TraceWriter log)
{
log.Info($"C# ServiceBus queue trigger function processed message: {msg}");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With