gpt4 book ai didi

azure - 使用本地 Cosmos DB 模拟器调试 Azure Functions

转载 作者:行者123 更新时间:2023-12-03 04:42:39 28 4
gpt4 key购买 nike

我正在尝试测试针对本地 Cosmos DB 模拟器运行的 Azure 函数(在 VS 2019 社区上用 C# 编写,使用 HTTP 触发器触发)。当我的代码尝试与模拟器通信时,它似乎超时,并给了我一个异常。

相关代码片段:

CosmosClient client = new CosmosClient(storageURL, authKeyString);

try
{
Database oldDB = client.GetDatabase(dbName);
DatabaseResponse dbResp = await oldDB.DeleteAsync();
}

我的输入变量:

storageURL  "http://localhost:8081"
authKeyString "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="

在调用 GetDatabase() 时,代码等待很长一段时间并抛出异常:

{"The operation was canceled."}
CancellationToken: IsCancellationRequested = true
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233029
HelpLink: null
InnerException: {"Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request."}
Message: "The operation was canceled."
Source: "System.Net.Http"
StackTrace: " at System.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpConnectionPool.<SendWithNtConnectionAuthAsync>d__40.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__39.MoveNext()\r\n at System.Runtime.ExceptionServices.Exce
ptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.DocumentClient.HttpRequestMessageHandler.<SendAsync>d__3.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.C
ompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__62.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.GatewayAccountReader.<GetDatabaseAccountAsync>d__8.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.Routing.GlobalEndpointManager.<GetDatabaseAccountFromAnyLocationsAsync>d__16.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuc
cess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.GatewayAccountReader.<InitializeReaderAsync>d__9.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.CosmosAccountServiceConfiguration.<InitializeAsync>d__36.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.DocumentClient.<InitializeGatewayConfigurationReaderAsync>d__309.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System
.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.DocumentClient.<GetInitializationTaskAsync>d__103.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.DocumentClient.<EnsureValidClientAsync>d__163.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.<EnsureValidClientAsync>d__9.MoveNext()\r\n at System.Runtime.ExceptionServices.Exceptio
nDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.<SendAsync>d__5.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.<SendAsync>d__7.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Azure.Cosmos.CosmosResponseFactory.<ProcessMessageAsync>d__16`1.MoveNext()\r\n at System.Runtime.
ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at Leaderboard_CosmosDB.Leaderboard_CosmosDB.<CreateDB>d__6.MoveNext() in C:\\Users\\Matthew\\source\\repos\\Leaderboard_CosmosDB\\Leaderboard_CosmosDB\\Leaderboard_CosmosDB.cs:line 89"
TargetSite: {Void MoveNext()}
Task: null

如果我将 local.settings.json 文件中的 storageURL 和 authKeyString 替换为 Azure 中运行的 Cosmos DB 实例中的 URL 和 key ,则代码可以正常工作。当然,我更喜欢在本地调试(并且免费)。

Cosmos 模拟器与函数调试器在同一台计算机上运行。我可以在同一台计算机上运行的 Web 浏览器中使用数据资源管理器在模拟器中创建新的数据库/容器。

我在两台不同的机器上都发生过这种情况。无论 Windows 防火墙打开还是关闭,都会发生这种情况。

让本地调试的 Azure 函数连接到 Cosmos DB 模拟器的本地实例的秘诀是什么?

编辑:其他人要求提供遇到问题的函数的完整代码。你在这里:

[FunctionName("CreateDB")]
public static async Task<string> CreateDB(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log,
ExecutionContext context)
{

string storageURL = "http://localhost:8081";
string authKeyString = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
string dbName = "testDB";
string containerName = "testContainer");
string partitionKeyPath = "/partition");
CosmosClient client = new CosmosClient(storageURL, authKeyString);

try
{
Database oldDB = client.GetDatabase(dbName);
DatabaseResponse dbResp = await oldDB.DeleteAsync(); //Error is here
}
catch (Exception ex)
{
if (!ex.Message.Contains("Resource Not Found"))
{
return ("Error deleting old database: " + ex.Message);
}
}


try
{
DatabaseResponse dbresp = await client.CreateDatabaseAsync(dbName);
ContainerResponse cresp = await dbresp.Database.CreateContainerAsync(new ContainerProperties(containerName, partitionKeyPath));
}
catch (Exception ex)
{
return (ex.Message);
}



return ("OK");
}

最佳答案

同样的环境,我成功了。

  1. Install Azure Cosmos Emulator

  2. 从模拟器页面获取端点和 key enter image description here

  3. 创建示例控制台应用

    class Program
{
static void Main(string[] args)
{
string endpoint = "https://localhost:8081";
string key = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
string databaseId = "TestDB";
string containerId = "TestContainer";
var client = new CosmosClient(endpoint, key);
var db = client.CreateDatabaseIfNotExistsAsync(databaseId).GetAwaiter().GetResult().Database;
var container = db.CreateContainerIfNotExistsAsync(containerId, "/partitionkey").GetAwaiter().GetResult().Container;
var result = container.CreateItemAsync(new { id = Guid.NewGuid(), partitionkey = "A", content = "Test" }).GetAwaiter().GetResult();
Console.WriteLine(result.StatusCode);

Console.ReadLine();
}
}
  • 结果 enter image description here
  • 从结果中,您可以看到我可以成功连接到模拟器并创建项目。请检查一下您的步骤是否有问题。

    关于azure - 使用本地 Cosmos DB 模拟器调试 Azure Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553438/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com