.NET Isolated Process Azure Functions
.NET Isolated Process Functions là một mô hình triển khai Azure Function cho phép các hàm chạy trong một quy trình riêng biệt (isolated process), tách biệt khỏi Azure Functions Runtime.
Đây là roadmap của isolated và in-process Azure Function
Sử dụng Isolated Process Azure Functions có nhiều lợi ích
- Quản lý quá trình app startup
- Cấu hình Dependency Injection
- Cấu hình middleware
- Tránh xung đột với các version với các class library
Tạo project Azure Function
Có 2 cách để tạo project Azure Function, 1 là tạo thông qua IDE (Visual Studio, Visual Studio Code), 2 là thông qua command line
1. Để tạo project từ command line, bạn cần cài đặt Azure Functions Core Tools tại đây
Gõ lệnhfunc init ProjectName
Chọn worker runtime là donet (isolated process)
Use the up/down arrow keys to select a worker runtime:
dotnet
dotnet (isolated process)
node
python
powershell
custom
2. Trong Visual Studio, chọn Azure Function => tại Dialog Additional information, chọn .NET 7.0 Isolated cho Function worker
Sau khi tạo ứng dụng, bạn sẽ có câu trúc folder như sau:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
File local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
}
}
Đây là file chứa App settings, connection strings, và các settings utilized ở local development và sẽ không được publish lên Azure Functions Host.
Có 1 key value quan trọng là donet-isolated. Ở trang Configuration trên Azure Function, bạn cần update giá trị từ dotnet thành dotnet-isolated
File Program.csusing Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();
host.Run();
Trường hợp bạn muốn đăng ký Dependency Injection
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddDbContext(options
=> Options.UseSqlServer(
Environment.GetEnvironmentVariable("MyConnection")));
services.AddOptions()
.Configure((settings, configuration)
=> configuration.GetSection("MyOptions").Bind(settings));
services.AddScoped();
})
.Build();
host.Run();
Nhận xét
Đăng nhận xét