FlutterCsharpRpc 0.0.1

dotnet add package FlutterCsharpRpc --version 0.0.1                
NuGet\Install-Package FlutterCsharpRpc -Version 0.0.1                
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="FlutterCsharpRpc" Version="0.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FlutterCsharpRpc --version 0.0.1                
#r "nuget: FlutterCsharpRpc, 0.0.1"                
#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.
// Install FlutterCsharpRpc as a Cake Addin
#addin nuget:?package=FlutterCsharpRpc&version=0.0.1

// Install FlutterCsharpRpc as a Cake Tool
#tool nuget:?package=FlutterCsharpRpc&version=0.0.1                

FlutterCsharpRPC

pub package issues-closed issues-open

Flutter Csharp RPC

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.

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 csharp_rpc package as a new dependency with the following command:

PS c:\src\flutter_project> flutter pub add csharp_rpc

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

import 'package:csharp_rpc/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 FlutterCsharpRpc Nuget package as a new dependency with the following command:

PS c:\src\flutter_project> dotnet add package FlutterCsharpRpc

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;
    }
}

âš¡ Demo

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


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. 
.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.0.1 410 4/26/2023