Joakim
Joakim

Reputation: 55

Create Azure EventHub Message in Azure Functions

I'm trying to do some Proof of Concept with EventHub and Azure Functions. I have a Generic Webhook Function in C# that I want to pass a message to my EventHub.

I get stuck on the parameter name given on the "Integrate" tab. If I declare that name among the parameters I have to give it a type. I can't figure out what kind of type though... I've tried:

I can't get it to work. If I don't do this I get the error message: "Missing binding argument named 'outputEventHubMessage'"

If I give it the wrong type I get the message: "Error indexing method 'Functions.GenericWebhookCSharp1'. Microsoft.Azure.WebJobs.Host: Can't bind to parameter."

I'm probably a bit lost in documentation or just a bit tired, but I'd appreciate any help here!

/Joakim

Upvotes: 2

Views: 1660

Answers (1)

mathewc
mathewc

Reputation: 13568

Likely you're just missing the out keyword on your parameter. Below is a working WebHook function that declares an out string message parameter that is mapped to the EventHub output, and writes an EventHub message via message = "Test Message".

Because async functions can't return out parameters, I made this function synchronous (returning an object rather than Task<object>). If you want to remain async, rather than use an out parameter, you can instead bind to an IAsyncCollector<string> parameter. You can then enqueue one or more messages by calling the AddAsync method on the collector.

More details on the EventHub binding and the types it supports can be found here. Note that the other bindings follow the same general patterns.

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;

public static object Run(HttpRequestMessage req, out string message, TraceWriter log)
{
    string jsonContent = req.Content.ReadAsStringAsync().Result;
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    log.Info($"Webhook was triggered! Name = {data.first}");

    message = "Test Message";

    var res = req.CreateResponse(HttpStatusCode.OK, new {
        greeting = $"Hello {data.first} {data.last}!"
    });

    return res;
}

Upvotes: 3

Related Questions