Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// See https://aka.ms/new-console-template for more information

using Section_1.HR;
using Section_1.HR;

InsertDepartments();
SelectDepartments();
Expand Down
2 changes: 0 additions & 2 deletions examples/Ydb.Sdk.AdoNet.Dapper.QuickStart/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// See https://aka.ms/new-console-template for more information

using Dapper;
using Ydb.Sdk.Ado;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -r linux-x64 -o /app/build

FROM --platform=linux/amd64 mcr.microsoft.com/dotnet/runtime:8.0 AS run
WORKDIR /app
COPY --from=build /app/build/. .
ENTRYPOINT ["dotnet", "Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Ado;
using Ydb.Sdk.Yc;

var connectionString = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build()
.GetConnectionString("ServerlessYDB") ??
throw new InvalidOperationException("ConnectionString.ServerlessYDB is empty.");

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
var logger = loggerFactory.CreateLogger<Program>();
var stopwatch = Stopwatch.StartNew();
stopwatch.Start();

await using var dataSource = new YdbDataSource(
new YdbConnectionStringBuilder(connectionString)
{
CredentialsProvider = new MetadataProvider(loggerFactory: loggerFactory),
LoggerFactory = loggerFactory,
DisableDiscovery = true
}
);

await using var ydbCommand = dataSource.CreateCommand();
ydbCommand.CommandText = "SELECT 'Hello Serverless YDB from Yandex Cloud Serverless Container!'u";
var scalar = await ydbCommand.ExecuteScalarAsync();
stopwatch.Stop();

logger.LogInformation("Success request! [Ms: {Ms}], {Select}", stopwatch.ElapsedMilliseconds, scalar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# YDB Sdk ADO.NET for Serverless Containers in Yandex Cloud

Sample application uses the YDB SDK for ADO.NET and can be deployed
to [Yandex Cloud Serverless Containers](https://yandex.cloud/en/docs/serverless-containers/).

## Getting started

1. **Setup
** [Yandex Container Registry](https://yandex.cloud/en/docs/container-registry/operations/registry/registry-create).
2. **Build and Push Docker Image**
```bash
docker build . -t cr.yandex/<container-registry-id>/ado-net-app:latest
docker push cr.yandex/<container-registry-id>/ado-net-app:latest
```
Replace <container-registry-id> with your actual Container Registry ID.
3. **Grant Required Permissions**. To enable your Serverless Container to access both YDB and your container image in
the Container Registry, grant the following roles to your Service Account:

- `ydb.editor` — access to YDB,
- `container-registry.images.puller` — permission to pull images from Container Registry.

4. **Create a new revision**. After pushing your image, create a new version of the Serverless Container as described in
the [official guide](https://yandex.cloud/en/docs/serverless-containers/quickstart/container#create-revision).
Specify your image and the necessary environment variables and secrets.

5. **Running the Yandex Serverless Container**.
After the new revision has been rolled out, you can use your container (e.g., for a health check) by executing the
following command:
```bash
curl --header "Authorization: Bearer $(yc iam create-token)" https://<serverless-container-host>/
```
Replace <serverless-container-host> with your actual serverless container host.

## ⚠️ Important Note: YdbDataSource and Connection Handling

When using the YDB SDK in a serverless container, it is always necessary to create a new YdbDataSource for each request
or function invocation.

This is because the network connectivity between a serverless container and external resources, such as YDB, can be
interrupted or reset during execution. If you cache or reuse existing connections, you may experience failures due to
stale or invalid connections.

Best practice:

_Create a fresh data source (or Driver for Topic API) for every request or function invocation in order to ensure
reliability in the serverless environment._
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0" />
<PackageReference Include="Ydb.Sdk" Version="0.18.0" />

<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"ServerlessYDB": "UseTls=true;Host=<host>;Port=2135;Database=<database>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Ydb.Protos" Version="1.1.1" />
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions examples/Ydb.Sdk.QueryService.QuickStart/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ internal class CmdOptions
public bool FallbackAnonymous { get; set; } = false;
}

// See https://aka.ms/new-console-template for more information

internal static class Program
{
private static ServiceProvider GetServiceProvider() =>
Expand Down
6 changes: 6 additions & 0 deletions examples/YdbExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schema.ManyToMany", "Entity
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database.Operations.Tutorial", "EntityFrameworkCore.Ydb.Samples\Database.Operations.Tutorial\Database.Operations.Tutorial.csproj", "{B2B0786F-7545-4717-8D04-26DF9C181607}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,6 +83,10 @@ Global
{B2B0786F-7545-4717-8D04-26DF9C181607}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2B0786F-7545-4717-8D04-26DF9C181607}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2B0786F-7545-4717-8D04-26DF9C181607}.Release|Any CPU.Build.0 = Release|Any CPU
{77625697-498B-4879-BABA-046EE93E7AF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77625697-498B-4879-BABA-046EE93E7AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77625697-498B-4879-BABA-046EE93E7AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77625697-498B-4879-BABA-046EE93E7AF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading