Skip to content

Commit 6832182

Browse files
dev: serverless container sample (#441)
1 parent 02ffd24 commit 6832182

File tree

10 files changed

+124
-8
lines changed

10 files changed

+124
-8
lines changed

examples/EntityFrameworkCore.Ydb.Samples/AddEntity.Sample/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Section_1.HR;
1+
using Section_1.HR;
42

53
InsertDepartments();
64
SelectDepartments();

examples/Ydb.Sdk.AdoNet.Dapper.QuickStart/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// See https://aka.ms/new-console-template for more information
2-
31
using Dapper;
42
using Ydb.Sdk.Ado;
53

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
2+
WORKDIR /src
3+
COPY . .
4+
RUN dotnet restore
5+
RUN dotnet publish -c Release -r linux-x64 -o /app/build
6+
7+
FROM --platform=linux/amd64 mcr.microsoft.com/dotnet/runtime:8.0 AS run
8+
WORKDIR /app
9+
COPY --from=build /app/build/. .
10+
ENTRYPOINT ["dotnet", "Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container.dll"]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Diagnostics;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Logging;
4+
using Ydb.Sdk.Ado;
5+
using Ydb.Sdk.Yc;
6+
7+
var connectionString = new ConfigurationBuilder()
8+
.SetBasePath(Directory.GetCurrentDirectory())
9+
.AddJsonFile("appsettings.json")
10+
.Build()
11+
.GetConnectionString("ServerlessYDB") ??
12+
throw new InvalidOperationException("ConnectionString.ServerlessYDB is empty.");
13+
14+
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
15+
var logger = loggerFactory.CreateLogger<Program>();
16+
var stopwatch = Stopwatch.StartNew();
17+
stopwatch.Start();
18+
19+
await using var dataSource = new YdbDataSource(
20+
new YdbConnectionStringBuilder(connectionString)
21+
{
22+
CredentialsProvider = new MetadataProvider(loggerFactory: loggerFactory),
23+
LoggerFactory = loggerFactory,
24+
DisableDiscovery = true
25+
}
26+
);
27+
28+
await using var ydbCommand = dataSource.CreateCommand();
29+
ydbCommand.CommandText = "SELECT 'Hello Serverless YDB from Yandex Cloud Serverless Container!'u";
30+
var scalar = await ydbCommand.ExecuteScalarAsync();
31+
stopwatch.Stop();
32+
33+
logger.LogInformation("Success request! [Ms: {Ms}], {Select}", stopwatch.ElapsedMilliseconds, scalar);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# YDB Sdk ADO.NET for Serverless Containers in Yandex Cloud
2+
3+
Sample application uses the YDB SDK for ADO.NET and can be deployed
4+
to [Yandex Cloud Serverless Containers](https://yandex.cloud/en/docs/serverless-containers/).
5+
6+
## Getting started
7+
8+
1. **Setup
9+
** [Yandex Container Registry](https://yandex.cloud/en/docs/container-registry/operations/registry/registry-create).
10+
2. **Build and Push Docker Image**
11+
```bash
12+
docker build . -t cr.yandex/<container-registry-id>/ado-net-app:latest
13+
docker push cr.yandex/<container-registry-id>/ado-net-app:latest
14+
```
15+
Replace <container-registry-id> with your actual Container Registry ID.
16+
3. **Grant Required Permissions**. To enable your Serverless Container to access both YDB and your container image in
17+
the Container Registry, grant the following roles to your Service Account:
18+
19+
- `ydb.editor` — access to YDB,
20+
- `container-registry.images.puller` — permission to pull images from Container Registry.
21+
22+
4. **Create a new revision**. After pushing your image, create a new version of the Serverless Container as described in
23+
the [official guide](https://yandex.cloud/en/docs/serverless-containers/quickstart/container#create-revision).
24+
Specify your image and the necessary environment variables and secrets.
25+
26+
5. **Running the Yandex Serverless Container**.
27+
After the new revision has been rolled out, you can use your container (e.g., for a health check) by executing the
28+
following command:
29+
```bash
30+
curl --header "Authorization: Bearer $(yc iam create-token)" https://<serverless-container-host>/
31+
```
32+
Replace <serverless-container-host> with your actual serverless container host.
33+
34+
## ⚠️ Important Note: YdbDataSource and Connection Handling
35+
36+
When using the YDB SDK in a serverless container, it is always necessary to create a new YdbDataSource for each request
37+
or function invocation.
38+
39+
This is because the network connectivity between a serverless container and external resources, such as YDB, can be
40+
interrupted or reset during execution. If you cache or reuse existing connections, you may experience failures due to
41+
stale or invalid connections.
42+
43+
Best practice:
44+
45+
_Create a fresh data source (or Driver for Topic API) for every request or function invocation in order to ensure
46+
reliability in the serverless environment._
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
16+
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0" />
17+
<PackageReference Include="Ydb.Sdk" Version="0.18.0" />
18+
19+
<None Update="appsettings.json">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ConnectionStrings": {
3+
"ServerlessYDB": "UseTls=true;Host=<host>;Port=2135;Database=<database>"
4+
}
5+
}

examples/Ydb.Sdk.AdoNet.Yandex.Cloud/Ydb.Sdk.AdoNet.Yandex.Cloud.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<PackageReference Include="CommandLineParser" Version="2.8.0" />
1313
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
15-
<PackageReference Include="Ydb.Protos" Version="1.1.1" />
1615
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0" />
1716
</ItemGroup>
1817
<ItemGroup>

examples/Ydb.Sdk.QueryService.QuickStart/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ internal class CmdOptions
2020
public bool FallbackAnonymous { get; set; } = false;
2121
}
2222

23-
// See https://aka.ms/new-console-template for more information
24-
2523
internal static class Program
2624
{
2725
private static ServiceProvider GetServiceProvider() =>

examples/YdbExamples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schema.ManyToMany", "Entity
2727
EndProject
2828
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database.Operations.Tutorial", "EntityFrameworkCore.Ydb.Samples\Database.Operations.Tutorial\Database.Operations.Tutorial.csproj", "{B2B0786F-7545-4717-8D04-26DF9C181607}"
2929
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container", "Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container\Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container.csproj", "{77625697-498B-4879-BABA-046EE93E7AF7}"
31+
EndProject
3032
Global
3133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3234
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +83,10 @@ Global
8183
{B2B0786F-7545-4717-8D04-26DF9C181607}.Debug|Any CPU.Build.0 = Debug|Any CPU
8284
{B2B0786F-7545-4717-8D04-26DF9C181607}.Release|Any CPU.ActiveCfg = Release|Any CPU
8385
{B2B0786F-7545-4717-8D04-26DF9C181607}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{77625697-498B-4879-BABA-046EE93E7AF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{77625697-498B-4879-BABA-046EE93E7AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{77625697-498B-4879-BABA-046EE93E7AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{77625697-498B-4879-BABA-046EE93E7AF7}.Release|Any CPU.Build.0 = Release|Any CPU
8490
EndGlobalSection
8591
GlobalSection(SolutionProperties) = preSolution
8692
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)