• Stars
    star
    79
  • Rank 408,196 (Top 9 %)
  • Language
    C#
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extensible RPC library with Typescript client generator

NuGet Stats Downloads License

CoreRPC

Extensible library for WCF-like RPC targeting netstandard1.3 (compatible with .NET, .NET Framework, Mono and .NET Core). TCP transport supports connection pooling and multiplexing requests within one connection, infrastructure itself allows multiple "services" to be hosted inside one host. You may define your own handler factory or "routing" mechanism. Serializer (JSON.NET is used by default) is also easy to replace.

NuGet Packages

Install the following package into your projects.

Target CoreRPC Package NuGet
Standalone CoreRPC CoreBadge
ASP .NET Core CoreRPC.AspNetCore AspBadge

Protocol Definition

Put an interface into your shared library and reference it from both client and server apps:

public interface IService
{
    Task<string> Foo(int bar);
}

Server App

Implement the declared interface:

public class Service : IService
{
    public Task<string> Foo(int bar)
    {
        return Task.FromResult(bar.ToString());
    }
}

Configure the router and start your server using either TCP, HTTP, or Named Pipes:

// Configure the router.
var router = new DefaultTargetSelector();
router.Register<IService, Service>();
var engine = new Engine().CreateRequestHandler(router);

// Use Named Pipes.
new NamedPipeHost(engine).StartListening("AwesomePipeName");

// Or, use HTTP (ASP.NET Core).
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    app.UseCoreRPC("/rpc", engine);
}

Client App

Use the protocol your server uses, and call remote procedures!

// Use HTTP. 
var transport = new HttpClientTransport("http://example.com/rpc");

// Or, use Named Pipes.
var transport = new NamedPipeClientTransport("AwesomePipeName");

// Crete the proxy and call remote procedures!
var proxy = new Engine().CreateProxy<IService>(transport);
var res = await proxy.Foo(1);

Assembly Scanning

CoreRPC.AspNetCore package supports automatic RPC registration. All you need is to mark your classes with the RegisterRpc attribute. CoreRPC will discover and register classes marked with that attribute automatically once you add a call to .UseCoreRpc() to your app builder:

// Implement the shared interface.
[RegisterRpc(typeof(IService))]
public class Service : IService
{
    public Task<string> Foo(int bar)
    {
        return Task.FromResult(bar.ToString());
    }
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // CoreRPC will register the class maked with RegisterRpc automatically.
    app.UseCoreRpc("/rpc");
}

You can override the built-in assembly scanning engine and a few other options if necessary:

// This may be useful when writing integrational tests, etc.
app.UseCoreRpc("/rpc", config => 
{
    // By default, CoreRPC uses camel case resolver, but you can override that.
    config.JsonSerializer.ContractResolver = new DefaultContractResolver();
    config.RpcTypeResolver = () =>
    {
        // Skip abstract classes and types not marked with [RegisterRpc] (default behavior).
        var assembly = Assembly.GetAssembly(typeof(Startup));
        return assembly.DefinedTypes
            .Where(type => !type.IsAbstract || type.IsInterface && 
                   type.GetCustomAttribute<RegisterRpcAttribute>() != null);
    };
    
    // You can also add a custom RPC method call interceptor.
    config.Interceptors.Add(new MyMethodCallInterceptor());
});

RPC Interception

If you need to intercept RPCs, implement the IMethodCallInterceptor interface and register it:

// The simplest interceptor that apparently does nothing.
public class Interceptor : IMethodCallInterceptor
{
    public Task<object> Intercept(MethodCall call, object context, Func<Task<object>> invoke)
    {
        // Add your custom logic here.
        return invoke();
    }
}

// Register the method call interceptor.
app.UseCoreRpc("/rpc", config => config.Interceptors.Add(new Interceptor()));

TypeScript API Generation

CoreRPC provides you an ability to generate TypeScript code for your server-side API.

// Store the generated TS code somewhere, e.g. save it to 'api.ts' file.
// Putting such C# code into your Startup.cs file might be a good idea.
string generatedCode = AspNetCoreRpcTypescriptGenerator.GenerateCode(types, config =>
{
    config.ApiFieldNamingPolicy = type => type.Replace("Rpc", string.Empty);
    config.DtoFieldNamingPolicy = TypescriptGenerationOptions.ToCamelCase;
});

