122 lines
3.2 KiB
C#
122 lines
3.2 KiB
C#
|
|
using ChatRoomContract;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.Extensions.Logging;
|
|
using SimpleRpc.Serialization.Hyperion;
|
|
using SimpleRpc.Transports;
|
|
using SimpleRpc.Transports.Http.Client;
|
|
using System.Diagnostics;
|
|
using System.Net;
|
|
using NLog;
|
|
|
|
namespace GrpcToSimpleRPCAdapter;
|
|
|
|
public class Server
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var self = new Server();
|
|
self.Run(args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logger for this class.
|
|
/// </summary>
|
|
Logger log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Configures logging subsystem.
|
|
/// </summary>
|
|
private void ConfigureLogging()
|
|
{
|
|
var config = new NLog.Config.LoggingConfiguration();
|
|
|
|
var console =
|
|
new NLog.Targets.ConsoleTarget("console")
|
|
{
|
|
Layout = @"${date:format=HH\:mm\:ss}|${level}| ${message} ${exception}"
|
|
};
|
|
config.AddTarget(console);
|
|
config.AddRuleForAllLevels(console);
|
|
|
|
LogManager.Configuration = config;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Program body.
|
|
/// </summary>
|
|
/// <param name="args">Command line arguments.</param>
|
|
private void Run(string[] args)
|
|
{
|
|
//configure logging
|
|
ConfigureLogging();
|
|
|
|
//indicate server is about to start
|
|
log.Info("Server is about to start");
|
|
|
|
//start the server
|
|
StartServer(args);
|
|
}
|
|
|
|
public IChatRoomService getSimpleRPCService()
|
|
{
|
|
//connect to the server, get service client proxy
|
|
var sc = new ServiceCollection();
|
|
sc
|
|
.AddSimpleRpcClient(
|
|
"chatRoomService", //must be same as on line 86
|
|
new HttpClientTransportOptions
|
|
{
|
|
Url = "http://127.0.0.1:5000/simplerpc",
|
|
Serializer = "HyperionMessageSerializer"
|
|
}
|
|
)
|
|
.AddSimpleRpcHyperionSerializer();
|
|
|
|
sc.AddSimpleRpcProxy<IChatRoomService>("chatRoomService"); //must be same as on line 77
|
|
|
|
var sp = sc.BuildServiceProvider();
|
|
|
|
var chatRoom = sp.GetService<IChatRoomService>();
|
|
Debug.Assert(chatRoom != null);
|
|
|
|
return chatRoom;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts integrated server.
|
|
/// </summary>
|
|
/// <param name="args">Command line arguments.</param>
|
|
private void StartServer(string[] args)
|
|
{
|
|
//create web app builder
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//configure integrated server
|
|
builder.WebHost.ConfigureKestrel(opts => {
|
|
opts.Listen(IPAddress.Loopback, 5002, opts =>
|
|
{
|
|
opts.Protocols = HttpProtocols.Http2;
|
|
});
|
|
});
|
|
|
|
//add support for GRPC services
|
|
builder.Services.AddGrpc();
|
|
|
|
//add the actual services
|
|
builder.Services.AddSingleton(getSimpleRPCService());
|
|
|
|
//build the server
|
|
var app = builder.Build();
|
|
|
|
//turn on request routing
|
|
app.UseRouting();
|
|
|
|
//configure routes
|
|
app.MapGrpcService<ChatRoomService>();
|
|
|
|
//run the server
|
|
app.Run();
|
|
// app.RunAsync(); //use this if you need to implement background processing in the main thread
|
|
}
|
|
} |