.net Grpc System.ObjectDisposedException:安全句柄已关闭

.net Grpc System.ObjectDisposedException:安全句柄已关闭,.net,google-app-engine,google-cloud-platform,asp.net-core-2.0,google-cloud-pubsub,.net,Google App Engine,Google Cloud Platform,Asp.net Core 2.0,Google Cloud Pubsub,当使用Pub/Sub停止服务并在Google App Engine上运行时,会收到以下stacktrace System.ObjectDisposedException: Safe handle has been closed at System.Runtime.InteropServices.SafeHandle.DangerousAddRef at System.StubHelpers.StubHelpers.SafeHandleAddRef at Grpc.Core

当使用Pub/Sub停止服务并在Google App Engine上运行时,会收到以下stacktrace

System.ObjectDisposedException: Safe handle has been closed
    at System.Runtime.InteropServices.SafeHandle.DangerousAddRef
    at System.StubHelpers.StubHelpers.SafeHandleAddRef
    at Grpc.Core.Internal.ChannelSafeHandle.CreateCall
    at Grpc.Core.Internal.AsyncCall`2.CreateNativeCall
    at Grpc.Core.Internal.AsyncCall`2.Initialize
    at Grpc.Core.Internal.AsyncCall`2.StartDuplexStreamingCall
    at Grpc.Core.Calls.AsyncDuplexStreamingCall[TRequest,TResponse]
    at Grpc.Core.DefaultCallInvoker.AsyncDuplexStreamingCall[TRequest,TResponse]
    at Grpc.Core.Internal.InterceptingCallInvoker.AsyncDuplexStreamingCall[TRequest,TResponse]
    at Google.Cloud.PubSub.V1.Subscriber.SubscriberClient.StreamingPull
    at Google.Api.Gax.Grpc.ApiBidirectionalStreamingCall.<>c__DisplayClass0_0`2.<Create>b__0
    at Google.Api.Gax.Grpc.ApiBidirectionalStreamingCall`2.Call
    at Google.Cloud.PubSub.V1.SubscriberServiceApiClientImpl.StreamingPull
    at Google.Cloud.PubSub.V1.SubscriberClientImpl.SingleChannel.<StartAsync>d__18.MoveNext
对于该项目,我们使用ASP.NET内核和库来处理发布/订阅通信。使用发布/订阅仿真器在本地调试时不存在此问题。因此,我们假设这与appengine逐步淘汰docker容器的方式有关?仅捕获异常并不是一个令人满意的解决方案,因为我们有可能使处理过的消息未被确认。我们能否以任何方式解决此问题,以便以安全的方式停止服务


提前谢谢。

我发现错误在我的代码中。。。我正在ASP.NET Core中使用IHostedService,并使用以下方法停止订阅服务器:

cancellationToken.Registerasync=>{await subscriber.StopAsynctoken}


但是,这将触发并忘记,因此在处理服务提供商内部的对象之前,不会等待客户端正确停止。由于我使用了Google.Cloud.Diagnostics.AspNetCore,它在服务提供商中注册,并使用Grpc记录一些有用的调试语句,因此可以在最终使用之前对其进行处理。

这可能与您是否认为您可以提供重现该问题的最小示例有关?我发现这是我自己的代码。我已经发布了一个答案,如果其他人和我犯同样的错误。