https://github.com/taherfattahi/grpcnetframework
Using gRPC Services in .NET Framework
https://github.com/taherfattahi/grpcnetframework
grpc netframework
Last synced: 2 months ago
JSON representation
Using gRPC Services in .NET Framework
- Host: GitHub
- URL: https://github.com/taherfattahi/grpcnetframework
- Owner: taherfattahi
- Created: 2020-12-10T08:50:21.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-07-02T10:02:59.000Z (3 months ago)
- Last Synced: 2025-07-02T11:22:32.731Z (3 months ago)
- Topics: grpc, netframework
- Language: C#
- Homepage:
- Size: 1.92 MB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```html
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:
```html
namespace GrpcServer
{
class GrpcImpl : GrpcLibrary.GrpcLibrary.GrpcLibraryBase
{
// SayHello
public override Task 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
{
TcpListener l = new TcpListener(IPAddress.Loopback, listofPortNumber[i]);
l.Start();
int port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
catch
{
continue;
}
}
return 0;
}}
}
```## GrpcClient:
```html
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 = new Channel("127.0.0.1:" + listofPortNumber[i], ChannelCredentials.Insecure);
if (IsReady(channel))
{
isConnected = true;
break;
}
}if (channel != null && isConnected)
{
var client = new GrpcLibrary.GrpcLibrary.GrpcLibraryClient(channel);
while (true)
{
try
{
Console.WriteLine("type something: ");
var input = Console.ReadLine();
var reply = client.SayHello(new HelloRequest { 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;
}}
}```