当我将thrift客户端引用放在using块中时,我得到一个TTransportException:"无法读取,远程端已关闭异常。"
//THRIFT IDL
namespace csharp ReferenceData.API
exception EntityDoesNotExistException {
1: string Key;
}
service ReferenceDataService {
bool HelloWorld() throws (1: EntityDoesNotExistException entityDoesNotExistException);
}
namespace Server
{
public class Service: ReferenceDataService.Iface
{
public bool HelloWorld()
{
throw new EntityDoesNotExistException(){Key = "TEST KEY"};
}
}
}
namespace Server
{
class Program
{
static void Main(string[] args)
{
try
{
var processor = new ReferenceData.API.ReferenceDataService.Processor(new Service());
TServerTransport serverTransport = new TServerSocket(9094);
TServer server = new TThreadedServer(processor, serverTransport);
Console.WriteLine("Running the Payroll Service on port {0}...", "9094");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
Console.ReadLine();
}
}
}
namespace Client
{
class Program
{
static void Main(string[] args)
{
var transport = new Thrift.Transport.TSocket("127.0.0.1", 9094);
transport.Open();
var service = new ReferenceDataService.Client(new Thrift.Protocol.TBinaryProtocol(transport));
using (service)
{
try
{
service.HelloWorld();
}
catch (EntityDoesNotExistException ex)
{
Console.WriteLine(ex.Key);
}
}
Console.ReadLine();
}
}
}
远程方法调用实际上成功,但我观察到Processor.Process方法被调用两次,这是第二次触发异常的调用。 Process方法应该被调用两次吗?我是否应该像这样明确地调用Dispose,我认为我应该尽快关闭套接字。
答案 0 :(得分:1)
行为是设计的。
在连接结束之前调用Process()
方法,通常是因为客户端断开连接。在这种情况下,ReadMessageBegin()
会失败,并且会在Process()
循环正下方的TThreadedServer.cs中发现此异常。
对于好奇:例外是thrown in TTransport.ReadAll()