Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
When you create a function, language-specific trigger code is added in your project from a set of trigger templates. If you want to connect your function to other services by using input or output bindings, you have to add specific binding definitions in your function. To learn more about bindings, see Azure Functions triggers and bindings concepts.
Local development
When you develop functions locally, you need to update the function code to add bindings. For languages that use function.json, Visual Studio Code provides tooling to add bindings to a function.
Manually add bindings based on examples
When adding a binding to an existing function, you need to add binding-specific attributes to the function definition in code.
When adding a binding to an existing function, you need to add binding-specific annotations to the function definition in code.
When adding a binding to an existing function, you need to update the function code and add a definition to the function.json configuration file.
When adding a binding to an existing function, you need update the function definition, depending on your model:
In Go, you configure supported triggers by using the fluent registration API in your main() function. Each trigger type has a dedicated registration method with functional options for configuration. No separate binding configuration file is needed.
The following example shows an HTTP triggered function. If you need to write to Queue Storage from a Go function, use the Azure SDK for Go directly because Queue Storage output bindings aren't currently supported by the Go worker:
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/azure/azure-functions-golang-worker/sdk"
"github.com/azure/azure-functions-golang-worker/worker"
)
func main() {
app := sdk.FunctionApp()
app.HTTP("HttpExample", httpHandler,
sdk.WithMethods("GET", "POST"),
sdk.WithAuth("anonymous"),
)
worker.Start(app)
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
var body struct{ Name string }
json.NewDecoder(r.Body).Decode(&body)
name = body.Name
}
if name == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Please pass a name on the query string or in the request body.")
return
}
// Queue output bindings are not yet supported in the Go worker.
// Use the Azure SDK for Go to write to Queue Storage directly.
fmt.Fprintf(w, "Hello, %s!", name)
}
Note
The Go worker currently supports triggers only. Input and output bindings, such as Queue Storage output bindings, aren't yet available. Use the Azure SDK for Go to interact with other Azure services directly from your function code.
The Go worker currently supports the following trigger types:
| Trigger type | Registration method | Examples |
|---|---|---|
| HTTP | app.HTTP() |
HTTP samples |
| Timer | app.Timer() |
Timer samples |
| Azure Cosmos DB | app.CosmosDB() |
Cosmos DB samples |
| Azure Service Bus (Queue) | app.ServiceBusQueue() |
Service Bus queue samples |
| Azure Service Bus (Topic) | app.ServiceBusTopic() |
Service Bus topic samples |
| Event Hubs | app.EventHub() |
Event Hubs samples |
| Event Grid | app.EventGrid() |
Event Grid samples |
| Blob Storage | app.Blob() |
Blob samples |
For more information, see the Go developer reference.
The following example shows the function definition after adding a Queue Storage output binding to an HTTP triggered function:
Because an HTTP triggered function also returns an HTTP response, the function returns a MultiResponse object, which represents both the HTTP and queue output.
[Function("HttpExample")]
public MultiResponse Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
This example is the definition of the MultiResponse object that includes the output binding:
public class MultiResponse
{
[QueueOutput("outqueue",Connection = "AzureWebJobsStorage")]
public string[] Messages { get; set; }
public IActionResult HttpResponse { get; set; }
}
This example uses ASP.NET Core integration. If you aren't using ASP.NET Core integration, you need to change HttpRequest to HttpRequestData and IActionResult to HttpResponseData.
Messages are sent to the queue when the function completes. The way you define the output binding depends on your process model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@QueueOutput(name = "msg", queueName = "outqueue",
connection = "AzureWebJobsStorage") OutputBinding<String> msg,
final ExecutionContext context) {
For more information, including links to example binding code that you can refer to, see Add bindings to a function.
const { app, output } = require('@azure/functions');
const sendToQueue = output.storageQueue({
queueName: 'outqueue',
connection: 'AzureWebJobsStorage',
});
app.http('HttpExample', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
extraOutputs: [sendToQueue],
handler: async (request, context) => {
try {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || (await request.text());
context.log(`Name: ${name}`);
if (name) {
const msg = `Name passed to the function ${name}`;
context.extraOutputs.set(sendToQueue, [msg]);
return { body: msg };
} else {
context.log('Missing required data');
return { status: 404, body: 'Missing required data' };
}
} catch (error) {
context.log(`Error: ${error}`);
return { status: 500, body: 'Internal Server Error' };
}
},
});
The way you define the output binding depends on the version of your Node.js model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.
$outputMsg = $name
Push-OutputBinding -name msg -Value $outputMsg
For more information, including links to example binding code that you can refer to, see Add bindings to a function.
@app.route(route="HttpExample")
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
def HttpExample(req: func.HttpRequest, msg: func.Out [func.QueueMessage]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
The way you define the output binding depends on the version of your Python model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.
import {
app,
output,
HttpRequest,
HttpResponseInit,
InvocationContext,
StorageQueueOutput,
} from '@azure/functions';
const sendToQueue: StorageQueueOutput = output.storageQueue({
queueName: 'outqueue',
connection: 'AzureWebJobsStorage',
});
export async function HttpExample(
request: HttpRequest,
context: InvocationContext,
): Promise<HttpResponseInit> {
try {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || (await request.text());
context.log(`Name: ${name}`);
if (name) {
const msg = `Name passed to the function ${name}`;
context.extraOutputs.set(sendToQueue, [msg]);
return { body: msg };
} else {
context.log('Missing required data');
return { status: 404, body: 'Missing required data' };
}
} catch (error) {
context.log(`Error: ${error}`);
return { status: 500, body: 'Internal Server Error' };
}
}
app.http('HttpExample', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: HttpExample,
});
The way you define the output binding depends on the version of your Node.js model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.
Use the following table to find examples of specific binding types that you can use to guide you in updating an existing function. First, choose the language tab that corresponds to your project.
Binding code for C# depends on the specific process model.
| Service | Examples | Samples |
|---|---|---|
| Blob Storage | Trigger Input Output |
Link |
| Azure Cosmos DB | Trigger Input Output |
Link |
| Azure Data Explorer | Input Output |
Link |
| Azure SQL | Trigger Input Output |
Link |
| Event Grid | Trigger Output |
Link |
| Event Hubs | Trigger Output |
|
| IoT Hub | Trigger Output |
|
| HTTP | Trigger | Link |
| Queue Storage | Trigger Output |
Link |
| RabbitMQ | Trigger Output |
|
| SendGrid | Output | |
| Service Bus | Trigger Output |
Link |
| Azure SignalR Service | Trigger Input Output |
|
| Table Storage | Input Output |
|
| Timer | Trigger | Link |
| Twilio | Output | Link |
| Service | Examples | Samples |
|---|---|---|
| Blob Storage | Trigger Input Output |
Link |
| Azure Cosmos DB | Trigger Input Output |
Link |
| Azure Data Explorer | Input Output |
Link |
| Azure SQL | Trigger Input Output |
|
| Event Grid | Trigger Output |
Link |
| Event Hubs | Trigger Output |
|
| IoT Hub | Trigger Output |
|
| HTTP | Trigger | Link |
| Queue Storage | Trigger Output |
Link |
| RabbitMQ | Trigger Output |
|
| SendGrid | Output | |
| Service Bus | Trigger Output |
Link |
| Azure SignalR Service | Trigger Input Output |
|
| Table Storage | Input Output |
|
| Timer | Trigger | Link |
| Twilio | Output |
| Service | Examples | Samples |
|---|---|---|
| Blob Storage | Trigger Input Output |
Link |
| Azure Cosmos DB | Trigger Input Output |
Link |
| Azure Data Explorer | Input Output |
|
| Azure SQL | Trigger Input Output |
Link |
| Event Grid | Trigger Output |
|
| Event Hubs | Trigger Output |
|
| IoT Hub | Trigger Output |
|
| HTTP | Trigger | Link |
| Queue Storage | Trigger Output |
Link |
| RabbitMQ | Trigger Output |
|
| SendGrid | Output | |
| Service Bus | Trigger Output |
Link |
| Azure SignalR Service | Trigger Input Output |
|
| Table Storage | Input Output |
|
| Timer | Trigger | |
| Twilio | Output |
| Service | Examples | Samples |
|---|---|---|
| Blob Storage | Trigger Input Output |
|
| Azure Cosmos DB | Trigger Input Output |
|
| Azure SQL | Trigger Input Output |
|
| Event Grid | Trigger Output |
|
| Event Hubs | Trigger Output |
|
| IoT Hub | Trigger Output |
|
| HTTP | Trigger | Link |
| Queue Storage | Trigger Output |
|
| RabbitMQ | Trigger Output |
|
| SendGrid | Output | |
| Service Bus | Trigger Output |
|
| Azure SignalR Service | Trigger Input Output |
|
| Table Storage | Input Output |
|
| Timer | Trigger | |
| Twilio | Output |
Binding code for Python depends on the Python model version.
| Service | Examples | Samples |
|---|---|---|
| Blob Storage | Trigger Input Output |
Link |
| Azure Cosmos DB | Trigger Input Output |
Link |
| Azure Data Explorer | Input Output |
|
| Azure SQL | Trigger Input Output |
Link |
| Event Grid | Trigger Output |
|
| Event Hubs | Trigger Output |
|
| IoT Hub | Trigger Output |
|
| HTTP | Trigger | Link |
| Queue Storage | Trigger Output |
|
| RabbitMQ | Trigger Output |
|
| SendGrid | Output | |
| Service Bus | Trigger Output |
Link |
| Azure SignalR Service | Trigger Input Output |
|
| Table Storage | Input Output |
|
| Timer | Trigger | |
| Twilio | Output |
Visual Studio Code
When you use Visual Studio Code to develop your function and your function uses a function.json file, the Azure Functions extension can automatically add a binding to an existing function.json file. To learn more, see Add input and output bindings.
Azure portal
When you develop your functions in the Azure portal, you add input and output bindings in the Integrate tab for a given function. The new bindings are added to either the function.json file or to the method attributes, depending on your language. The following articles show examples of how to add bindings to an existing function in the portal: