Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

GrpcNetframework

Using gRPC Services in .NET Framework
In GrpcLibrary, the transfer.cm file contains the source code generated by the protocol buffer compiler.

ChatServiceToServer.proto:

syntax = "proto3";
package GrpcLibrary;
service GrpcLibrary {
rpc RequestStringFunction (RequestString) returns (RequestString);
rpc SendMessageFunction (SendMessage) returns (SendMessage);
rpc RequestByteFunction (RequestByte) returns (RequestByte);
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message Empty {}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
message RequestString {
string sourceId = 1;
repeated string destinationIDs = 2;
string rqst = 3;
string option = 4;
}
message SendMessage {
string sourceID = 1;
repeated string destinationIDs = 2;
string msg = 3;
string option = 4;
}
message RequestByte {
string sourceID = 1;
repeated string destinationIDs = 2;
string msg = 3;
bytes rqst = 4;
string option = 5;
}

GrpcServer:

namespace GrpcServer
{
class GrpcImpl : GrpcLibrary.GrpcLibrary.GrpcLibraryBase
{
// SayHello
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
}
}
class Program
{
static int[] listofPortNumber = new int[] { 10011, 10012, 10013, 10007 };
static void Main(string[] args)
{
int Port = FreeTcpPort();
Server server = new Server
{
Services = { GrpcLibrary.GrpcLibrary.BindService(new GrpcImpl()) },
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine("GrpcService server listening on port " + Port);
Console.ReadKey();
server.ShutdownAsync().Wait();
}
public static int FreeTcpPort()
{
for (int i = 0; i <listofPortNumber.Length;i++){try{TcpListenerl = newTcpListener(IPAddress.Loopback,listofPortNumber[i]);l.Start();intport = ((IPEndPoint)l.LocalEndpoint).Port;l.Stop();returnport;}catch{continue;}}return0;}}}

GrpcClient:

namespace GrpcClient
{
class Program
{
static void Main(string[] args)
{
int[] listofPortNumber = new int[] { 10011, 10012, 10013, 10007 };
Channel channel = null;
bool isConnected = false;
for (int i = 0; i <listofPortNumber.Length;i++){channel = newChannel("127.0.0.1:" + listofPortNumber[i], ChannelCredentials.Insecure);
if (IsReady(channel))
{
isConnected = true;break;}}if(channel!= null&&isConnected){varclient = newGrpcLibrary.GrpcLibrary.GrpcLibraryClient(channel);while(true){try{Console.WriteLine("type something: ");
var input = Console.ReadLine();varreply = client.SayHello(newHelloRequest{Name = input});Console.WriteLine("reply: " + reply.Message);
//channel.ShutdownAsync().Wait();
//Console.ReadKey();
}
catch (Exception ex)
{
}
}
}
}
public static bool IsReady(Channel channel)
{
channel.ConnectAsync();
return channel.State == ChannelState.Ready;}}}

About

Using gRPC Services in .NET Framework

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages