Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 70 additions & 10 deletions docs/SetupGuide_DotnetCSharpScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- [Single Row](#single-row)
- [Sample with multiple Bindings](#sample-with-multiple-bindings)
- [Trigger Binding](#trigger-binding)
- [function.json Properties for Trigger Bindings](#functionjson-properties-for-trigger-bindings)
- [Setup for Trigger Bindings](#setup-for-trigger-bindings)

## CSharp Scripting

Expand Down Expand Up @@ -131,19 +133,19 @@ The database scripts used for the following samples can be found [here](https://

#### Query String

See the [GetProducts](https://github.com/Azure/azure-functions-sql-extension/blob/main/samples/samples-csx/InputBindingSamples/GetProducts) sample
See the [GetProducts](https://github.com/Azure/azure-functions-sql-extension/blob/main/samples/samples-csx/GetProducts) sample

#### Empty Parameter Value

See the [GetProductsNameEmpty](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/InputBindingSamples/GetProductsNameEmpty) sample
See the [GetProductsNameEmpty](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/GetProductsNameEmpty) sample

#### Null Parameter Value

See the [GetProductsNameNull](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/InputBindingSamples/GetProductsNameNull) sample
See the [GetProductsNameNull](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/GetProductsNameNull) sample

#### Stored Procedure

See the [GetProductsStoredProcedure](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/InputBindingSamples/GetProductsStoredProcedure) sample
See the [GetProductsStoredProcedure](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/GetProductsStoredProcedure) sample

## Output Binding

Expand Down Expand Up @@ -173,14 +175,13 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
```csharp
#load "employee.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static Product Run(HttpRequest req, ILogger log, [Sql("dbo.Employees", "SqlConnectionString")] out Employee employee)
public static Product Run(HttpRequest req, ILogger log, out Employee employee)
{
log.LogInformation("CSX HTTP trigger function processed a request.");

Expand Down Expand Up @@ -219,17 +220,76 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a

#### Array

See the [AddProductsArray](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/OutputBindingSamples/AddProductsArray) sample
See the [AddProductsArray](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/AddProductsArray) sample

#### Single Row

See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/OutputBindingSamples/AddProduct) sample
See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/AddProduct) sample

### Sample with multiple Bindings

See the [GetAndAddProducts](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/InputBindingSamples/GetAndAddProducts) sample
See the [GetAndAddProducts](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/GetAndAddProducts) sample


## 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.

### function.json Properties for Trigger Bindings

The following table explains the binding configuration properties that you set in the *function.json* file.

|function.json property | Description|
|---------|----------------------|
|**type** | Required. Must be set to `sqlTrigger`.|
|**direction** | Required. Must be set to `in`. |
|**name** | Required. The name of the variable that represents the set of changes that triggered the function code. Must be set to `changes`. |
| **tableName** | Required. The name of the table to be monitored for changes. |
| **connectionStringSetting** | Required. The name of an 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). |

### 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).

- Open your app in VS Code
- Press 'F1' and search for 'Azure Functions: Create Function'
- Choose HttpTrigger -> (Provide a function name) -> anonymous
- In the folder that is created with the provided function name, open the file (`run.csx`), replace its contents block with the below code. Note that the casing of the Object field names and the table column names must match.

```csharp
#load "Product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Sql;

public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log)
{
log.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes));
}
```

- We also need to add the SQL trigger binding for the Products table by defining `bindings.changes` property. Open the function.json file.
- Paste the below in the file as an additional entry to the "bindings": [] array.

```json
{
"name": "changes",
"type": "sqlTrigger",
"direction": "in",
"tableName": "dbo.Products",
"connectionStringSetting": "SqlConnectionString"
}
```

*In the above, "dbo.Products" is the name of the table our trigger binding is triggered on. The line below is similar to the input binding and specifies where our SqlConnectionString is. For more information on this, see the [function.json Properties for Trigger Bindings](#functionjson-properties-for-trigger-bindings) section*

- Open the local.settings.json file, and in the brackets for "Values," verify there is a 'SqlConnectionString.' If not, add it.
- Hit 'F5' to run your code.
- 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!
18 changes: 18 additions & 0 deletions samples/samples-csx/AddProduct/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static Product Run(HttpRequest req, ILogger log, out Product product)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
product = JsonConvert.DeserializeObject<Product>(requestBody);

return product;
}
22 changes: 22 additions & 0 deletions samples/samples-csx/AddProductParams/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static Product Run(HttpRequest req, ILogger log, out Product product)
{
product = new Product
{
Name = req.Query["name"],
ProductId = int.Parse(req.Query["productId"]),
Cost = int.Parse(req.Query["cost"])
};

return product;
}
18 changes: 18 additions & 0 deletions samples/samples-csx/AddProductWithDefaultPK/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static ProductWithDefaultPK Run(HttpRequest req, ILogger log, out ProductWithDefaultPK product)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
product = JsonConvert.DeserializeObject<ProductWithDefaultPK>(requestBody);

return product;
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static ProductWithoutId Run(HttpRequest req, ILogger log, [Sql("dbo.Products", "SqlConnectionString")] out ProductWithoutId product)
public static ProductWithoutId Run(HttpRequest req, ILogger log, out ProductWithoutId product)
{
log.LogInformation("C# HTTP trigger function processed a request.");

product = new ProductWithoutId
{
Name = req.Query["name"],
Cost = int.Parse(req.Query["cost"])
};

string responseMessage = string.IsNullOrEmpty(product.Name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {product.Name}. This HTTP triggered function executed successfully.";

return product;
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static ProductWithOptionalId Run(HttpRequest req, ILogger log, [Sql("dbo.ProductsWithIdentity", "SqlConnectionString")] out ProductWithOptionalId product)
public static ProductWithOptionalId Run(HttpRequest req, ILogger log, out ProductWithOptionalId product)
{
log.LogInformation("C# HTTP trigger function processed a request.");

product = product = new ProductWithOptionalId
{
Name = req.Query["name"],
ProductId = string.IsNullOrEmpty(req.Query["productId"]) ? (int?)null : int.Parse(req.Query["productId"]),
Cost = int.Parse(req.Query["cost"])
};

string responseMessage = string.IsNullOrEmpty(product.Name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {product.Name}. This HTTP triggered function executed successfully.";

return product;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static MultiplePrimaryKeyProductWithoutId Run(HttpRequest req, ILogger log, out MultiplePrimaryKeyProductWithoutId product)
{
product = product = new MultiplePrimaryKeyProductWithoutId
{
ExternalId = int.Parse(req.Query["externalId"]),
Name = req.Query["name"],
Cost = int.Parse(req.Query["cost"])
};

return product;
}
18 changes: 18 additions & 0 deletions samples/samples-csx/AddProductsArray/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static Product[] Run(HttpRequest req, ILogger log, out Product[] products)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
products = JsonConvert.DeserializeObject<Product[]>(requestBody);

return products;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

public static ICollector<Product> Run(HttpRequest req, ILogger log, [Sql("dbo.Products", "SqlConnectionString")] ICollector<Product> products)
public static ICollector<Product> Run(HttpRequest req, ILogger log, ICollector<Product> products)
{
log.LogInformation("C# HTTP trigger function processed a request.");

List<Product> newProducts = ProductUtilities.GetNewProducts(5000);
foreach (Product product in newProducts)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static ProductWithoutId[] Run(HttpRequest req, ILogger log, [Sql("dbo.Products", "SqlConnectionString")] out ProductWithoutId[] products)
public static ProductWithoutId[] Run(HttpRequest req, ILogger log, out ProductWithoutId[] products)
{
log.LogInformation("C# HTTP trigger function processed a request.");

products = new[]
{
new ProductWithoutId
Expand All @@ -28,9 +25,5 @@ public static ProductWithoutId[] Run(HttpRequest req, ILogger log, [Sql("dbo.Pro
}
};

string responseMessage = products.Length > 0
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: "No data passed, Please pass the objects to upsert in the request body.";

return products;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Sql"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Collections.Generic;

public static Product[] Run(HttpRequest req, ILogger log, IEnumerable<Product> products, [Sql("dbo.ProductsWithIdentity", "SqlConnectionString")] out Product[] productsWithIdentity)
public static Product[] Run(HttpRequest req, ILogger log, IEnumerable<Product> products, out Product[] productsWithIdentity)
{
log.LogInformation("C# HTTP trigger function processed a request.");
productsWithIdentity = products.ToArray<Product>();
return productsWithIdentity;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

#load "../../Common/product.csx"
#load "../Common/product.csx"
#r "Newtonsoft.Json"

using System.Net;
Expand All @@ -12,6 +12,5 @@ using System.Collections.Generic;

public static IActionResult Run(HttpRequest req, ILogger log, IEnumerable<ProductName> products)
{
log.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult(products);
}
Loading