Skip to content

Commit f53fb8f

Browse files
updating pivots and examples for .NET
1 parent 1e8257c commit f53fb8f

File tree

3 files changed

+96
-77
lines changed

3 files changed

+96
-77
lines changed

articles/azure-functions/functions-bindings-return-value.md

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ description: Learn to manage return values for Azure Functions
44
ms.topic: reference
55
ms.devlang: csharp, fsharp, java, javascript, powershell, python
66
ms.custom: devx-track-csharp
7-
ms.date: 01/14/2019
7+
ms.date: 07/25/2023
8+
zone_pivot_groups: programming-languages-set-functions-lang-workers
89
---
910

1011
# Using the Azure Function return value
1112

12-
This article explains how return values work inside a function.
13+
This article explains how return values work inside a function. In languages that have a return value, you can bind a function [output binding](./functions-triggers-bindings.md#binding-direction) to the return value.
1314

14-
In languages that have a return value, you can bind a function [output binding](./functions-triggers-bindings.md#binding-direction) to the return value:
15+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
1516

16-
* In a C# class library, apply the output binding attribute to the method return value.
17-
* In Java, apply the output binding annotation to the function method.
18-
* In other languages, set the `name` property in *function.json* to `$return`.
17+
Set the `name` property in *function.json* to `$return`. If there are multiple output bindings, use the return value for only one of them.
1918

20-
If there are multiple output bindings, use the return value for only one of them.
19+
::: zone-end
2120

22-
In C# and C# script, alternative ways to send data to an output binding are `out` parameters and [collector objects](functions-reference-csharp.md#writing-multiple-output-values).
21+
::: zone pivot="programming-language-csharp"
2322

24-
# [C#](#tab/csharp)
23+
How return values are used depends on the C# mode you're using in your function app:
24+
25+
# [In-process](#tab/in-process)
26+
27+
28+
In a C# class library, apply the output binding attribute to the method return value. In C# and C# script, alternative ways to send data to an output binding are `out` parameters and [collector objects](functions-reference-csharp.md#writing-multiple-output-values).
2529

2630
Here's C# code that uses the return value for an output binding, followed by an async example:
2731

@@ -47,62 +51,15 @@ public static Task<string> Run([QueueTrigger("inputqueue")]WorkItem input, ILogg
4751
}
4852
```
4953

50-
# [C# Script](#tab/csharp-script)
51-
52-
Here's the output binding in the *function.json* file:
53-
54-
```json
55-
{
56-
"name": "$return",
57-
"type": "blob",
58-
"direction": "out",
59-
"path": "output-container/{id}"
60-
}
61-
```
62-
63-
Here's the C# script code, followed by an async example:
64-
65-
```cs
66-
public static string Run(WorkItem input, ILogger log)
67-
{
68-
string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
69-
log.LogInformation($"C# script processed queue message. Item={json}");
70-
return json;
71-
}
72-
```
73-
74-
```cs
75-
public static Task<string> Run(WorkItem input, ILogger log)
76-
{
77-
string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
78-
log.LogInformation($"C# script processed queue message. Item={json}");
79-
return Task.FromResult(json);
80-
}
81-
```
82-
83-
# [F#](#tab/fsharp)
54+
# [Isolated process](#tab/isolated-process)
8455

85-
Here's the output binding in the *function.json* file:
56+
See [Output bindings in the .NET worker guide](./dotnet-isolated-process-guide.md#output-bindings) for details and examples.
8657

87-
```json
88-
{
89-
"name": "$return",
90-
"type": "blob",
91-
"direction": "out",
92-
"path": "output-container/{id}"
93-
}
94-
```
95-
96-
Here's the F# code:
58+
---
9759

98-
```fsharp
99-
let Run(input: WorkItem, log: ILogger) =
100-
let json = String.Format("{{ \"id\": \"{0}\" }}", input.Id)
101-
log.LogInformation(sprintf "F# script processed queue message '%s'" json)
102-
json
103-
```
60+
::: zone-end
10461

105-
# [JavaScript](#tab/javascript)
62+
::: zone pivot="programming-language-javascript"
10663

10764
Here's the output binding in the *function.json* file:
10865

@@ -124,7 +81,11 @@ module.exports = function (context, input) {
12481
return json;
12582
}
12683
```
127-
# [PowerShell](#tab/PowerShell)
84+
85+
86+
::: zone-end
87+
88+
::: zone pivot="programming-language-powershell"
12889

12990
Here's the output binding in the *function.json* file:
13091

@@ -146,7 +107,9 @@ Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
146107
})
147108
```
148109

149-
# [Python](#tab/python)
110+
::: zone-end
111+
112+
::: zone pivot="programming-language-python"
150113

151114
Here's the output binding in the *function.json* file:
152115

@@ -169,7 +132,13 @@ def main(input: azure.functions.InputStream) -> str:
169132
})
170133
```
171134

