diff options
author | Jens Geyer <jensg@apache.org> | 2021-12-04 22:48:37 +0100 |
---|---|---|
committer | Jens Geyer <Jens-G@users.noreply.github.com> | 2021-12-05 13:05:58 +0100 |
commit | 4c7b9fd38ad614dbf37c37e45569790b44868458 (patch) | |
tree | 6344c046f268684fbb5293c76581e0bc8b87df5b /tutorial | |
parent | 4637f6d5f8df752291f5e3c8923824e819a2b60f (diff) | |
download | thrift-4c7b9fd38ad614dbf37c37e45569790b44868458.tar.gz |
THRIFT-5479 Add net 6 support
Client: netstd
Patch: Jens Geyer
Diffstat (limited to 'tutorial')
-rw-r--r-- | tutorial/netstd/Client/Client.csproj | 4 | ||||
-rw-r--r-- | tutorial/netstd/Client/Program.cs | 87 | ||||
-rw-r--r-- | tutorial/netstd/Interfaces/Interfaces.csproj | 4 | ||||
-rw-r--r-- | tutorial/netstd/Server/Program.cs | 85 | ||||
-rw-r--r-- | tutorial/netstd/Server/Server.csproj | 4 |
5 files changed, 93 insertions, 91 deletions
diff --git a/tutorial/netstd/Client/Client.csproj b/tutorial/netstd/Client/Client.csproj index 1f877517e..cf01951be 100644 --- a/tutorial/netstd/Client/Client.csproj +++ b/tutorial/netstd/Client/Client.csproj @@ -19,7 +19,7 @@ --> <PropertyGroup> - <TargetFramework>net5.0</TargetFramework> + <TargetFramework>net6.0</TargetFramework> <LangVersion>9.0</LangVersion> <AssemblyName>Client</AssemblyName> <PackageId>Client</PackageId> @@ -32,7 +32,7 @@ </PropertyGroup> <ItemGroup> - <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" /> + <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> </ItemGroup> <ItemGroup> diff --git a/tutorial/netstd/Client/Program.cs b/tutorial/netstd/Client/Program.cs index 9c47450e7..f1c5236e1 100644 --- a/tutorial/netstd/Client/Program.cs +++ b/tutorial/netstd/Client/Program.cs @@ -39,10 +39,25 @@ using shared; namespace Client { + public static class LoggingHelper + { + public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { + ConfigureLogging(builder); + }); + + public static void ConfigureLogging(ILoggingBuilder logging) + { + logging.SetMinimumLevel(LogLevel.Trace); + logging.AddConsole(); + logging.AddDebug(); + } + + public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); + } + public class Program { - private static readonly ServiceCollection ServiceCollection = new(); - private static ILogger Logger; + private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed private static void DisplayHelp() @@ -86,47 +101,35 @@ Sample: { args ??= Array.Empty<string>(); - ServiceCollection.AddLogging(logging => ConfigureLogging(logging)); - using (var serviceProvider = ServiceCollection.BuildServiceProvider()) + if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) { - Logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger(nameof(Client)); - - if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) - { - DisplayHelp(); - return; - } + DisplayHelp(); + return; + } - Logger.LogInformation("Starting client..."); + Logger.LogInformation("Starting client..."); - using (var source = new CancellationTokenSource()) - { - RunAsync(args, source.Token).GetAwaiter().GetResult(); - } + using (var source = new CancellationTokenSource()) + { + RunAsync(args, source.Token).GetAwaiter().GetResult(); } } - private static void ConfigureLogging(ILoggingBuilder logging) - { - logging.SetMinimumLevel(LogLevel.Trace); - logging.AddConsole(); - logging.AddDebug(); - } - + private static async Task RunAsync(string[] args, CancellationToken cancellationToken) { var numClients = GetNumberOfClients(args); - Logger.LogInformation($"Selected # of clients: {numClients}"); + Logger.LogInformation("Selected # of clients: {numClients}", numClients); var transport = GetTransport(args); - Logger.LogInformation($"Selected client transport: {transport}"); + Logger.LogInformation("Selected client transport: {transport}", transport); var protocol = MakeProtocol( args, MakeTransport(args)); - Logger.LogInformation($"Selected client protocol: {GetProtocol(args)}"); + Logger.LogInformation("Selected client protocol: {GetProtocol(args)}", GetProtocol(args)); var mplex = GetMultiplex(args); - Logger.LogInformation("Multiplex " + (mplex ? "yes" : "no")); + Logger.LogInformation("Multiplex {mplex}", mplex); var tasks = new Task[numClients]; for (int i = 0; i < numClients; i++) @@ -240,7 +243,7 @@ Sample: { var numClients = args.FirstOrDefault(x => x.StartsWith("-mc"))?.Split(':')?[1]; - Logger.LogInformation($"Selected # of clients: {numClients}"); + Logger.LogInformation("Selected # of clients: {numClients}", numClients); if (int.TryParse(numClients, out int c) && (0 < c) && (c <= 100)) return c; @@ -310,7 +313,7 @@ Sample: } catch (Exception ex) { - Logger.LogError(ex.ToString()); + Logger.LogError("{ex}",ex); } finally { @@ -319,7 +322,7 @@ Sample: } catch (TApplicationException x) { - Logger.LogError(x.ToString()); + Logger.LogError("{x}",x); } } @@ -329,12 +332,12 @@ Sample: // Async version - Logger.LogInformation($"{client.ClientId} Ping()"); + Logger.LogInformation("{client.ClientId} Ping()", client.ClientId); await client.ping(cancellationToken); - Logger.LogInformation($"{client.ClientId} Add(1,1)"); + Logger.LogInformation("{client.ClientId} Add(1,1)", client.ClientId); var sum = await client.add(1, 1, cancellationToken); - Logger.LogInformation($"{client.ClientId} Add(1,1)={sum}"); + Logger.LogInformation("{client.ClientId} Add(1,1)={sum}", client.ClientId, sum); var work = new Work { @@ -345,13 +348,13 @@ Sample: try { - Logger.LogInformation($"{client.ClientId} Calculate(1)"); + Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); await client.calculate(1, work, cancellationToken); - Logger.LogInformation($"{client.ClientId} Whoa we can divide by 0"); + Logger.LogInformation("{client.ClientId} Whoa we can divide by 0", client.ClientId); } catch (InvalidOperation io) { - Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); + Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); } work.Op = Operation.SUBTRACT; @@ -360,20 +363,20 @@ Sample: try { - Logger.LogInformation($"{client.ClientId} Calculate(1)"); + Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); var diff = await client.calculate(1, work, cancellationToken); - Logger.LogInformation($"{client.ClientId} 15-10={diff}"); + Logger.LogInformation("{client.ClientId} 15-10={diff}", client.ClientId, diff); } catch (InvalidOperation io) { - Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); + Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); } - Logger.LogInformation($"{client.ClientId} GetStruct(1)"); + Logger.LogInformation("{client.ClientId} GetStruct(1)", client.ClientId); var log = await client.getStruct(1, cancellationToken); - Logger.LogInformation($"{client.ClientId} Check log: {log.Value}"); + Logger.LogInformation("{client.ClientId} Check log: {log.Value}", client.ClientId, log.Value); - Logger.LogInformation($"{client.ClientId} Zip() with delay 100mc on server side"); + Logger.LogInformation("{client.ClientId} Zip() with delay 100mc on server side", client.ClientId); await client.zip(cancellationToken); } diff --git a/tutorial/netstd/Interfaces/Interfaces.csproj b/tutorial/netstd/Interfaces/Interfaces.csproj index 989f6db40..a28874585 100644 --- a/tutorial/netstd/Interfaces/Interfaces.csproj +++ b/tutorial/netstd/Interfaces/Interfaces.csproj @@ -19,7 +19,7 @@ --> <PropertyGroup> - <TargetFramework>net5.0</TargetFramework> + <TargetFramework>net6.0</TargetFramework> <AssemblyName>Interfaces</AssemblyName> <PackageId>Interfaces</PackageId> <Version>0.16.0.0</Version> @@ -34,7 +34,7 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="System.ServiceModel.Primitives" Version="4.8.1" /> + <PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" /> </ItemGroup> <Target Name="PreBuild" BeforeTargets="_GenerateRestoreProjectSpec;Restore;Compile"> diff --git a/tutorial/netstd/Server/Program.cs b/tutorial/netstd/Server/Program.cs index 80205d54f..d9c9dbf12 100644 --- a/tutorial/netstd/Server/Program.cs +++ b/tutorial/netstd/Server/Program.cs @@ -38,53 +38,55 @@ using shared; using Thrift.Processor; using System.Diagnostics; -#pragma warning disable IDE0063 // using #pragma warning disable IDE0057 // substr namespace Server { + public static class LoggingHelper + { + public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { + ConfigureLogging(builder); + }); + + public static void ConfigureLogging(ILoggingBuilder logging) + { + logging.SetMinimumLevel(LogLevel.Trace); + logging.AddConsole(); + logging.AddDebug(); + } + + public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); + } + public class Program { - private static readonly ServiceCollection ServiceCollection = new(); - private static ILogger Logger; + private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed public static void Main(string[] args) { args ??= Array.Empty<string>(); - ServiceCollection.AddLogging(logging => ConfigureLogging(logging)); - using (var serviceProvider = ServiceCollection.BuildServiceProvider()) + if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) { - Logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger(nameof(Server)); - - if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) - { - DisplayHelp(); - return; - } - - using (var source = new CancellationTokenSource()) - { - RunAsync(args, source.Token).GetAwaiter().GetResult(); + DisplayHelp(); + return; + } - Logger.LogInformation("Press any key to stop..."); + using (var source = new CancellationTokenSource()) + { + RunAsync(args, source.Token).GetAwaiter().GetResult(); - Console.ReadLine(); - source.Cancel(); - } + Logger.LogInformation("Press any key to stop..."); - Logger.LogInformation("Server stopped"); + Console.ReadLine(); + source.Cancel(); } - } - private static void ConfigureLogging(ILoggingBuilder logging) - { - logging.SetMinimumLevel(LogLevel.Trace); - logging.AddConsole(); - logging.AddDebug(); + Logger.LogInformation("Server stopped"); } + private static void DisplayHelp() { Logger.LogInformation(@" @@ -226,15 +228,12 @@ Sample: try { Logger.LogInformation( - string.Format( - "TSimpleAsyncServer with \n{0} transport\n{1} buffering\nmultiplex = {2}\n{3} protocol", - transport, - buffering, - multiplex ? "yes" : "no", - protocol - )); - - var loggerFactory = ServiceCollection.BuildServiceProvider().GetService<ILoggerFactory>(); + "TSimpleAsyncServer with \n{transport} transport\n{buffering} buffering\nmultiplex = {multiplex}\n{protocol} protocol", + transport, + buffering, + multiplex ? "yes" : "no", + protocol + ); var server = new TSimpleAsyncServer( itProcessorFactory: new TSingletonProcessorFactory(processor), @@ -243,7 +242,7 @@ Sample: outputTransportFactory: transportFactory, inputProtocolFactory: protocolFactory, outputProtocolFactory: protocolFactory, - logger: loggerFactory.CreateLogger<TSimpleAsyncServer>()); + logger: LoggingHelper.CreateLogger<TSimpleAsyncServer >()); Logger.LogInformation("Starting the server..."); @@ -251,7 +250,7 @@ Sample: } catch (Exception x) { - Logger.LogInformation(x.ToString()); + Logger.LogInformation("{x}",x); } } @@ -327,7 +326,7 @@ Sample: .UseUrls("http://localhost:9090") .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() - .ConfigureLogging((ctx,logging) => ConfigureLogging(logging)) + .ConfigureLogging((ctx,logging) => LoggingHelper.ConfigureLogging(logging)) .Build(); Logger.LogTrace("test"); @@ -379,7 +378,7 @@ Sample: public async Task<SharedStruct> getStruct(int key, CancellationToken cancellationToken) { - Logger.LogInformation("GetStruct({0})", key); + Logger.LogInformation("GetStruct({key})", key); return await Task.FromResult(_log[key]); } @@ -391,13 +390,13 @@ Sample: public async Task<int> add(int num1, int num2, CancellationToken cancellationToken) { - Logger.LogInformation($"Add({num1},{num2})"); + Logger.LogInformation("Add({num1},{num2})", num1, num2); return await Task.FromResult(num1 + num2); } public async Task<int> calculate(int logid, Work w, CancellationToken cancellationToken) { - Logger.LogInformation($"Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])"); + Logger.LogInformation("Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])", logid, w.Op, w.Num1, w.Num2); int val; switch (w.Op) @@ -462,7 +461,7 @@ Sample: { public async Task<SharedStruct> getStruct(int key, CancellationToken cancellationToken) { - Logger.LogInformation("GetStruct({0})", key); + Logger.LogInformation("GetStruct({key})", key); return await Task.FromResult(new SharedStruct() { Key = key, diff --git a/tutorial/netstd/Server/Server.csproj b/tutorial/netstd/Server/Server.csproj index e5e54a5b1..6bee63fc3 100644 --- a/tutorial/netstd/Server/Server.csproj +++ b/tutorial/netstd/Server/Server.csproj @@ -19,7 +19,7 @@ --> <PropertyGroup> - <TargetFramework>net5.0</TargetFramework> + <TargetFramework>net6.0</TargetFramework> <LangVersion>9.0</LangVersion> <AssemblyName>Server</AssemblyName> <PackageId>Server</PackageId> @@ -40,6 +40,6 @@ <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.1" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" /> - <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" /> + <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" /> </ItemGroup> </Project> |