Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用ShutdowAsync()后如何重新启动gRPC服务器;在C#下?_C#_Grpc - Fatal编程技术网

调用ShutdowAsync()后如何重新启动gRPC服务器;在C#下?

调用ShutdowAsync()后如何重新启动gRPC服务器;在C#下?,c#,grpc,C#,Grpc,下面是一个教程,其中gRPC服务器声明为: Server server = new Server { Services = { RouteGuide.BindService(new RouteGuideImpl(features)) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; 然后 被称为,一些事情完成了,最后 server.ShutdownAsync().Wai

下面是一个教程,其中gRPC服务器声明为:

Server server = new Server
{
    Services = { RouteGuide.BindService(new RouteGuideImpl(features)) },
    Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
然后

被称为,一些事情完成了,最后

server.ShutdownAsync().Wait();
有人打电话来,但我试着

server.Start();
再次出现以下错误:

Exception encountered: System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Grpc.Core.Utils.GrpcPreconditions.CheckState(Boolean condition) in T:\src\github\grpc\src\csharp\Grpc.Core.Api\Utils\GrpcPreconditions.cs:line 88
   at Grpc.Core.Server.Start() in T:\src\github\grpc\src\csharp\Grpc.Core\Server.cs:line 151

是否每次要重新启动服务器对象时都要创建一个新的服务器对象实例?服务器对象没有实现Dispose方法,我担心内存泄漏。

我认为最好创建一个新的服务器对象。服务器对象不可重用,这是正常工作的。一旦关闭(shutdownsync()或KillAsync()),资源将被清理。要创建新服务器,只需创建一个
新服务器()
。它可以具有与原始服务器相同的参数并在相同的端口上侦听(假设旧服务器已成功关闭)。顺便说一句,关闭并重新创建服务器是一个有点不寻常的用例-通常你会启动一个服务器,让它保持尽可能长的活动时间。感谢@jantatermusch,这是为一个测试应用程序做的,我需要能够在任何时候任意更改服务器的端口
Exception encountered: System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Grpc.Core.Utils.GrpcPreconditions.CheckState(Boolean condition) in T:\src\github\grpc\src\csharp\Grpc.Core.Api\Utils\GrpcPreconditions.cs:line 88
   at Grpc.Core.Server.Start() in T:\src\github\grpc\src\csharp\Grpc.Core\Server.cs:line 151