From e01cbdb5acc4bde99890bc049430a051b7093f8c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 6 Jun 2024 15:04:51 +0200 Subject: [PATCH] Implement a ranking server --- Tasks/Lab7/RankingServer/Competitor.cs | 14 ++++++ Tasks/Lab7/RankingServer/IRankingService.cs | 11 +++++ Tasks/Lab7/RankingServer/Program.cs | 49 +++++++++++++++++++ .../RankingServer/Properties/appsettings.json | 10 ++++ .../Properties/launchSettings.json | 12 +++++ Tasks/Lab7/RankingServer/RankingServer.csproj | 15 ++++++ Tasks/Lab7/RankingServer/RankingServer.sln | 25 ++++++++++ Tasks/Lab7/RankingServer/RankingService.cs | 48 ++++++++++++++++++ 8 files changed, 184 insertions(+) create mode 100644 Tasks/Lab7/RankingServer/Competitor.cs create mode 100644 Tasks/Lab7/RankingServer/IRankingService.cs create mode 100644 Tasks/Lab7/RankingServer/Program.cs create mode 100644 Tasks/Lab7/RankingServer/Properties/appsettings.json create mode 100644 Tasks/Lab7/RankingServer/Properties/launchSettings.json create mode 100644 Tasks/Lab7/RankingServer/RankingServer.csproj create mode 100644 Tasks/Lab7/RankingServer/RankingServer.sln create mode 100644 Tasks/Lab7/RankingServer/RankingService.cs diff --git a/Tasks/Lab7/RankingServer/Competitor.cs b/Tasks/Lab7/RankingServer/Competitor.cs new file mode 100644 index 0000000..b51c239 --- /dev/null +++ b/Tasks/Lab7/RankingServer/Competitor.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace RankingServer +{ + [DataContract] + public class Competitor + { + [DataMember] + public string? Name { get; set; } + + [DataMember] + public string? Time { get; set; } + } +} diff --git a/Tasks/Lab7/RankingServer/IRankingService.cs b/Tasks/Lab7/RankingServer/IRankingService.cs new file mode 100644 index 0000000..d9d09af --- /dev/null +++ b/Tasks/Lab7/RankingServer/IRankingService.cs @@ -0,0 +1,11 @@ +using CoreWCF; + +namespace RankingServer +{ + [ServiceContract] + public interface IRankingService + { + [OperationContract] + List RankingList(); + } +} diff --git a/Tasks/Lab7/RankingServer/Program.cs b/Tasks/Lab7/RankingServer/Program.cs new file mode 100644 index 0000000..7f1befb --- /dev/null +++ b/Tasks/Lab7/RankingServer/Program.cs @@ -0,0 +1,49 @@ +// See https://aka.ms/new-console-template for more information +using RankingServer; +using CoreWCF.Configuration; +using CoreWCF.Description; +using CoreWCF; +using System.Net; + +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); + +builder.WebHost + .ConfigureKestrel( + (context, options) => + { + options.AllowSynchronousIO = true; + }) + .UseKestrel( + (context, options) => + { + options.Listen(IPAddress.Loopback, 5000); + options.Listen( + IPAddress.Loopback, + 5001, + listenOptions => + { + listenOptions.UseHttps(); + }); + }); + +// Add support +builder.Services.AddServiceModelServices().AddServiceModelMetadata(); +builder.Services.AddSingleton(); +builder.Services.AddAuthorization(); +builder.Services.AddAuthentication(); + +WebApplication app = builder.Build(); + +app.UseServiceModel( + builder => + { + builder + .AddService((serviceOptions) => { }) + .AddServiceEndpoint(new BasicHttpBinding(), "/RankingService/basichttp") + .AddServiceEndpoint(new WSHttpBinding(SecurityMode.Transport), "/RankingService/WSHttps"); + }); + +ServiceMetadataBehavior serviceMetadataBehavior = app.Services.GetRequiredService(); +serviceMetadataBehavior.HttpGetEnabled = true; +app.Logger.LogInformation("Starting Ranking Service…"); +app.Run(); diff --git a/Tasks/Lab7/RankingServer/Properties/appsettings.json b/Tasks/Lab7/RankingServer/Properties/appsettings.json new file mode 100644 index 0000000..1c49669 --- /dev/null +++ b/Tasks/Lab7/RankingServer/Properties/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "CoreWCF.Channels": "Warning", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Tasks/Lab7/RankingServer/Properties/launchSettings.json b/Tasks/Lab7/RankingServer/Properties/launchSettings.json new file mode 100644 index 0000000..cad6af6 --- /dev/null +++ b/Tasks/Lab7/RankingServer/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "RankingServer": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:59129;http://localhost:59130" + } + } +} \ No newline at end of file diff --git a/Tasks/Lab7/RankingServer/RankingServer.csproj b/Tasks/Lab7/RankingServer/RankingServer.csproj new file mode 100644 index 0000000..c7e1943 --- /dev/null +++ b/Tasks/Lab7/RankingServer/RankingServer.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + diff --git a/Tasks/Lab7/RankingServer/RankingServer.sln b/Tasks/Lab7/RankingServer/RankingServer.sln new file mode 100644 index 0000000..529c30d --- /dev/null +++ b/Tasks/Lab7/RankingServer/RankingServer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34928.147 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RankingServer", "RankingServer.csproj", "{81FE59F3-371E-4E80-A2C8-C93E03774EE9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {81FE59F3-371E-4E80-A2C8-C93E03774EE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81FE59F3-371E-4E80-A2C8-C93E03774EE9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81FE59F3-371E-4E80-A2C8-C93E03774EE9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81FE59F3-371E-4E80-A2C8-C93E03774EE9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3B55BFCE-E49B-4594-B364-4CF5D7D70E92} + EndGlobalSection +EndGlobal diff --git a/Tasks/Lab7/RankingServer/RankingService.cs b/Tasks/Lab7/RankingServer/RankingService.cs new file mode 100644 index 0000000..7c9628a --- /dev/null +++ b/Tasks/Lab7/RankingServer/RankingService.cs @@ -0,0 +1,48 @@ + +namespace RankingServer +{ + public class RankingService : IRankingService + { + private string[] ranks = { + "Mueller Stefan,02:31:14", + "Marti Adrian,2:30:09", + "Kiptum Daniel,2:11:31", + "Ancay Tarcis,2:20:02", + "Kreibuhl Christian,2:21:47", + "Ott Michael,2:33:48", + "Menzi Christoph,2:27:26", + "Oliver Ruben,2:32:12", + "Elmer Beat,2:33:53", + "Kuehni Martin,2:33:36" + }; + + private Lazy> competitors; + + public RankingService() + { + competitors = new Lazy>( + () => + { + List ranklingList = new List(); + foreach (string rank in ranks) + { + string[] fields = rank.Split(','); + + Competitor competitor = new() + { + Name = fields[0], + Time = fields[1] + }; + + ranklingList.Add(competitor); + }; + return ranklingList; + }); + } + + public List RankingList() + { + return competitors.Value; + } + } +}