实现自定义BotState服务后,Cosmos DB会抛出“找不到资源”错误

时间:2018-01-25 10:59:51

标签: c# .net botframework azure-cosmosdb

我们最近使用以下文章更新了我们的僵尸程序,以便从即将弃用的默认BotState服务迁移到Azure Cosmos数据库存储: https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-state-azure-cosmosdb

以下模块在Application_Start方法中的对话容器中注册:

public class CustomBotStateServiceModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var stateStore = new DocumentDbBotDataStore(
            new Uri(ConfigurationManager.AppSettings.Get("EndpointUri")),
            ConfigurationManager.AppSettings.Get("PrimaryKey"),
            ConfigurationManager.AppSettings.Get("DatabaseId"),
            "BotState");

        builder.Register(c => stateStore)
               .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
               .AsSelf()
               .SingleInstance();

        builder.Register(c => new CachingBotDataStore(stateStore, CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
               .As<IBotDataStore<BotData>>()
               .AsSelf()
               .InstancePerLifetimeScope();
    }
}

在我们使用Bot Channel Emulator的本地测试中,我们注意到,无论何时第一次访问BotState,都会将以下错误打印到Visual Studio的Debug Output中:

DocDBTrace Information: 0 : DocumentClient with id 1 initialized at endpoint: https://<our-instance>.documents.azure.com/ with ConnectionMode: Gateway, connection Protocol: Https, and consistency level: null
DocDBTrace Information: 0 : RefreshLocationAsync() refreshing locations
DocDBTrace Information: 0 : Set WriteEndpoint https://<our-instance>-westus.documents.azure.com/ ReadEndpoint https://<our-instance>-westus.documents.azure.com/
DocDBTrace Error: 0 : DocumentClientException with status code: NotFound, message: Message: {"Errors":["Resource Not Found"]}
ActivityId: <activity-id>, Request URI: /apps/<app-id>/services/<service-id>/partitions/<partition-id>/replicas/<replica-id>/, RequestStats: 
ResponseTime: 2018-01-25T08:20:59.3067000Z, StoreReadResult: StorePhysicalAddress: rntbd://<docdb-location>.documents.azure.com:14043/apps/<app-id>/services/<service-id>/partitions/<partition-id>/replicas/<replica-id>/, LSN: 154, GlobalCommittedLsn: 151, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, RequestCharge: 1, ItemLSN: -1, ResourceType: Document, OperationType: Read
, SDK: Microsoft.Azure.Documents.Common/1.19.162.2, and response headers: {
"x-ms-last-state-change-utc": "Wed, 24 Jan 2018 00:00:51.902 GMT",
"x-ms-schemaversion": "1.4",
"x-ms-xp-role": "1",
"x-ms-global-Committed-lsn": "151",
"x-ms-number-of-read-regions": "0",
"x-ms-request-charge": "1",
"x-ms-serviceversion": "version=1.19.162.2",
"x-ms-activity-id": "<activity-id>",
"Strict-Transport-Security": "max-age=31536000",
"x-ms-gatewayversion": "version=1.19.162.2",
"X-Cache": "MISS from <node>",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Date": "Thu, 25 Jan 2018 08:20:58 GMT",
"Server": "Microsoft-HTTPAPI/2.0",
"Via": "1.1 <node> (squid)",
}
DocDBTrace Error: 0 : Operation will NOT be retried. Current attempt 0, Exception: Microsoft.Azure.Documents.DocumentClientException: Message: {"Errors":["Resource Not Found"]}
ActivityId: <activity-id>, Request URI: /apps/<app-id>/services/<service-id>/partitions/<partition-id>/replicas/<replica-id>/, RequestStats: 
ResponseTime: 2018-01-25T08:20:59.3067000Z, StoreReadResult: StorePhysicalAddress: rntbd://<docdb-location>.documents.azure.com:14043/apps/<app-id>/services/<service-id>/partitions/<partition-id>/replicas/<replica-id>/, LSN: 154, GlobalCommittedLsn: 151, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: True, RequestCharge: 1, ItemLSN: -1, ResourceType: Document, OperationType: Read
, SDK: Microsoft.Azure.Documents.Common/1.19.162.2, documentdb-dotnet-sdk/1.19.1 Host/32-bit MicrosoftWindowsNT6.1.7601ServicePack1
   at Microsoft.Azure.Documents.Client.ClientExtensions.<ParseResponseAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.Documents.GatewayStoreModel.<>c__DisplayClass10.<<InvokeAsync>b__f>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<>c__DisplayClass2.<<ExecuteAsync>b__0>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<ExecuteRetry>d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<ExecuteRetry>d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<ExecuteAsync>d__a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.GatewayStoreModel.<InvokeAsync>d__1f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.GatewayStoreModel.<ProcessMessageAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.Client.DocumentClient.<ReadAsync>d__30c.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.Client.DocumentClient.<ReadDocumentPrivateAsync>d__18d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<>c__DisplayClass2.<<ExecuteAsync>b__0>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<ExecuteRetry>d__1b.MoveNext() 

之后不久,这些错误会再打印1-2次。尽管如此,我们的自定义BotState服务似乎正常工作,因为我们可以看到在相关的Cosmos DB集合中创建的条目。

注意:我们没有专门使用我们的Cosmos DB来存储BotState数据。我们使用它来存储机器人活动数据,日志等。它目前有两个包含多个集合的数据库。

这些错误的根本原因是什么?这是我们应该考虑的事情吗?一旦我们从容器注册中拔出自定义BotState服务,错误就会消失。

1 个答案:

答案 0 :(得分:3)

我可以根据我的测试和研究重现这个问题,我发现当我们想要将状态数据存储到Azure Cosmos数据库集合时,Azure SDK可以帮助我们检查文档是否存在,这样你就可以看到当您的bot应用程序第一次存储数据时,Visual Studio输出窗口中输出消息:"Resource Not Found"

enter image description here

如果您使用Fiddler捕获流量,您可以找到SDK发出请求以检测该文档是否存在于该集合中。

enter image description here

正如您所发现的,它只是由SDK编写的输出消息,它不会在您的代码中导致任何代码异常。