-
Notifications
You must be signed in to change notification settings - Fork 16
RPC Server: Advanced Usages
Chen edited this page May 30, 2017
·
7 revisions
If you have read RPC Server: Getting Started, here you can learn some more advanced usages on server-side RPC handling.
Like "middlewares" in ASP.NET Core, in JsonRpc.Standard, it's possible to intercept the incoming JSON-RPC Request messages and customize the way they are processed. In JsonRpc.Standard, the middlewares are added by calling JsonRpcServiceHostBuilder.Intercept
. For example, if you want to log the timing of each requests and responses, you may insert a middleware before the actual method dispatching and invocation step. Here is how it's done in UnitTestProject1.Utility
public static IJsonRpcServiceHost CreateJsonRpcServiceHost(UnitTestBase owner)
{
var builder = new JsonRpcServiceHostBuilder();
builder.Register(typeof(Utility).GetTypeInfo().Assembly);
builder.ContractResolver = DefaultContractResolver;
var globalSw = Stopwatch.StartNew();
var session = new SessionFeature();
if (owner.Output != null)
{
builder.Intercept(async (context, next) =>
{
var sw = Stopwatch.StartNew();
owner.Output.WriteLine("{0}> {1}", globalSw.Elapsed, context.Request);
try
{
context.Features.Set(session);
await next();
owner.Output.WriteLine("{0}< {1}", globalSw.Elapsed, context.Response);
}
finally
{
owner.Output.WriteLine("Server: Ellapsed time: {0}", sw.Elapsed);
}
});
}
builder.LoggerFactory = owner.LoggerFactory;
return builder.Build();
}
in this xUnit
-based unit test project, I used ITestOutputHelper
provided by xUnit
to print the timings, which will give me the following output
00:00:00.3802118> {"id":"32106157#1","method":"one","jsonrpc":"2.0"}
00:00:00.8513458< {"id":"32106157#1","result":1,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.4970798
00:00:00.9267219> {"id":"32106157#2","method":"one","params":{"negative":false},"jsonrpc":"2.0"}
00:00:00.9487014< {"id":"32106157#2","result":1,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.0223802
00:00:00.9496177> {"id":"32106157#3","method":"one","params":{"negative":true},"jsonrpc":"2.0"}
00:00:00.9500884< {"id":"32106157#3","result":-1,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.0020213
00:00:00.9521625> {"id":"32106157#4","method":"two","jsonrpc":"2.0"}
00:00:00.9572604< {"id":"32106157#4","result":2,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.0055367
00:00:00.9582994> {"id":"32106157#5","method":"two","params":{"negative":false},"jsonrpc":"2.0"}
00:00:00.9586298< {"id":"32106157#5","result":2,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.0005468
00:00:00.9590794> {"id":"32106157#6","method":"two","params":{"negative":true},"jsonrpc":"2.0"}
00:00:00.9593365< {"id":"32106157#6","result":-2,"jsonrpc":"2.0"}
Server: Ellapsed time: 00:00:00.0004628