172-
# [Java](#tab/java)
135+
136+
::: zone-end
137+
138+
::: zone pivot="programming-language-java"
139+
140+
Apply the output binding annotation to the function method. If there are multiple output bindings, use the return value for only one of them.
141+
173142

174143
Here's Java code that uses the return value for an output binding:
175144

@@ -187,7 +156,8 @@ public static String run(
187156
}
188157
```
189158

190-
---
159+
::: zone-end
160+
191161

192162
## Next steps
193163

articles/azure-functions/functions-reference-csharp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,36 @@ The `#load` directive works only with *.csx* files, not with *.cs* files.
217217

218218
## Binding to method return value
219219

220-
You can use a method return value for an output binding, by using the name `$return` in *function.json*. For examples, see [Triggers and bindings](./functions-bindings-return-value.md).
220+
You can use a method return value for an output binding, by using the name `$return` in *function.json*.
221+
222+
```json
223+
{
224+
"name": "$return",
225+
"type": "blob",
226+
"direction": "out",
227+
"path": "output-container/{id}"
228+
}
229+
```
230+
231+
Here's the C# script code using the return value, followed by an async example:
232+
233+
```csharp
234+
public static string Run(WorkItem input, ILogger log)
235+
{
236+
string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
237+
log.LogInformation($"C# script processed queue message. Item={json}");
238+
return json;
239+
}
240+
```
241+
242+
```csharp
243+
public static Task<string> Run(WorkItem input, ILogger log)
244+
{
245+
string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
246+
log.LogInformation($"C# script processed queue message. Item={json}");
247+
return Task.FromResult(json);
248+
}
249+
```
221250

222251
Use the return value only if a successful function execution always results in a return value to pass to the output binding. Otherwise, use `ICollector` or `IAsyncCollector`, as shown in the following section.
223252

articles/azure-functions/functions-reference-fsharp.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ F# for Azure Functions is a solution for easily running small pieces of code, or
1818
1919
This article assumes that you've already read the [Azure Functions developer reference](functions-reference.md).
2020

21-
## How .fsx works
21+
## How an F# script works
2222
An `.fsx` file is an F# script. It can be thought of as an F# project that's contained in a single file. The file contains both the code for your program (in this case, your Azure Function) and directives for managing dependencies.
2323

2424
When you use an `.fsx` for an Azure Function, commonly required assemblies are automatically included for you, allowing you to focus on the function rather than "boilerplate" code.
2525

2626
## Folder structure
2727

28-
The folder structure for an F# script project looks like the following:
28+
The folder structure for an F# script project adheres to the following pattern:
2929

3030
```
3131
FunctionsProject
@@ -44,7 +44,7 @@ FunctionsProject
4444

4545
There's a shared [host.json](functions-host-json.md) file that can be used to configure the function app. Each function has its own code file (.fsx) and binding configuration file (function.json).
4646

47-
The binding extensions required in [version 2.x and later versions](functions-versions.md) of the Functions runtime are defined in the `extensions.csproj` file, with the actual library files in the `bin` folder. When developing locally, you must [register binding extensions](./functions-bindings-register.md#extension-bundles). When developing functions in the Azure portal, this registration is done for you.
47+
The binding extensions required in [version 2.x and later versions](functions-versions.md) of the Functions runtime are defined in the `extensions.csproj` file, with the actual library files in the `bin` folder. When you're developing locally, you must [register binding extensions](./functions-bindings-register.md#extension-bundles). When you're developing functions in the Azure portal, this registration is done for you.
4848

4949
## Binding to arguments
5050
Each binding supports some set of arguments, as detailed in the [Azure Functions triggers and bindings developer reference](functions-triggers-bindings.md). For example, one of the argument bindings a blob trigger supports is a POCO, which can be expressed using an F# record. For example:
@@ -85,8 +85,28 @@ let Run(input: string, item: byref<Item>) =
8585
item <- result
8686
```
8787

88+
You can use a method return value for an output binding, by using the name `$return` in *function.json*:
89+
90+
```json
91+
{
92+
"name": "$return",
93+
"type": "blob",
94+
"direction": "out",
95+
"path": "output-container/{id}"
96+
}
97+
```
98+
99+
Here's the F# code that uses the return value:
100+
101+
```fsharp
102+
let Run(input: WorkItem, log: ILogger) =
103+
let json = String.Format("{{ \"id\": \"{0}\" }}", input.Id)
104+
log.LogInformation(sprintf "F# script processed queue message '%s'" json)
105+
json
106+
```
107+
88108
## Logging
89-
To log output to your [streaming logs](../app-service/troubleshoot-diagnostic-logs.md) in F#, your function should take an argument of type [ILogger](/dotnet/api/microsoft.extensions.logging.ilogger). For consistency, we recommend this argument is named `log`. For example:
109+
To log output to your [streaming logs](../app-service/troubleshoot-diagnostic-logs.md) in F#, your function should take an argument of type [`ILogger`](/dotnet/api/microsoft.extensions.logging.ilogger). For consistency, we recommend this argument is named `log`. For example:
90110

