使用 C# 代码在本地创建 CosmosDB 数据库会引发内部服务器错误

Creating a CosmosDB database locally with C# code throws an Internal Server error

我尝试在本地 cosmosDB 模拟器中创建一个数据库,但这失败了。它抛出异常并显示以下消息:

Response status code does not indicate success: InternalServerError (500); Substatus: 0; ActivityId: 36f6a95d-26d7-4ecd-9c81-f6df220f3cdc; Reason: (Unknown server error occurred when processing this request. ActivityId: 36f6a95d-26d7-4ecd-9c81-f6df220f3cdc, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, Windows/10.0.19042 cosmos-netstandard-sdk/3.24.1);

此错误发生在我的项目以及仅包含以下代码行的简单、空的控制台应用程序中:

using Microsoft.Azure.Cosmos;

namespace CosmosDBTest;

class Program
{
    public static async Task Main()
    {
        using var client = new CosmosClient("<A local connection string>");
        var response = await client.CreateDatabaseIfNotExistsAsync("test", 500);
        var database = response.Database;
        await database.CreateContainerIfNotExistsAsync("container1", "id");
    }
}

我使用的模拟器版本是2.14.6。 NuGet包的版本是3.26.1

是什么导致了这个异常,我该如何解决?我曾将旧版本的模拟器与旧版本的 NuGet 包结合使用。但在我降级模拟器 and/or 包之前,我想看看是否有更好的解决方法。

我还没有尝试过模拟器提供的演示应用程序。有趣的是,它位于名为“.NET”的选项卡上(旁边是另一个选项卡 .NET core),“.NET”示例是用我没有安装的非常旧的 .net core 2.1 版本编写的甚至。

尝试重置模拟器,这看起来像是模拟器本身的故障。 HTTP 500 来自模拟器,SDK 刚刚显示响应。

参考:https://docs.microsoft.com/azure/cosmos-db/troubleshoot-local-emulator

If you installed a new version of the emulator and are experiencing errors, ensure you reset your data. You can reset your data by right-clicking the Azure Cosmos DB Emulator icon on the system tray, and then clicking Reset Data…. If that does not fix the errors, you can uninstall the emulator and any older versions of the emulator if found, remove C:\Program files\Azure Cosmos DB Emulator directory and reinstall the emulator.

Alternatively if resetting the data doesn't work, navigate to %LOCALAPPDATA%\CosmosDBEmulator location and delete the folder.