如何实现异步节俭服务器?
How can I implement an async thrift server?
我在 thrift 上使用以下命令生成异步 c# class。
thrift-0.12.0.exe --gen csharp:async file.thrift
然后我在 c# 中以异步类型实现所有定义的方法。
现在我的问题是:我如何 运行 在 c# 中以异步类型工作的节俭服务器?
下面来自 GitHub 上的 thrift 存储库的示例是同步的。我需要它的异步版本。请有人帮助我。
public static void Main()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new Calculator.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
Console.WriteLine("done.");
}
你快到了。
thrift-0.12.0.exe --gen csharp:async file.thrift
这将为每个服务生成两个接口:"traditional" 同步 IFace
和一个名为 IAsync
的异步变体,这是您需要实现的。
此外,您希望使用 YourService.AsyncProcessor
.
而不是使用 YourService.Processor
实现
0.13.0 的新 netstd 实现(将在未来版本中废弃 C# 和 netcore 绑定)仅 支持异步变体。
例子
Example.thrift
namespace * Example
service Example {
double Ping(1: double input)
}
生成Example.cs
namespace Example
{
public interface IAsync {
Task<double> PingAsync(double input);
}
// more code ...
}
ExampleHandler.cs 实施您的服务
namespace ExampleSvc
{
public class HandlerImpl : Example.Example.IAsync
{
public Task<double> PingAsync(double input, CancellationToken cancel)
{
return Task.FromResult(input);
}
}
}
ServerMain.cs 设置服务器
// create protocol factory, we use framed compact
var protoFactory = new TCompactProtocol.Factory();
var transFactory = new TFramedTransport.Factory();
var handler = new ExampleSvc.HandlerImpl();
var processor = new Example.Example.AsyncProcessor(handler);
var servertrans = new TServerSocketTransport(9000);
var serverEngine = new TThreadPoolAsyncServer(processor, servertrans, transportFactory, protocolFactory);
serverEngine.ServeAsync(CancellationToken.None).Wait();
特别是后者是从一个更大的 netstd 源文件组装而成,并简化为服务器作为示例。我没有测试这个例子,但它应该是这样的。
我在 thrift 上使用以下命令生成异步 c# class。
thrift-0.12.0.exe --gen csharp:async file.thrift
然后我在 c# 中以异步类型实现所有定义的方法。 现在我的问题是:我如何 运行 在 c# 中以异步类型工作的节俭服务器? 下面来自 GitHub 上的 thrift 存储库的示例是同步的。我需要它的异步版本。请有人帮助我。
public static void Main()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new Calculator.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
Console.WriteLine("done.");
}
你快到了。
thrift-0.12.0.exe --gen csharp:async file.thrift
这将为每个服务生成两个接口:"traditional" 同步 IFace
和一个名为 IAsync
的异步变体,这是您需要实现的。
此外,您希望使用 YourService.AsyncProcessor
.
YourService.Processor
实现
0.13.0 的新 netstd 实现(将在未来版本中废弃 C# 和 netcore 绑定)仅 支持异步变体。
例子
Example.thrift
namespace * Example
service Example {
double Ping(1: double input)
}
生成Example.cs
namespace Example
{
public interface IAsync {
Task<double> PingAsync(double input);
}
// more code ...
}
ExampleHandler.cs 实施您的服务
namespace ExampleSvc
{
public class HandlerImpl : Example.Example.IAsync
{
public Task<double> PingAsync(double input, CancellationToken cancel)
{
return Task.FromResult(input);
}
}
}
ServerMain.cs 设置服务器
// create protocol factory, we use framed compact
var protoFactory = new TCompactProtocol.Factory();
var transFactory = new TFramedTransport.Factory();
var handler = new ExampleSvc.HandlerImpl();
var processor = new Example.Example.AsyncProcessor(handler);
var servertrans = new TServerSocketTransport(9000);
var serverEngine = new TThreadPoolAsyncServer(processor, servertrans, transportFactory, protocolFactory);
serverEngine.ServeAsync(CancellationToken.None).Wait();
特别是后者是从一个更大的 netstd 源文件组装而成,并简化为服务器作为示例。我没有测试这个例子,但它应该是这样的。