Skip to content

Commit 0edfaf8

Browse files
Merge pull request #833 from Azure/maddy/mergeCsxCleanUp
merge csx clean up
2 parents 0a183fa + 05bfbd1 commit 0edfaf8

File tree

55 files changed

+206
-276
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+206
-276
lines changed

docs/SetupGuide_DotnetCSharpScript.md

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- [Single Row](#single-row)
2323
- [Sample with multiple Bindings](#sample-with-multiple-bindings)
2424
- [Trigger Binding](#trigger-binding)
25+
- [function.json Properties for Trigger Bindings](#functionjson-properties-for-trigger-bindings)
26+
- [Setup for Trigger Bindings](#setup-for-trigger-bindings)
2527

2628
## CSharp Scripting
2729

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

132134
#### Query String
133135

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

136138
#### Empty Parameter Value
137139

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

140142
#### Null Parameter Value
141143

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

144146
#### Stored Procedure
145147

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

148150
## Output Binding
149151

@@ -173,14 +175,13 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
173175
```csharp
174176
#load "employee.csx"
175177
#r "Newtonsoft.Json"
176-
#r "Microsoft.Azure.WebJobs.Extensions.Sql"
177178
178179
using System.Net;
179180
using Microsoft.AspNetCore.Mvc;
180181
using Microsoft.Extensions.Primitives;
181182
using Newtonsoft.Json;
182183
183-
public static Product Run(HttpRequest req, ILogger log, [Sql("dbo.Employees", "SqlConnectionString")] out Employee employee)
184+
public static Product Run(HttpRequest req, ILogger log, out Employee employee)
184185
{
185186
log.LogInformation("CSX HTTP trigger function processed a request.");
186187
@@ -219,17 +220,76 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
219220

220221
#### Array
221222

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

224225
#### Single Row
225226

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

228229
### Sample with multiple Bindings
229230

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

232233

233234
## Trigger Binding
234235

235-
> Trigger binding support is only available for in-proc C# functions at present.
236+
See [Trigger Binding Overview](./BindingsOverview.md#trigger-binding) for general information about the Azure SQL Trigger binding.
237+
238+
### function.json Properties for Trigger Bindings
239+
240+
The following table explains the binding configuration properties that you set in the *function.json* file.
241+
242+
|function.json property | Description|
243+
|---------|----------------------|
244+
|**type** | Required. Must be set to `sqlTrigger`.|
245+
|**direction** | Required. Must be set to `in`. |
246+
|**name** | Required. The name of the variable that represents the set of changes that triggered the function code. Must be set to `changes`. |
247+
| **tableName** | Required. The name of the table to be monitored for changes. |
248+
| **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). |
249+
250+
### Setup for Trigger Bindings
251+
252+
Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server).
253+
254+
- Open your app in VS Code
255+
- Press 'F1' and search for 'Azure Functions: Create Function'
256+
- Choose HttpTrigger -> (Provide a function name) -> anonymous
257+
- 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.
258+
259+
```csharp
260+
#load "Product.csx"
261+
#r "Newtonsoft.Json"
262+
#r "Microsoft.Azure.WebJobs.Extensions.Sql"
263+
264+
using System.Net;
265+
using Microsoft.AspNetCore.Mvc;
266+
using Microsoft.Extensions.Primitives;
267+
using Newtonsoft.Json;
268+
using Microsoft.Azure.WebJobs.Extensions.Sql;
269+
270+
public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log)
271+
{
272+
log.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes));
273+
}
274+
```
275+
276+
- We also need to add the SQL trigger binding for the Products table by defining `bindings.changes` property. Open the function.json file.
277+
- Paste the below in the file as an additional entry to the "bindings": [] array.
278+
279+
```json
280+
{
281+
"name": "changes",
282+
"type": "sqlTrigger",
283+
"direction": "in",
284+
"tableName": "dbo.Products",
285+
"connectionStringSetting": "SqlConnectionString"
286+
}
287+
```
288+
289+
*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*
290+
291+
- Open the local.settings.json file, and in the brackets for "Values," verify there is a 'SqlConnectionString.' If not, add it.
292+
- Hit 'F5' to run your code.
293+
- Update, insert, or delete rows in your SQL table while the function app is running and observe the function logs.
294+
- You should see the new log messages in the Visual Studio Code terminal containing the values of row-columns after the update operation.
295+
- Congratulations! You have successfully created your first SQL trigger binding!

samples/samples-csx/OutputBindingSamples/AddProduct/function.json renamed to samples/samples-csx/AddProduct/function.json

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
#load "../Common/product.csx"
5+
#r "Newtonsoft.Json"
6+
7+
using System.Net;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Primitives;
10+
using Newtonsoft.Json;
11+
12+
public static Product Run(HttpRequest req, ILogger log, out Product product)
13+
{
14+
string requestBody = new StreamReader(req.Body).ReadToEnd();
15+
product = JsonConvert.DeserializeObject<Product>(requestBody);
16+
17+
return product;
18+
}

samples/samples-csx/OutputBindingSamples/AddProductParams/function.json renamed to samples/samples-csx/AddProductParams/function.json

File renamed without changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
#load "../Common/product.csx"
5+
#r "Newtonsoft.Json"
6+
7+
using System.Net;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Primitives;
10+
using Newtonsoft.Json;
11+
12+
public static Product Run(HttpRequest req, ILogger log, out Product product)
13+
{
14+
product = new Product
15+
{
16+
Name = req.Query["name"],
17+
ProductId = int.Parse(req.Query["productId"]),
18+
Cost = int.Parse(req.Query["cost"])
19+
};
20+
21+
return product;
22+
}

samples/samples-csx/OutputBindingSamples/AddProductWithDefaultPK/function.json renamed to samples/samples-csx/AddProductWithDefaultPK/function.json

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
#load "../Common/product.csx"
5+
#r "Newtonsoft.Json"
6+
7+
using System.Net;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Primitives;
10+
using Newtonsoft.Json;
11+
12+
public static ProductWithDefaultPK Run(HttpRequest req, ILogger log, out ProductWithDefaultPK product)
13+
{
14+
string requestBody = new StreamReader(req.Body).ReadToEnd();
15+
product = JsonConvert.DeserializeObject<ProductWithDefaultPK>(requestBody);
16+
17+
return product;
18+
}

samples/samples-csx/OutputBindingSamples/AddProductWithIdentityColumn/function.json renamed to samples/samples-csx/AddProductWithIdentityColumn/function.json

File renamed without changes.

samples/samples-csx/OutputBindingSamples/AddProductWithIdentityColumn/run.csx renamed to samples/samples-csx/AddProductWithIdentityColumn/run.csx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
#load "../../Common/product.csx"
4+
#load "../Common/product.csx"
55
#r "Newtonsoft.Json"
6-
#r "Microsoft.Azure.WebJobs.Extensions.Sql"
76

87
using System.Net;
98
using Microsoft.AspNetCore.Mvc;
109
using Microsoft.Extensions.Primitives;
1110
using Newtonsoft.Json;
1211

13-
public static ProductWithoutId Run(HttpRequest req, ILogger log, [Sql("dbo.Products", "SqlConnectionString")] out ProductWithoutId product)
12+
public static ProductWithoutId Run(HttpRequest req, ILogger log, out ProductWithoutId product)
1413
{
15-
log.LogInformation("C# HTTP trigger function processed a request.");
16-
1714
product = new ProductWithoutId
1815
{
1916
Name = req.Query["name"],
2017
Cost = int.Parse(req.Query["cost"])
2118
};
2219

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

samples/samples-csx/OutputBindingSamples/AddProductWithIdentityColumnIncluded/function.json renamed to samples/samples-csx/AddProductWithIdentityColumnIncluded/function.json

File renamed without changes.

0 commit comments

Comments
 (0)