You can perform Remote Procedure Calls (RPC) across processes using IRemoteRequestHandler<TRequest, TResponse> on the server side and IRemoteRequestHandler<TRequest, TResponse> on the client side. This requires TcpInterprocess or NamedPipeInterprocess to be enabled.
Server Configuration
Set HostAsServer = true in the interprocess options.
Implementation Example
Server Handler:
public class MyAsyncHandler : IAsyncRequestHandler<int, string>
{
public async ValueTask<string> InvokeAsync(int request, CancellationToken cancellationToken = default)
{
await Task.Delay(1);
return request == -1 ? throw new Exception("NO -1") : $"ECHO:{request}";
}
}
Client Call:
async void A(IRemoteRequestHandler<int, string> remoteHandler)
{
var v = await remoteHandler.InvokeAsync(9999);
Console.WriteLine(v); // ECHO:9999
}
Host.CreateDefaultBuilder()
.ConfigureServices((ctx, services) => {
services.AddMessagePipe()
.AddTcpInterprocess("127.0.0.1", 3215, x => {
x.HostAsServer = true;
});
});