使用本地Cosmos DB仿真器调试Azure功能

时间:2020-01-01 14:43:28

标签: azure azure-functions azure-cosmosdb

我正在尝试测试针对本地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

如果我用来自Azure中运行的Cosmos数据库实例的URL和密钥替换local.settings.json文件中的storageURL和authKeyString,则代码可以正常工作。当然,我更愿意在本地调试(并且免费)。

Cosmos Emulator与功能调试器在同一台计算机上运行。我可以在同一台计算机上运行的Web浏览器中使用Data Explorer在模拟器中创建一个新的数据库/容器。

在2台不同的机器上发生了这种情况。无论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 个答案:

答案 0 :(得分:0)

在相同的环境下,我获得了成功。

  1. Install Azure Cosmos Emulator

  2. 从仿真器页面获取端点和密钥 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();
        }
    }
  1. 结果 enter image description here

从结果中,您可以看到我可以成功连接到模拟器并创建项目。请检查您的步骤,看是否有任何问题。