SciTech.Rpc.NetGrpc.Client
1.0.0-rc.174
dotnet add package SciTech.Rpc.NetGrpc.Client --version 1.0.0-rc.174
NuGet\Install-Package SciTech.Rpc.NetGrpc.Client -Version 1.0.0-rc.174
<PackageReference Include="SciTech.Rpc.NetGrpc.Client" Version="1.0.0-rc.174" />
<PackageVersion Include="SciTech.Rpc.NetGrpc.Client" Version="1.0.0-rc.174" />
<PackageReference Include="SciTech.Rpc.NetGrpc.Client" />
paket add SciTech.Rpc.NetGrpc.Client --version 1.0.0-rc.174
#r "nuget: SciTech.Rpc.NetGrpc.Client, 1.0.0-rc.174"
#:package SciTech.Rpc.NetGrpc.Client@1.0.0-rc.174
#addin nuget:?package=SciTech.Rpc.NetGrpc.Client&version=1.0.0-rc.174&prerelease
#tool nuget:?package=SciTech.Rpc.NetGrpc.Client&version=1.0.0-rc.174&prerelease
SciTech.Rpc
SciTech.Rpc is a high-performance, code-first remote procedure call framework for .NET. Like WCF, services are defined by attributed interfaces in code, not by external definition files. It handles remote calls on all levels, from services on the internet over HTTP/2 (using gRPC) to calls between processes on the same machine and within a process.
SciTech.Rpc has been used in production for several years. Version 1.0 is currently in the release candidate stage.
There are two communication layers:
- Lightweight, a very efficient implementation based on System.IO.Pipelines. It communicates over TCP (with TLS or Negotiate authentication), named pipes, or directly within a process, and includes service discovery on the local network. The wire format is specified in docs/lightweight-protocol.md.
- NetGrpc, based on the fully managed gRPC for .NET. Services are hosted in ASP.NET Core and can be accessed over HTTP/2.
The libraries target .NET Framework 4.7.2, .NET 8 and .NET 10. The NetGrpc server requires ASP.NET Core (.NET 8 or later).
Packages
| Package | Contents |
|---|---|
| SciTech.Rpc | The core framework, used by the other packages. Includes a Roslyn analyzer for RPC service interfaces. |
| SciTech.Rpc.Lightweight | The Lightweight client and server. |
| SciTech.Rpc.NetGrpc | The gRPC server, for ASP.NET Core. |
| SciTech.Rpc.NetGrpc.Client | The gRPC client. |
| SciTech.Rpc.Protobuf | A Protobuf serializer, based on protobuf-net. |
| SciTech.Rpc.DataContract | A DataContract serializer. |
| SciTech.Rpc.Resources | Remote resources and locks. |
| SciTech.Core | Utility types used by SciTech.Rpc. |
Defining RPC services
An interface is marked as an RPC service by applying the [RpcService] attribute:
[RpcService]
public interface IThermostatService
{
Task<double> GetTemperatureAsync();
Task SetTargetTemperatureAsync(double temperature, CancellationToken cancellationToken);
double TargetTemperature { get; set; }
event EventHandler<TemperatureChangedEventArgs> TemperatureChanged;
}
All public members of an RPC service are RPC operations, so there's no need to mark each member with an
attribute. By default, the name of a service is the name of the interface without the initial "I", in the
namespace of the interface. It can be changed with the Name and Namespace properties of [RpcService].
An operation is identified by its name only, so overloaded members are not allowed. An operation can be
implemented as a blocking method, an asynchronous method, or an asynchronous method with a CancellationToken.
The client and server definitions may differ: a client side interface can, for example, define both a blocking
and an asynchronous variant of the same operation, as long as it refers to the server side definition:
[RpcService(ServerDefinitionType = typeof(IThermostatService))]
public interface IThermostatServiceClient : IThermostatService, IRpcProxy
{
Task<double> GetTargetTemperatureAsync();
}
The supported kinds of operations are:
| Operation | Declaration |
|---|---|
| Unary call | int Add(int a, int b); or Task<int> AddAsync(int a, int b); |
| Property | double Value { get; set; } |
| Event | event EventHandler<TEventArgs> Changed; |
| Server streaming | IAsyncEnumerable<Reading> GetReadings(int count, CancellationToken cancellationToken); |
| Client streaming | Task<int> Upload(string name, IAsyncEnumerable<Sample> samples); |
| Callback | Task MeasureAsync(int count, Action<Reading> callback); |
Event handlers are invoked in the synchronization context provided when the proxy was retrieved, e.g. the user interface thread.
Publishing services
A service implementation is published by an IRpcServer, either as a singleton, which clients access by the
service name, or as an instance. A published instance is identified by an RpcObjectRef<TService>, which can be
returned to clients, similar to an ObjRef in .NET Remoting:
var server = new LightweightRpcServer();
// Clients connect to "localhost", and the server only listens on the loopback address.
server.AddEndPoint(new TcpRpcEndPoint("localhost", "127.0.0.1", 50052, false));
// Singletons are accessed by service name.
server.PublishSingleton<IThermostatService>(new ThermostatService());
server.Start();
// Instances are identified by an RpcObjectRef, which can be returned to a client.
using var publishedSensor = server.ServicePublisher.PublishInstance<ISensorService>(new SensorService());
RpcObjectRef<ISensorService> sensorRef = publishedSensor.Value;
// ...
await server.ShutdownAsync();
The first argument of TcpRpcEndPoint is the host name that clients use to connect, and the second the IP address
to listen on. To allow connections from other machines, listen on all network interfaces instead, with
bindToAllInterfaces: true, and require authentication (see Authentication). Listening on a host
name, without an IP address and with bindToAllInterfaces: false, is not recommended, since a host name may resolve
to several addresses, but only one of them is used.
The service interfaces are registered with an RpcServiceDefinitionsBuilder. Publishing a service registers its
interfaces automatically, but registration is only possible before the server is started. A service returned by an
operation, e.g. ISensorService GetSensor(string name), is converted to an RpcObjectRef and published
automatically if the AllowAutoPublish option is enabled.
See the LightweightServer example for a complete server.
Publishing with ASP.NET Core gRPC
The NetGrpc server is configured in the ASP.NET Core startup:
IServiceCollection.AddNetGrpc()adds the NetGrpc services, and the ASP.NET Core gRPC services.IServiceCollection.RegisterRpcService<TService>()registers a service interface.IApplicationBuilder.PublishRpcSingleton<TService, TServiceImpl>()publishes a singleton, e.g. one that is created by the service provider for each call.IEndpointRouteBuilder.MapNetGrpcServices()maps the registered services to gRPC endpoints. Services must be registered before this is called.
See the NetGrpcServer example.
Connecting to services
A client uses a proxy, which implements the service interface, retrieved from a connection:
await using var connection = new TcpRpcConnection(new RpcConnectionInfo(new Uri("lightweight.tcp://localhost:50052")));
var thermostat = connection.GetServiceSingleton<IThermostatServiceClient>();
double temperature = await thermostat.GetTemperatureAsync();
var sensor = connection.GetServiceInstance(sensorRef);
NetGrpcConnection connects to a NetGrpc server, e.g. at grpc://localhost:50051, and NamedPipeRpcConnection
and InprocRpcConnection provide the other Lightweight transports. An RpcConnectionManager creates and reuses
connections to any reachable server, based on the connection info of an RpcObjectRef. See the
GreeterClient example.
IRpcProxy.TryCastAsync<TService>() checks whether the object behind a proxy implements another service, for
example a newer version of a service.
Service discovery
A Lightweight server with a LightweightDiscoveryEndPoint can be discovered by clients on the local network,
over UDP multicast. A client can ask for the singleton services it needs, and then only servers that publish at
least one of them answer:
var discoveryClient = new LightweightDiscoveryClient();
discoveryClient.ServerDiscovered += (s, e) =>
{
var server = e.DiscoveredServer;
// Services lists which of the requested services the server publishes.
bool isCompatible = server.Services.Contains("Thermostat.ThermostatService2");
server.Properties.TryGetValue("version", out var version);
Console.WriteLine($"{server.ConnectionInfo.DisplayName}, version {version}, compatible: {isCompatible}");
};
await discoveryClient.FindServicesAsync(
new[] { "Thermostat.ThermostatService", "Thermostat.ThermostatService2" },
cancellationToken);
A server describes itself with server properties, application-defined key/value pairs set in
RpcServerOptions.ServerProperties, e.g. a product name and version. Discovery is unauthenticated, so it should
only be used as a pre-filter. After connecting, IRpcChannel.GetServerInfoAsync() returns the same information
over the authenticated connection, together with the singleton services the server publishes.
Versioning services
A published service interface should not be changed: operations must not be removed, and their parameters and
return types must not change. A client can't detect such a change, and a changed signature may be deserialized
incorrectly. New operations are added in a new interface, e.g. IThermostatService2 : IThermostatService with its
own service name, and clients check whether it's available: by discovery or GetServerInfoAsync for a
singleton, and by TryCastAsync for an object.
Errors and faults
An exception thrown by a service implementation is not sent to the client: the client gets an
RpcFailureException with a generic message. To return error information, the exception is turned into an RPC
fault, with a fault code, a message and optional details:
- Exception converters convert an ordinary exception to a fault on the server side, and back to the exception
on the client side, e.g.
[RpcFaultConverter("DeviceAction", typeof(DeviceActionException))]on a service or an operation. Converters that apply to all services are registered inRpcServerOptions.ExceptionConvertersandRpcClientOptions.ExceptionConverters. - Declared faults are declared with
[RpcFault(typeof(TFault))], similar to[FaultContract]in WCF. The implementation throws anRpcFaultException<TFault>, which reaches the client with its details.
Faults are handled the same way for all kinds of operations, including streaming operations. See Exception handling for the details.
The framework throws these exceptions:
RpcFailureException: an undeclared exception in an operation, or another failure of the call.RpcFaultException,RpcFaultException<TFault>: a declared fault.RpcServiceUnavailableException: the service singleton or instance is not published on the server.RpcCommunicationException: a communication error, e.g. the server is unreachable or disconnected.RpcDefinitionException: an error in an RPC service definition.TimeoutExceptionandOperationCanceledException: the call timed out or was cancelled.
Authentication
A Lightweight TCP end point can require TLS (SslServerOptions) or Negotiate (NegotiateServerOptions)
authentication, and the client provides the matching SslClientOptions or NegotiateClientOptions. NetGrpc uses
the authentication of ASP.NET Core, and operations or services can require authorization with [RpcAuthorize]
and [RpcAllowAnonymous].
Metadata, e.g. security tokens, can be attached to calls with RpcClientCallInterceptor and read with
RpcServerCallInterceptor, registered in the client and server options.
Serialization
Lightweight uses JSON (JsonRpcSerializer) by default, and NetGrpc uses Protobuf (ProtobufRpcSerializer).
The serializer is configured in the client and server options, and both sides must use the same one. Types that
are serialized polymorphically are declared with [RpcKnownType], which is supported by all serializers.
Building SciTech.Rpc
Building requires the .NET SDK specified in global.json. Running the .NET Framework tests requires Windows.
dotnet build --configuration Release
dotnet test --configuration Release --framework net8.0
The tests use fixed TCP ports, so run one target framework at a time.
The documentation site, with the articles in docs and the API reference, is built with
docfx into docs/_site:
dotnet dnx docfx docs/docfx.json
Feedback and contributions
Feedback, issues and pull requests are welcome. Use the GitHub issues and pull requests, or e-mail the maintainer, Andreas Suurkuusk. Security issues should be reported as described in SECURITY.md. Contributors are listed in CONTRIBUTORS.md.
SciTech.Rpc is licensed under the BSD 3-Clause License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- Grpc.Net.Client (>= 2.84.0)
- SciTech.Rpc (>= 1.0.0-rc.174)
- SciTech.Rpc.Protobuf (>= 1.0.0-rc.174)
-
net10.0
- Grpc.Net.Client (>= 2.84.0)
- SciTech.Rpc (>= 1.0.0-rc.174)
- SciTech.Rpc.Protobuf (>= 1.0.0-rc.174)
-
net8.0
- Grpc.Net.Client (>= 2.84.0)
- SciTech.Rpc (>= 1.0.0-rc.174)
- SciTech.Rpc.Protobuf (>= 1.0.0-rc.174)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-rc.174 | 66 | 11/9/2026 |
| 1.0.0-rc.173 | 80 | 10/27/2026 |
| 1.0.0-beta.27 | 376 | 2/10/2022 |
| 1.0.0-beta.26 | 308 | 2/10/2022 |
| 1.0.0-beta.25 | 337 | 12/22/2021 |
| 1.0.0-beta.24 | 354 | 10/7/2021 |
| 1.0.0-beta.23 | 368 | 10/6/2021 |
| 1.0.0-beta.22 | 403 | 9/9/2021 |
| 1.0.0-beta.21 | 347 | 8/18/2021 |
| 1.0.0-beta.20 | 359 | 8/13/2021 |
| 1.0.0-beta.18 | 372 | 2/3/2021 |
| 1.0.0-beta.17 | 402 | 2/1/2021 |
| 1.0.0-beta.16 | 369 | 1/26/2021 |
| 1.0.0-beta.15 | 386 | 1/18/2021 |
| 1.0.0-beta.14 | 409 | 1/8/2021 |
| 1.0.0-beta.13 | 388 | 1/4/2021 |