-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Trigger Binding set up in SetupGuide_DotnetOutOfProc.md #845
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,4 +352,60 @@ public static Task<Product> Run( | |
|
||
## Trigger Binding | ||
|
||
> Trigger binding support is only available for in-proc C# functions at present. | ||
See [Trigger Binding Overview](./BindingsOverview.md#trigger-binding) for general information about the Azure SQL Trigger binding. | ||
|
||
### SqlTriggerAttribute | ||
|
||
The SqlAttribute for Trigger bindings takes two [arguments](https://github.com/Azure/azure-functions-sql-extension/blob/release/trigger/Worker.Extensions.Sql/src/SqlTriggerAttribute.cs) | ||
|
||
- **TableName**: Represents the name of the table to be monitored for changes. | ||
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0). | ||
|
||
The trigger binding can bind to type `IReadOnlyList<SqlChange<T>>`: | ||
|
||
- **IReadOnlyList<SqlChange\<T\>>**: If there are multiple rows updated in the SQL table, the user function will get invoked with a batch of changes, where each element is a `SqlChange` object. Here `T` is a generic type-argument that can be substituted with a user-defined POCO, or Plain Old C# Object, representing the user table row. The POCO should therefore follow the schema of the queried table. See the [Query String](#query-string) section for an example of what the POCO should look like. The two properties of class `SqlChange<T>` are `Item` of type `T` which represents the table row and `Operation` of type `SqlChangeOperation` which indicates the kind of row operation (insert, update, or delete) that triggered the user function. | ||
|
||
Note that for insert and update operations, the user function receives POCO object containing the latest values of table columns. For delete operation, only the properties corresponding to the primary keys of the row are populated. | ||
|
||
Any time when the changes happen to the "Products" table, the user function will be invoked with a batch of changes. The changes are processed sequentially, so if there are a large number of changes pending to be processed, the function will be passed a batch containing the earliest changes first. | ||
|
||
### Setup for Trigger Bindings | ||
|
||
Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server), and that you have the 'Employee.cs' file from the [Setup for Input Bindings](#setup-for-input-bindings) section. | ||
|
||
- Create a new file with the following content: | ||
|
||
```csharp | ||
using System.Collections.Generic; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
||
namespace Company.Function | ||
{ | ||
public static class EmployeeTrigger | ||
{ | ||
private static readonly Action<ILogger, string, Exception> _loggerMessage = LoggerMessage.Define<string>(LogLevel.Information, eventId: new EventId(0, "INFO"), formatString: "{Message}"); | ||
|
||
[FunctionName("EmployeeTrigger")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Function("EmployeeTrigger") |
||
public static void Run( | ||
[SqlTrigger("[dbo].[Employees]", "SqlConnectionString")] | ||
IReadOnlyList<SqlChange<Employee>> changes, FunctionContext context) | ||
{ | ||
foreach (SqlChange<Employee> change in changes) | ||
{ | ||
Employee employee = change.Item; | ||
_loggerMessage(context.GetLogger("EmployeeTrigger"), $"Change operation: {change.Operation}", null); | ||
_loggerMessage(context.GetLogger("EmployeeTrigger"), $"EmployeeID: {employee.EmployeeId}, FirstName: {employee.FirstName}, LastName: {employee.LastName}, Company: {employee.Company}, Team: {employee.Team}", null); | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- *Skip these steps if you have not completed the output binding tutorial.* | ||
- Open your output binding file and modify some of the values. For example, change the value of Team column from 'Functions' to 'Azure SQL'. | ||
- Hit 'F5' to run your code. Click the link of the HTTP trigger from the output binding tutorial. | ||
- Update, insert, or delete rows in your SQL table while the function app is running and observe the function logs. | ||
- You should see the new log messages in the Visual Studio Code terminal containing the values of row-columns after the update operation. | ||
- Congratulations! You have successfully created your first SQL trigger binding! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure the TOC is updated as well