.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();
Advantages of using the Isolated Process
Trả lờiXóaThe first advantage of switching to Isolated Process is improved stability and availability of functions. By running the functions in a separate process, errors will not be able to affect the web application and vice versa. This means that the functions will remain online even if the web application experiences problems. This will improve the application’s availability.
Secondly, this allows better resource isolation. Each function will have its own memory and CPU resource allocation, which will prevent conflicts between functions and ensure better resource usage. This will also better manage peak loads by allowing the simultaneous execution of multiple functions.
The third advantage is improved security. It will be more difficult for attackers to access the data and resources of the web application. This will increase the security of the application and protect sensitive user data.
In conclusion, the transition from Azure Functions in process to isolated process will bring several benefits for developers, including improved stability and availability, better resource isolation, and improved security. If you are using Azure Functions in your application, you should consider switching to isolated process mode to fully benefit from it.
https://medium.com/@yusufsarikaya023/clean-architecture-in-serverless-azure-function-713582c7dc9b
Trả lờiXóa