Skip to content

Commit 4f6957b

Browse files
Merge pull request #247574 from mattchenderson/sdktypes2
adding microsoft.extensions.azure example
2 parents e6cf87e + 6cf08ad commit 4f6957b

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
@@ -122,6 +122,65 @@ The following example injects a singleton service dependency:
122122

123123
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).
124124

125+
#### Register Azure clients
126+
127+
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:
128+
129+
```csharp
130+
using Microsoft.Extensions.Azure;
131+
using Microsoft.Extensions.Hosting;
132+
133+
var host = new HostBuilder()
134+
.ConfigureFunctionsWorkerDefaults()
135+
.ConfigureServices((hostContext, services) =>
136+
{
137+
services.AddAzureClients(clientBuilder =>
138+
{
139+
clientBuilder.AddBlobServiceClient(hostContext.Configuration.GetSection("MyStorageConnection"))
140+
.WithName("copierOutputBlob");
141+
});
142+
})
143+
.Build();
144+
145+
host.Run();
146+
```
147+
148+
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:
149+
150+
```csharp
151+
using Microsoft.Extensions.Azure;
152+
using Microsoft.Extensions.Logging;
153+
154+
namespace MyFunctionApp
155+
{
156+
public class BlobCopier
157+
{
158+
private readonly ILogger<BlobCopier> _logger;
159+
private readonly BlobContainerClient _copyContainerClient;
160+
161+
public BlobCopier(ILogger<BlobCopier> logger, IAzureClientFactory<BlobServiceClient> blobClientFactory)
162+
{
163+
_logger = logger;
164+
_copyContainerClient = blobClientFactory.CreateClient("copierOutputBlob").GetBlobContainerClient("samples-workitems-copy");
165+
_copyContainerClient.CreateIfNotExists();
166+
}
167+
168+
[Function("BlobCopier")]
169+
public async Task Run([BlobTrigger("samples-workitems/{name}", Connection = "MyStorageConnection")] Stream myBlob, string name)
170+
{
171+
await _copyContainerClient.UploadBlobAsync(name, myBlob);
172+
_logger.LogInformation($"Blob {name} copied!");
173+
}
174+
175+
}
176+
}
177+
```
178+
179+
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).
180+
181+
> [!TIP]
182+
> 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`.
183+
125184
### Middleware
126185

127186
.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.
@@ -246,7 +305,7 @@ Each trigger and binding extension also has its own minimum version requirement,
246305
[eventhub-sdk-types]: ./functions-bindings-event-hubs.md?tabs=isolated-process%2Cextensionv5&pivots=programming-language-csharp#binding-types
247306
[servicebus-sdk-types]: ./functions-bindings-service-bus.md?tabs=isolated-process%2Cextensionv5&pivots=programming-language-csharp#binding-types
248307

249-
<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.
308+
<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.
250309

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

@@ -502,6 +561,7 @@ Because your isolated worker process app runs outside the Functions runtime, you
502561
[FunctionContext]: /dotnet/api/microsoft.azure.functions.worker.functioncontext?view=azure-dotnet&preserve-view=true
503562
[ILogger]: /dotnet/api/microsoft.extensions.logging.ilogger
504563
[ILogger&lt;T&gt;]: /dotnet/api/microsoft.extensions.logging.ilogger-1
564+
[ILoggerFactory]: /dotnet/api/microsoft.extensions.logging.iloggerfactory
505565
[GetLogger]: /dotnet/api/microsoft.azure.functions.worker.functioncontextloggerextensions.getlogger
506566
[GetLogger&lt;T&gt;]: /dotnet/api/microsoft.azure.functions.worker.functioncontextloggerextensions.getlogger#microsoft-azure-functions-worker-functioncontextloggerextensions-getlogger-1
507567
[HttpRequestData]: /dotnet/api/microsoft.azure.functions.worker.http.httprequestdata?view=azure-dotnet&preserve-view=true

0 commit comments

Comments
 (0)