public static Grpc.Core.Server Start(int port, ICategoryService categoryService, IGroupService groupService, IStudyCardService studyCardService) { var hostName = Dns.GetHostName(); IPHostEntry iphostentry = Dns.GetHostByName(hostName); var serverPorts = iphostentry.AddressList .Where(x => x.AddressFamily == AddressFamily.InterNetwork) .Select(x => new ServerPort($"{x}", port, ServerCredentials.Insecure)) .ToList(); if (!serverPorts.Any(x => "127.0.0.1".Equals(x.Host, StringComparison.OrdinalIgnoreCase) || "localhost".Equals(x.Host, StringComparison.OrdinalIgnoreCase))) { serverPorts.Add(new ServerPort("localhost", port, ServerCredentials.Insecure)); } var server = new Grpc.Core.Server { Services = { CategoryService.BindService(new Services.CategoryService(categoryService)), GroupService.BindService(new Services.GroupService(groupService)), StudyCardService.BindService(new Services.StudyCardService(studyCardService)) } }; foreach (var serverPort in serverPorts) { server.Ports.Add(serverPort); } server.Start(); return(server); }
static async Task Main(string[] args) { var dataContextFactory = new DataContextFactory(); var categoryService = new CategoryService(new CategoryRepository(dataContextFactory)); var groupService = new GroupService(new GroupRepository(dataContextFactory)); var studyCardService = new StudyCardService(new StudyCardRepository(dataContextFactory)); var port = 10987; var server = StudyCards.Server.StudyCardServer.Start(port, categoryService, groupService, studyCardService); Console.WriteLine("Server started"); Console.WriteLine("Press any key to shutdown"); Console.ReadKey(); await server.ShutdownAsync().ConfigureAwait(false); }