Skip to content

Commit

Permalink
Merge pull request #17 from AzureCosmosDB/auto-20241106145332
Browse files Browse the repository at this point in the history
[AUTO] 2024-11-06 | Merge latest from source repo
  • Loading branch information
2 parents 2637327 + 965564d commit 7470770
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
15 changes: 15 additions & 0 deletions models/Settings/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings;

public record Configuration
{
public required AzureCosmosDB AzureCosmosDB { get; init; }
}

public record AzureCosmosDB
{
public required string Endpoint { get; init; }

public required string DatabaseName { get; init; }

public required string ContainerName { get; init; }
}
15 changes: 12 additions & 3 deletions services/DemoService.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
using System.Configuration;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Options;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces;

using Settings = Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings;

namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services;

public sealed class DemoService(CosmosClient client) : IDemoService
public sealed class DemoService(
CosmosClient client,
IOptions<Settings.Configuration> configurationOptions
) : IDemoService
{
private readonly Settings.Configuration configuration = configurationOptions.Value;

public string GetEndpoint() => $"{client.Endpoint}";

public async Task RunAsync(Func<string, Task> writeOutputAync)
{
Database database = client.GetDatabase("cosmicworks");
Database database = client.GetDatabase(configuration.AzureCosmosDB.DatabaseName);

database = await database.ReadAsync();
await writeOutputAync($"Get database:\t{database.Id}");

Container container = database.GetContainer("products");
Container container = database.GetContainer(configuration.AzureCosmosDB.ContainerName);

container = await container.ReadContainerAsync();
await writeOutputAync($"Get container:\t{container.Id}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.45.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\models\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj" />
Expand Down
6 changes: 3 additions & 3 deletions web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<UserSecretsId>f6167579-5a7c-405e-bdae-cf20a79d6b9d</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.45.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\models\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj" />
Expand Down
10 changes: 9 additions & 1 deletion web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
using Azure.Identity;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Options;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.Components;

using Settings = Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorComponents().AddInteractiveServerComponents();

builder.Services.AddSingleton<CosmosClient>((_) =>
builder.Services.AddOptions<Settings.Configuration>().Bind(builder.Configuration.GetSection(nameof(Settings.Configuration)));

builder.Services.AddSingleton<CosmosClient>((serviceProvider) =>
{
IOptions<Settings.Configuration> configurationOptions = serviceProvider.GetRequiredService<IOptions<Settings.Configuration>>();
Settings.Configuration configuration = configurationOptions.Value;

CosmosClient client = new(
connectionString: "<azure-cosmos-db-nosql-connection-string>"
);
Expand Down
10 changes: 8 additions & 2 deletions web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
"AllowedHosts": "*",
"Configuration": {
"AzureCosmosDB": {
"DatabaseName": "cosmicworks",
"ContainerName": "products"
}
}
}

0 comments on commit 7470770

Please sign in to comment.