1

I'm starting to develop an Azure Function that sends messages to an Event Hub.

Using the example provided on Azure Event Hubs output binding for Azure Functions I'm able to send an event using the return value of the HTTP trigger.

[FunctionName("EventHubOutput")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
  log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
  return $"{DateTime.Now}";
}

For my implementation, I don't want to use the return value of the function trigger (in my case HTTP) and would like to use the output of a method within the function. The reason for this is that I would like to validate the request method and only send the message to the event hub is it passes validation.

I can replace return: with method: but I can't find any documentation which tells me how to implement method:.

5
  • I'm not sure I follow what you want to accomplish, if you want to use the output of a method, wouldn't it be enough to call the method within the function and return its value? Commented May 16, 2020 at 6:35
  • What I’m trying to accomplish is a HTTP trigger function which returns a HTTP status only. Within the function I want to validate the received request payload. If it passes validation, the payload will be sent to the Event Hub and the trigger returns a success status. If it fails validation the message is not sent to the Event Hub and the function returns an error status. At the moment, the return: is the return value for the HTTP trigger which will Always send a message to the Event Hub and I don’t want that. I want to return the output from the validation method. Commented May 16, 2020 at 14:07
  • Then do you really need the output binding? Maybe remove that binding and just send the message to the EventHub with the EventHubClient, kind of what this example does: learn.microsoft.com/en-us/azure/event-hubs/… Commented May 16, 2020 at 14:23
  • As devcrp says, we can use EventHubClient to send message to eventhub directly. It is as same as output binding. Commented May 17, 2020 at 11:47
  • If you dont have more doubts, can we end this question now?:) Commented May 21, 2020 at 9:47

2 Answers 2

4

If you don't want to use output binding, you can use code to send directly, they are the same principle. Code about how to send a message to the event hub:

https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send#write-code-to-send-messages-to-the-event-hub

Sign up to request clarification or add additional context in comments.

2 Comments

That link is pointing to guidance for the legacy version of Event Hubs; you'd probably want to use the current version: learn.microsoft.com/en-us/azure/event-hubs/…
While this does not answer the original question, it will give me what I need and I already have some working code that uses this method, so accepting it as an answer.
0

Insert the following into your header for your Azure Function:

[EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]IAsyncCollector<[your type here]> eventsToSend

You can now use the following to queue messages for transmission in a threadsafe manner (i.e. it works just fine if you have a series of threads queuing to it at the same time):

await eventsToSend.AddAsync([Your Object]).ConfigureAwait(false);

If you have nothing to send out, you do not call the .AddAsync() method in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.