Skip to content

.NET Worker bindings

Anthony Chu edited this page Aug 25, 2021 · 4 revisions

With the .NET worker, we have migrated to a new binding model. In this model, no explicit Microsoft.Azure.WebJobs references are required.

The attributes required to create Functions, Triggers, and Bindings are now available in these Nuget packages --

  • Common types (FunctionAttribute): Microsoft.Azure.Functions.Worker.Extensions.Abstractions
  • Http bindings: Microsoft.Azure.Functions.Worker.Extensions.Http
  • Storage (Queue, Blob, Table) bindings: Microsoft.Azure.Functions.Worker.Extensions.Storage
  • Timer bindings: Microsoft.Azure.Functions.Worker.Extensions.Timer
  • Event Hubs bindings: Microsoft.Azure.Functions.Worker.Extensions.EventHubs
  • Event Grid bindings: Microsoft.Azure.Functions.Worker.Extensions.EventGrid
  • Service Bus bindings: Microsoft.Azure.Functions.Worker.Extensions.ServiceBus
  • Cosmos DB bindings: Microsoft.Azure.Functions.Worker.Extensions.CosmosDB
  • RabbitMQ bindings: Microsoft.Azure.Functions.Worker.Extensions.RabbitMQ
  • SignalR Service bindings: Microsoft.Azure.Functions.Worker.Extensions.SignalRService
  • Kafka bindings: Microsoft.Azure.Functions.Worker.Extensions.Kafka
  • Warmup trigger: Microsoft.Azure.Functions.Worker.Extensions.Warmup

Samples

You can find samples using all of our new binding types under samples/SampleApp (link)

Output Bindings

In the new model, we have change how output bindings work. You can now specify output bindings in two ways:

Using Method Attributes (works if you only have one output binding)

In this model, the function return value is treated as the value for the Output Binding. An example --

public static class EventHubsFunction
{
    [Function("EventHubsFunction")]
    [EventHubOutput("MyEventHubName", Connection = "EventHubConnectionAppSetting")]
    public static string Run([EventHubTrigger("src", Connection = "EventHubConnectionAppSetting")] string input,
        FunctionContext context)
    {
        var logger = context.GetLogger("EventHubsFunction");

        logger.LogInformation(input);

        var message = $"Output message created at {DateTime.Now}";
        return message;
    }
}

Using Property Attributes (works with any number of output bindings)

In this model, the function return type can specify output bindings using property attributes.

/// <summary>
/// This class specifies output bindings in the properties of <see cref="MyOutputType"/>.
/// <see cref="MyOutputType"/> defines a Queue output binding, and an Http Response property.
/// By default, a property of type <see cref="HttpResponseData"/> in the return type of the function
/// is treated as an Http output binding. This property can be used to provide a response to the Http trigger.
/// </summary>
public static class MultiOutput
{
    [Function("MultiOutput")]
    public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
        FunctionContext context)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.WriteString("Success!");

        string myQueueOutput = "some output";

        return new MyOutputType()
        {
            Name = myQueueOutput,
            HttpReponse = response
        };
    }
}

public class MyOutputType
{
    [QueueOutput("myQueue")]
    public string Name { get; set; }

    public HttpResponseData HttpReponse { get; set; }
}
Clone this wiki locally