Type-safe client-server communication for C# featuring Bridge.NET and NancyFx. It is a Remote Procedure Call (RPC) implementation for Bridge.NET as the client and Nancy as the Server.
Introducing Cable: Deep Dive Into Building Type-Safe Web Apps in C# with Bridge.NET and Nancy
Cable.ArticleSample Project used in the article to demonstrate Cable. The repo can be used a template for client/server apps. Everything is already set up.
Cable.StandaloneConverter Project that demonstrates using the Cable JSON converter stand-alone without HTTP abstractions
Install Cable.Nancy
from nuget
Install-Package Cable.Nancy
public class Student
{
public string Name { get; set; }
public int Id { get; set; }
public string[] Subjects { get; set; }
public DateTime DateOfBirth { get; set; }
}
public interface IStudentService
{
Task<IEnumerable<Student>> GetAllStudents();
Task<Student> TryFindStudentByName(string name);
}
public class StudentServiceModule : NancyModule
{
public StudentServiceModule(IStudentService service)
{
// automatically generate routes for the provided implementation of IStudentService
NancyServer.RegisterRoutesFor(this, service);
}
}
Install Cable.Bridge
from nuget:
Install-Package Cable.Bridge
Reference the file SharedTypes.cs
into your client project, making the types available.
Create a typed proxy and talk to server directly:
using System;
using Cable.Bridge;
using ServerSide;
namespace ClientSide
{
public class Program
{
public static async void Main()
{
// resolve a typed proxy
var studentService = BridgeClient.Resolve<IStudentService>();
// call server just by calling the function
var students = await studentService.GetAllStudents();
// use the results directly
foreach (var student in students)
{
var name = student.Name;
var age = DateTime.Now.Year - student.DateOfBirth.Year;
Console.WriteLine($"{name} is {age} years old");
}
}
}
}