Skip to content
Draft
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
40 changes: 34 additions & 6 deletions ServerlessLibraryAPI/CosmosLibraryStore.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;
using ServerlessLibrary.Models;

namespace ServerlessLibrary
Expand All @@ -28,7 +29,7 @@ async public Task<IList<LibraryItem>> GetAllItems()
return libraryItems.ToList();
}
}

/// <summary>
/// Cosmos db APIs
/// </summary>
Expand Down Expand Up @@ -98,10 +99,37 @@ public static void Initialize()
{
if (container == null)
{
CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
ServerlessLibrarySettings.CosmosEndpoint,
ServerlessLibrarySettings.CosmosAuthkey);
CosmosClient client = cosmosClientBuilder.Build();
CosmosClient client;

// Use DefaultAzureCredential as the default authentication method (recommended for Azure workloads)
if (!string.IsNullOrEmpty(ServerlessLibrarySettings.CosmosEndpoint))
{
try
{
// Create DefaultAzureCredential with basic options compatible with .NET Core 2.1
TokenCredential credential = new DefaultAzureCredential();
client = new CosmosClient(ServerlessLibrarySettings.CosmosEndpoint, credential);
}
catch
{
// Fallback to connection string authentication if DefaultAzureCredential fails
if (!string.IsNullOrEmpty(ServerlessLibrarySettings.CosmosAuthkey))
{
client = new CosmosClient(
ServerlessLibrarySettings.CosmosEndpoint,
ServerlessLibrarySettings.CosmosAuthkey);
}
else
{
throw new System.InvalidOperationException(
"Unable to authenticate with Cosmos DB. Ensure either managed identity is configured or CosmosAuthkey is provided.");
}
}
}
else
{
throw new System.InvalidOperationException("CosmosEndpoint must be configured.");
}

DatabaseResponse databaseResponse = client.CreateDatabaseIfNotExistsAsync(DatabaseId).Result;
Database database = databaseResponse;
Expand Down
8 changes: 6 additions & 2 deletions ServerlessLibraryAPI/ServerlessLibraryAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
<ApplicationInsightsAnnotationResourceId>/subscriptions/7c1b7bab-00b2-4cb7-924e-205c4f411810/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/ServerlessLibrary</ApplicationInsightsAnnotationResourceId>
<UserSecretsId>235c2497-239d-47f0-8ea7-af2dd2416d95</UserSecretsId>
<RootNamespace>ServerlessLibrary</RootNamespace>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.5.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.6.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.26.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.9.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

Expand Down