Usage Examples

See tests for more examples.

More Repositories

1

XamlX

General purpose pluggable XAML compiler with no runtime dependencies.
C#
310
star
2

evhttp-sharp

libevent2-based HTTP server for C#
C#
138
star
3

skypetab-ng

A program that adds tabs to Skypeâ„¢ for Linux
C++
78
star
4

NObservable

MobX like observable state management library with Blazor support
C#
67
star
5

Avalonia.BattleCity

C#
42
star
6

Avalonia-Silk.NET-Example

C#
31
star
7

Avalonia.Ide

C#
30
star
8

avalonia-net-core-example

Example for running AvaloniaUI on .NET Core
PowerShell
28
star
9

MicroCom

IDL-based COM interop codegen
C#
25
star
10

MonoLib

Library with classes from Mono to bring modern C# language features (async/await) to .NET 2.0
C#
17
star
11

Numerge

Nuget package merging utiility
C#
17
star
12

pipedrive-net

.NET client for Pipedrive API ( https://developers.pipedrive.com/v1 )
C#
12
star
13

prowingen

prOWINgen - C#/Mono bindings for proxygen
C#
11
star
14

example-avalonia-huge-tree

C#
9
star
15

Avalonia-unit-testing-with-headless-platform

C#
9
star
16

LargeBson

C#
8
star
17

dotnext-akka-demo

Demo for .NEXT
C#
8
star
18

Nancy.Hosting.Event.2

libevent2-based host for NancyFx
C#
8
star
19

NWayland

.NET Wayland bindings, currently WIP
C#
7
star
20

avalonia-dnd-example

C#
7
star
21

hal-json-net

HAL JSON support for Json.Net
C#
6
star
22

NotifyHelper

INotifyPropertyChanged autogenerator
C#
5
star
23

evdev-sharp

A library for consuming evdev-capable devices
C#
5
star
24

SlimDock

C#
4
star
25

Bridge.Razor

Razor support for bridge.net
C#
4
star
26

monodevelop-appmenu

MonoDevelop.AppMenu
C#
4
star
27

packagereference-migrator-script

Migrates your project to new PackageReference format
C#
4
star
28

LightMvvm

C#
4
star
29

simple-clr-host

C
3
star
30

webmerge

A small utility that takes a web page and then merges scripts/css/images to a single HTML file.
C++
3
star
31

Pechka

C#
3
star
32

SkaderToy

C#
3
star
33

AvaloniaMonoDemo

C#
3
star
34

AvalonialessXamlExample

C#
2
star
35

simple-managed-websocket-server

C#
2
star
36

how-skype-fix-vulnerabilities

2
star
37

remoteui

TypeScript
2
star
38

dotnext2018spb

2
star
39

ScaleWine

A program for scaling wine virtual desktops
C++
2
star
40

CoreRPC-React-MobX

C#
2
star
41

PageAlloc

C#
2
star
42

scgi-sharp

ScgiSharp
C#
2
star
43

PhoneWave

C#
2
star
44

distributed-task-queue

Wrapper around RabbitMQ for distributing tasks (does NOT follow pub-sub concept of most AMQP-related libraries).
C#
2
star
45

dotnext2016spb

C#
2
star
46

msk.net-2016-08-11

JavaScript
2
star
47

custom-tfm-experiment

C#
1
star
48

dotnext2019spb

C#
1
star
49

simple-settings-example

C#
1
star
50

AvAotBench

C#
1
star
51

avalonia-catalog

example
C#
1
star
52

ngettext

Simple utility for extraction L._("something") from code and .pot-file generation
C#
1
star
53

ReplaceDllRepro

C#
1
star
54

baseimage-vnext

Makefile
1
star
55

acryllic-skshader-attempt

C#
1
star
56

msbuild-talk-2017-12-16

C#
1
star
57

Avalonia.XamarinForms

Xamarin forms integration (experimental and incomplete)
C#
1
star