Codecooo.FlutterSharpRpc 0.1.5

dotnet add package Codecooo.FlutterSharpRpc --version 0.1.5
                    
NuGet\Install-Package Codecooo.FlutterSharpRpc -Version 0.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Codecooo.FlutterSharpRpc" Version="0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Codecooo.FlutterSharpRpc" Version="0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="Codecooo.FlutterSharpRpc" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Codecooo.FlutterSharpRpc --version 0.1.5
                    
#r "nuget: Codecooo.FlutterSharpRpc, 0.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Codecooo.FlutterSharpRpc@0.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Codecooo.FlutterSharpRpc&version=0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=Codecooo.FlutterSharpRpc&version=0.1.5
                    
Install as a Cake Tool

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, inherit RpcNotifier 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 : RpcNotifier 
{
    public async Task BackgroundUpdate()
    {
        int tick = 0;

        while (true)
        {
            string text = $"Update from C# server notifications. The current tick is {tick}";
            await JsonRpc.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. Not only that but it also comes built-in with ILogger integration in C# using ILoggerProvider, you can register it on one line.

bullder.AddLogging(builder =>
    builder.AddRpcLogging();
);

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. <br> 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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.1.5 87 2/10/2026
0.1.4 86 2/9/2026
0.1.3 91 2/9/2026
0.1.2-alpha 89 1/7/2026
0.1.0-alpha 96 1/4/2026
0.0.5 101 1/7/2026
0.0.4 95 1/4/2026
0.0.3 92 1/2/2026
0.0.2 104 1/1/2026