FlutterCsharpRPC pub package

Flutter Csharp RPC

This repo is a fork from the original FlutterCsharpRpc. This fork update and improve upon the original one with feature such as Server notifications and a test for native AOT compatibility.

With this package we can execute C# code from Dart (Flutter) application via JSON-RPC protocol.

In run-time, we will create a C# child process and communicate with it via JSON-RPC protocol on the standard in/out (stdin/stdout) stream.

The JSON-RPC protocol let us invoke C# methods on the child process.

!IMPORTANT
For now, this package will work only with .Net (Core) projects, and not with .Net Framework

For example, we have the C# method:

public DateTime GetCurrentDateTime()
{
    return DateTime.Now;
}

And we call it from Dart code with the invoke method:

String currentDateTime = await csharpRpc.invoke(method: "GetCurrentDateTime");

We can also invoke method with array of parameters:

var sum = await csharpRpc.invoke<int>(method: "SumNumbers", params: [2, 3]);

And even send a typed request parameter:

var filesResult = await csharpRpc.invoke(
   method: "GetFilesInFolder",
   param: GetFilesInFolderRequest(folderPath: Directory.current.path)
);
var files = FilesInFolderResponse.fromJson(filesResult);

here we sending instance of request type GetFilesInFolderRequest, then we convert the result to a response type FilesInFolderResponse with the fromJson method, so we can have fully typed communication experience 🎉

📋 Dart/Flutter Setup

In your pubspec.yaml, add the codecooo_csharp_rpc package as a new dependency with the following command:

dart pub add codecooo_csharp_rpc

In your program code, create and use the CsharpRpc class to invoke methods:

import 'package:codecooo_csharp_rpc/codecooo_csharp_rpc.dart';

// create instance of CsharpRpc
var pathToCsharpExecutableFile = "<path_to_your_csharp_app>/CsharpApp.exe";

CsharpRpc csharpRpc = await CsharpRpc(pathToCsharpExecutableFile).start();

// invoke the C# method 'GetCurrentDateTime'
var currentDateTime = await csharpRpc.invoke(method: "GetCurrentDateTime");

📋 C# Setup

In your C# project, add the FlutterSharpRpc Nuget package as a new dependency with the following command:

dotnet add package Codecooo.FlutterSharpRpc

In your program code, start the JSON-RPC server by calling the StartAsync method:

using FlutterCsharpRpc;

static async Task Main(string[] args)
{
    // your program setup (DI, service, etc) here...

    // start the JSON-RPC server
    await CsharpRpcServer.StartAsync(new Server());
}

public class Server
{
    public DateTime GetCurrentDateTime()
    {
        return DateTime.Now;
    }
}

Send Notification From Server to Flutter

If you want your server to make notification to flutter, implement the IRpcNotifierAware to your server class and then once you start the server the server class will have notifier instance registered that you can use to notify flutter. Remember notification dont have any response so it is fire and forget for example background process update. Here is the example

public class Server : IRpcNotifierAware 
{
    // IRpcNotifier instance for sending notification
    private IRpcNotifier rpcNotifier;

    // Once you start the server the package automatically call this method 
    // so that your server class have the IRpcNotifier instance
    public void AttachNotifier(IRpcNotifier notifier)
    {
        rpcNotifier = notifier;
    }

    public async Task BackgroundUpdate()
    {
        int tick = 0;

        while (true)
        {
            string text = $"Update from C# server notifications. The current tick is {tick}";
            await rpcNotifier.NotifyAsync("updateProgress", text);
            RpcLog.Info($"Notifying client of the current tick {tick}");
            tick++;
            await Task.Delay(1500);
        }
    }
}

Then on the dart side you want to subscribe to notification event.

// we listen to notification coming from c# server
csharpRpc.notifications.listen((notif) {
    if (notif.method != 'updateProgress') return;
    updateProgress(notif.params);
});

Logging in C#

During RPC process when you want to log event you need to do that on STDERR rather than the usual STDOUT since that is used by RPC. This package provide a simple abstraction layer RpcLog class that automatically write to STDERR and format them the usual way of ASP.NET. If dont want it you can always just use Console.Error.WriteLine.

Native AOT Compatibility for C#

This package for c# is somewhat compatible for AOT and trimming. You need to provide your own JsonSerializerContext for your own types and register your methods explicitly using delegate to avoid reflection. To start the server you need to use StartWithExplicitAsync rather than the usual StartAsync. This is the example:

await CsharpRpcServer.StartWithExplicitAsync(new Server(), JsonContext.Default,
    async (rpc, server) =>
    {
        rpc.AddLocalRpcMethod("GetCurrentDateTime", server.GetCurrentDateTime);
        rpc.AddLocalRpcMethod("SumNumbers", server.SumNumbers);
        rpc.AddLocalRpcMethod("GetFilesInFolder", server.GetFilesInFolder);
    });

⚡ Demo

See the full demo of Flutter and C# app that communicate between them.

🤖 Code Generation

Note: this feature is optional and not needed for small applications

When using JSON-RPC we need our Dart classes to be serialize to JSON.

Also, it will be helpful if our csharp program have the same classes as our Flutter program so we can easily communicate between them.

To solve those problems and to speed up and enhance our development experience, we can use code generation to do the work for us.

Take a look on a example Flutter app that use code generation solution.

📦 Publish (Release Mode)

While in development (Debug Mode) our C# program can located anywhere (see in basic example), but when we build our Flutter app in Release mode, its time to move and publish the C# program into the Flutter build folder (see in basic example).

  • publish the C# into the Flutter Release-build folder (example)
  • when publishing the C# program, set it as SelfContained, so it will work on every device even if Dotnet is not installed

Note: Those publish instructions are also suitable publish/deploy with MSIX.

🙏 Credit

Thanks to YehudaKremer for making the original FlutterCsharpRpc which this package based from.
This package based on Michael K Snead's article on medium: Flutter, C# and JSON RPC


Tags: Csharp C# RPC flutter csharp flutter c# csharp ffi csharp json-rpc FlutterCsharpRpc StreamJsonRpc