Skip to content

Commit 6cf08ad

Browse files
adding microsoft.extensions.azure example
1 parent 59fbc58 commit 6cf08ad

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

articles/azure-functions/dotnet-isolated-process-guide.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,65 @@ The following example injects a singleton service dependency:
120120

121121
This code requires `using Microsoft.Extensions.DependencyInjection;`. To learn more, see [Dependency injection in ASP.NET Core](/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0&preserve-view=true).
122122

123+
#### Register Azure clients
124+
125+
Dependency injection can be used to interact with other Azure services. You can inject clients from the [Azure SDK for .NET](/dotnet/azure/sdk/azure-sdk-for-dotnet) using the [Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure) package. After installing the package, [register the clients](/dotnet/azure/sdk/dependency-injection#register-clients) by calling `AddAzureClients()` on the service collection in `Program.cs`. The following example configures a [named client](/dotnet/azure/sdk/dependency-injection#configure-multiple-service-clients-with-different-names) for Azure Blobs:
126+
127+
```csharp
128+
using Microsoft.Extensions.Azure;
129+
using Microsoft.Extensions.Hosting;
130+
131+
var host = new HostBuilder()
132+
.ConfigureFunctionsWorkerDefaults()
133+
.ConfigureServices((hostContext, services) =>
134+
{
135+
services.AddAzureClients(clientBuilder =>
136+
{
137+
clientBuilder.AddBlobServiceClient(hostContext.Configuration.GetSection("MyStorageConnection"))
138+
.WithName("copierOutputBlob");
139+
});
140+
})
141+
.Build();
142+
143+
host.Run();
144+
```
145+
146+
The following example shows how we can use this registration and [SDK types](#sdk-types) to copy blob contents as a stream from one container to another using an injected client:
147+
148+
```csharp
149+
using Microsoft.Extensions.Azure;
150+
using Microsoft.Extensions.Logging;
151+
152+
namespace MyFunctionApp
153+
{
154+
public class BlobCopier
155+
{
156+
private readonly ILogger<BlobCopier> _logger;
157+
private readonly BlobContainerClient _copyContainerClient;
158+
159+
public BlobCopier(ILogger<BlobCopier> logger, IAzureClientFactory<BlobServiceClient> blobClientFactory)
160+
{
161+
_logger = logger;
162+
_copyContainerClient = blobClientFactory.CreateClient("copierOutputBlob").GetBlobContainerClient("samples-workitems-copy");
163+
_copyContainerClient.CreateIfNotExists();
164+
}
165+
166+
[Function("BlobCopier")]
167+
public async Task Run([BlobTrigger("samples-workitems/{name}", Connection = "MyStorageConnection")] Stream myBlob, string name)
168+
{
169+
await _copyContainerClient.UploadBlobAsync(name, myBlob);
170+
_logger.LogInformation($"Blob {name} copied!");
171+
}
172+
173+
}
174+
}
175+
```
176+
177+
The [ILogger&lt;T&gt;] in this example was also obtained through dependency injection. It is registered automatically. To learn more about configuration options for logging, see [Logging](#logging).
178+
179+
> [!TIP]
180+
> The example used a literal string for the name of the client in both `Program.cs` and the function. Consider instead using a shared constant string defined on the function class. For example, you could add `public const string CopyStorageClientName = nameof(_copyContainerClient);` and then reference `BlobCopier.CopyStorageClientName` in both locations. You could similarly define the configuration section name with the function rather than in `Program.cs`.
181+
123182
### Middleware
124183

125184
.NET isolated also supports middleware registration, again by using a model similar to what exists in ASP.NET. This model gives you the ability to inject logic into the invocation pipeline, and before and after functions execute.
@@ -244,7 +303,7 @@ Each trigger and binding extension also has its own minimum version requirement,
244303
[eventhub-sdk-types]: ./functions-bindings-event-hubs.md?tabs=isolated-process%2Cextensionv5&pivots=programming-language-csharp#binding-types
245304
[servicebus-sdk-types]: ./functions-bindings-service-bus.md?tabs=isolated-process%2Cextensionv5&pivots=programming-language-csharp#binding-types
246305

247-
<sup>1</sup> For output scenarios in which you would use an SDK type, you should create and work with SDK clients directly instead of using an output binding.
306+
<sup>1</sup> For output scenarios in which you would use an SDK type, you should create and work with SDK clients directly instead of using an output binding. See [Register Azure clients](#register-azure-clients) for an example of how to do this with dependency injection.
248307

249308
<sup>2</sup> The Service Bus trigger does not yet support message settlement scenarios for the isolated model.
250309

@@ -496,6 +555,7 @@ Because your isolated worker process app runs outside the Functions runtime, you
496555
[FunctionContext]: /dotnet/api/microsoft.azure.functions.worker.functioncontext?view=azure-dotnet&preserve-view=true
497556
[ILogger]: /dotnet/api/microsoft.extensions.logging.ilogger
498557
[ILogger&lt;T&gt;]: /dotnet/api/microsoft.extensions.logging.ilogger-1
558+
[ILoggerFactory]: /dotnet/api/microsoft.extensions.logging.iloggerfactory
499559
[GetLogger]: /dotnet/api/microsoft.azure.functions.worker.functioncontextloggerextensions.getlogger
500560
[GetLogger&lt;T&gt;]: /dotnet/api/microsoft.azure.functions.worker.functioncontextloggerextensions.getlogger#microsoft-azure-functions-worker-functioncontextloggerextensions-getlogger-1
501561
[HttpRequestData]: /dotnet/api/microsoft.azure.functions.worker.http.httprequestdata?view=azure-dotnet&preserve-view=true

0 commit comments

Comments
 (0)