91111
```fsharp
92112
let Run(blob: string, output: byref<string>, log: ILogger) =
@@ -167,7 +187,7 @@ The following assemblies are automatically added by the Azure Functions hosting
167187
* `System.Web.Http`
168188
* `System.Net.Http.Formatting`.
169189

170-
In addition, the following assemblies are special cased and may be referenced by simplename (e.g. `#r "AssemblyName"`):
190+
In addition, the following assemblies are special cased and may be referenced by simple name (e.g. `#r "AssemblyName"`):
171191

172192
* `Newtonsoft.Json`
173193
* `Microsoft.WindowsAzure.Storage`
@@ -178,7 +198,7 @@ In addition, the following assemblies are special cased and may be referenced by
178198
If you need to reference a private assembly, you can upload the assembly file into a `bin` folder relative to your function and reference it by using the file name (e.g. `#r "MyAssembly.dll"`). For information on how to upload files to your function folder, see the following section on package management.
179199

180200
## Editor Prelude
181-
An editor that supports F# Compiler Services will not be aware of the namespaces and assemblies that Azure Functions automatically includes. As such, it can be useful to include a prelude that helps the editor find the assemblies you are using, and to explicitly open namespaces. For example:
201+
An editor that supports F# Compiler Services won't be aware of the namespaces and assemblies that Azure Functions automatically includes. As such, it can be useful to include a prelude that helps the editor find the assemblies you're using, and to explicitly open namespaces. For example:
182202

183203
```fsharp
184204
#if !COMPILED
@@ -199,7 +219,7 @@ When Azure Functions executes your code, it processes the source with `COMPILED`
199219
<a name="package"></a>
200220

201221
## Package management
202-
To use NuGet packages in an F# function, add a `project.json` file to the function's folder in the function app's file system. Here is an example `project.json` file that adds a NuGet package reference to `Microsoft.ProjectOxford.Face` version 1.1.0:
222+
To use NuGet packages in an F# function, add a `project.json` file to the function's folder in the function app's file system. Here's an example `project.json` file that adds a NuGet package reference to `Microsoft.ProjectOxford.Face` version 1.1.0:
203223

204224
```json
205225
{
@@ -221,8 +241,8 @@ You may wish to put automatically references assemblies in your editor prelude,
221241

222242
### How to add a `project.json` file to your Azure Function
223243
1. Begin by making sure your function app is running, which you can do by opening your function in the Azure portal. This also gives access to the streaming logs where package installation output will be displayed.
224-
2. To upload a `project.json` file, use one of the methods described in [how to update function app files](functions-reference.md#fileupdate). If you are using [Continuous Deployment for Azure Functions](functions-continuous-deployment.md), you can add a `project.json` file to your staging branch in order to experiment with it before adding it to your deployment branch.
225-
3. After the `project.json` file is added, you will see output similar to the following example in your function's streaming log:
244+
2. To upload a `project.json` file, use one of the methods described in [how to update function app files](functions-reference.md#fileupdate). If you're using [Continuous Deployment for Azure Functions](functions-continuous-deployment.md), you can add a `project.json` file to your staging branch in order to experiment with it before adding it to your deployment branch.
245+
3. After the `project.json` file is added, you'll see output similar to the following example in your function's streaming log:
226246

227247
```
228248
2016-04-04T19:02:48.745 Restoring packages.
@@ -253,7 +273,7 @@ let Run(timer: TimerInfo, log: ILogger) =
253273
log.LogInformation("Site = " + GetEnvironmentVariable("WEBSITE_SITE_NAME"))
254274
```
255275

256-
## Reusing .fsx code
276+
## Reusing F# script code
257277
You can use code from other `.fsx` files by using a `#load` directive. For example:
258278

259279
`run.fsx`
@@ -272,7 +292,7 @@ let mylog(log: ILogger, text: string) =
272292
log.LogInformation(text);
273293
```
274294

275-
Paths provides to the `#load` directive are relative to the location of your `.fsx` file.
295+
Paths provided to the `#load` directive are relative to the location of your `.fsx` file.
276296

277297
* `#load "logger.fsx"` loads a file located in the function folder.
278298
* `#load "package\logger.fsx"` loads a file located in the `package` folder in the function folder.

0 commit comments

Comments
 (0)