Skip to content

Region.FromRegionId() throws CultureNotFoundException when running in Docker (Alpine, .NET 9) #349

@RazvanGolan

Description

@RazvanGolan

Summary

When using Region.FromRegionId() in the OCI .NET SDK, the runtime throws a CultureNotFoundException because the SDK internally calls:

new CultureInfo("en-US", useUserOverride: false)

In Docker (Alpine-based images), .NET defaults to Globalization Invariant Mode, where only the invariant culture is available. As a result, "en-US" cannot be created, and the OCI SDK fails to initialize the region registry, throwing this error:

System.TypeInitializationException: The type initializer for 'Oci.Common.Region' threw an exception.
 ---> System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode.
en-US is an invalid culture identifier.
   at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
   at Oci.Common.Region.Register(...)
   at Oci.Common.Region..cctor()
   --- End of inner exception stack trace ---
   at Oci.Common.Region.FromRegionId(String regionId)

Environment

  • OCI .NET SDK: v85.0.0
  • .NET Runtime: .NET 9
  • OS: Linux (Alpine base image)
  • Environment: Docker container
  • Globalization mode: Invariant mode

Use Case

Creating an OCI Object Storage client using:

var provider = new SimpleAuthenticationDetailsProvider
{
    TenantId = credentials.Tenancy,
    UserId = credentials.UserId,
    Fingerprint = credentials.Fingerprint,
    Region = Region.FromRegionId(credentials.RegionEndpoint),
    PrivateKeySupplier = new StringBasedPrivateKeySupplier(credentials.PrivateKey)
};

var client = new ObjectStorageClient(provider);

Reproducible Example (Default Single-Project Setup)

Dockerfile

This Dockerfile represents a standard minimal C# project built and run in Docker

FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS build

WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:9.0-alpine

WORKDIR /app
COPY --from=build /app/publish .

ENTRYPOINT ["dotnet", "OciIntegrationTest.dll"]

Program.cs (Minimal Repro)

using Oci.Common;
using Oci.ObjectstorageService;

var region = Region.FromRegionId("eu-frankfurt-1"); // <-- exception here
Console.WriteLine(region.RegionId);

This reproduces the crash on every Alpine-based .NET image unless the user manually installs ICU and disables invariant mode.

Expected Behavior

  • Should work in Docker without requiring ICU libraries
  • Or fallback to CultureInfo.InvariantCulture when running under invariant mode

Suggested Fix

Consider replacing:

regionId.Trim().ToLower(new CultureInfo("en-US", useUserOverride: false));

with:

regionId.Trim().ToLower(CultureInfo.InvariantCulture);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions