Skip to content

Commit be4880c

Browse files
feat: serverless container sample
1 parent 137f0f1 commit be4880c

File tree

7 files changed

+79
-1
lines changed

7 files changed

+79
-1
lines changed
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using System.Diagnostics;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Logging;
6+
using Ydb.Sdk.Ado;
7+
using Ydb.Sdk.Yc;
8+
9+
var connectionString = new ConfigurationBuilder()
10+
.SetBasePath(Directory.GetCurrentDirectory())
11+
.AddJsonFile("appsettings.json")
12+
.Build()
13+
.GetConnectionString("ServerlessYDB") ??
14+
throw new InvalidOperationException("ConnectionString.ServerlessYDB is empty.");
15+
16+
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
17+
var logger = loggerFactory.CreateLogger<Program>();
18+
var stopwatch = Stopwatch.StartNew();
19+
stopwatch.Start();
20+
21+
await using var dataSource = new YdbDataSource(
22+
new YdbConnectionStringBuilder(connectionString)
23+
{
24+
CredentialsProvider = new MetadataProvider(loggerFactory: loggerFactory),
25+
LoggerFactory = loggerFactory,
26+
DisableDiscovery = true
27+
}
28+
);
29+
30+
await using var ydbCommand = dataSource.CreateCommand();
31+
ydbCommand.CommandText = "SELECT 'Hello Serverless YDB from Yandex Cloud Serverless Container!'u";
32+
var scalar = await ydbCommand.ExecuteScalarAsync();
33+
stopwatch.Stop();
34+
35+
logger.LogInformation("Success request! [Ms: {Ms}], {Select}", stopwatch.ElapsedMilliseconds, scalar);

examples/Ydb.Sdk.AdoNet.Yandex.Cloud.Serverless.Container/README.md

Whitespace-only changes.
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/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)