The Communication library is network module using https://github.com/grpc/grpc.
.NET 8.0
c# 12.0
git clone https://github.com/s2quake/communication.git
cd communication
dotnet build
dotnet run --project src/JSSoft.Communication.Server
dotnet run --project src/JSSoft.Communication.Client
Run terminal and execute the command line below to create a solution and project
mkdir .example
dotnet new sln -o .example
dotnet new console -o .example/Server-Test
dotnet new console -o .example/Client-Test
dotnet sln .example add .example/Server-Test
dotnet sln .example add .example/Client-Test
dotnet add .example/Server-Test reference src/JSSoft.Communication
dotnet add .example/Client-Test reference src/JSSoft.Communication
Create IMyService.cs
file in the path .example/Server-Test.
Then copy and paste the code below into the file IMyService.cs
.
namespace Services;
public interface IMyService
{
Task<Guid> LoginAsync(string userID, CancellationToken cancellationToken);
Task<(string product, string version)> GetVersionAsync(CancellationToken cancellationToken);
}
Create MyService.cs
file in the path .example/Server-Test.
Then copy and paste the code below into the file MyService.cs
.
using JSSoft.Communication;
namespace Services;
sealed class MyService : ServerService<IMyService>, IMyService
{
public Task<Guid> LoginAsync(string userID, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
Console.WriteLine($"logged in: '{userID}'");
return Guid.NewGuid();
}, cancellationToken);
}
public Task<(string product, string version)> GetVersionAsync(CancellationToken cancellationToken)
{
return Task.Run(() =>
{
return ("MyServer", "1.0.0.0");
}, cancellationToken);
}
}
Create Program.cs
file in the path .example/Server-Test.
Then copy and paste the code below into the file Program.cs
.
using JSSoft.Communication;
using JSSoft.Communication.Extensions;
using Services;
var service = new MyService();
var serviceContext = new ServerContext([service]);
var token = await serviceContext.OpenAsync(cancellationToken: default);
Console.WriteLine("Server has been started.");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
var exitCode = await serviceContext.ReleaseAsync(token);
Environment.Exit(exitCode);
First, you need to include the file IMyService.cs
that you created when you created the Server-Test project.
Open .example/Client-Test/Client-Test.csproj
and insert <Compile Include="..\Server-Test\IMyService.cs" />
into the file as shown below.
...
<ItemGroup>
<ProjectReference Include="..\..\src\JSSoft.Communication\JSSoft.Communication.csproj" />
<!-- Add this line -->
<Compile Include="..\Server-Test\IMyService.cs" />
</ItemGroup>
...
And create Program.cs
file in the path .example/Server-Test.
Then copy and paste the code below into the file Program.cs
.
using JSSoft.Communication;
using JSSoft.Communication.Extensions;
using Services;
var service = new ClientService<IMyService>();
var serviceContext = new ClientContext([service]);
var token = await serviceContext.OpenAsync(cancellationToken: default);
var server = service.Server;
var id = await server.LoginAsync("admin", cancellationToken: default);
var (product, version) = await server.GetVersionAsync(cancellationToken: default);
Console.WriteLine($"logged in: {id}");
Console.WriteLine($"product: {product}");
Console.WriteLine($"version: {version}");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
var exitCode = await serviceContext.ReleaseAsync(token);
Environment.Exit(exitCode);
Build example solution
dotnet build .example
Run Server-Test example project in terminal
dotnet run --project .example/Server-Test
Run Client-Test example project in a new terminal
dotnet run --project .example/Client-Test