当前位置: 首页 > 知识库问答 >
问题:

用本地Cosmos DB模拟器调试Azure函数

龙令
2023-03-14
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=="
{"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 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个答案

魏刚豪
2023-03-14

同样的环境,我获得了成功。

>

  • 安装Azure Cosmos模拟器

    创建示例控制台应用程序

        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();
            }
        }
    

  •  类似资料:
    • 我在我的Win10计算机上安装了Azure CosmosDB模拟器,并且我有启动页面的可见性: 但当我尝试通过Robo3T或命令行连接到数据库时,它不起作用。我使用以下命令: 我得到了以下问题: 2019-07-18T14:33:27.265 0200 W NETWORK[thread1] 5000ms毫秒后连接127.0.0.1:10255失败,放弃。2019-07-18T14:33:27.26

    • 我有一个功能。Net标准2.0: 根据本文:https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local部分"非HTTP触发函数" “对于HTTP触发器和Webhook以外的所有类型的函数,您都可以通过调用管理终结点在本地测试您的函数。在本地服务器上使用HTTP POST请求调用此终结点会触发该函数。您可以选择

    • 理想情况下,我希望能够创建一个像.azcli这样的脚本,我可以在CosmosDB模拟器上本地运行,然后在我的devops管道中使用相同的脚本将资源上传到Azure。 这是我当前的 CLI 命令。 deploy-cosmos.azcli 我在Azure CLI文档中找不到任何提及CosmosDB模拟器的内容,并且CLI命令本身的参数似乎都无法直接指向模拟器。 https://docs.microso

    • 您好,我在Azure门户中创建了一个函数并看到队列数据出队。 我在我的计算机上安装了一个CLI,当我运行项目时,它将显示一个func。exe命令提示符。我在local.settings.json中添加了如下连接字符串: 当我在门户中向队列中添加消息时,应用程序未收到该消息。我需要更新哪些其他设置。

    • 我刚刚在MacOSX(和linux)上安装了Azure CosmosDB模拟器,并提供了docker容器,如下所示:https://docs.microsoft.com/en-us/azure/cosmos-db/linux-emulator 我看了一眼容器内部,并在“/tmp/cosmos/appdata”文件夹下找到了“default.sslcert.pfx”。我认为这是模拟器正在使用的证书,

    • 我有一个java版本为“1.8.0_191”的Mac当我运行我的android应用程序时,它在模拟器中运行得很好,但是当我开始调试应用程序时,它并没有启动应用程序,而是抛出一个如下所示的错误 我尝试了以下引用没有运气DDMS-不能绑定到本地8600的调试器在Android Studio 谁能给我提一下如何解决这个问题吗