|
| 1 | +using System.IO.Pipelines; |
| 2 | +using Microsoft.VisualStudio.Threading; |
| 3 | +using Nerdbank.Streams; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +public partial class Extensibility |
| 7 | +{ |
| 8 | + #region RpcContract |
| 9 | + [JsonRpcContract, GenerateShape(IncludeMethods = MethodShapeFlags.PublicInstance)] |
| 10 | + public partial interface IWeatherService |
| 11 | + { |
| 12 | + Task<int> GetTemperatureAsync(string city, CancellationToken cancellationToken); |
| 13 | + } |
| 14 | + #endregion |
| 15 | + |
| 16 | + #region ServiceImpl |
| 17 | + class WeatherService : IWeatherService |
| 18 | + { |
| 19 | + public Task<int> GetTemperatureAsync(string city, CancellationToken cancellationToken) |
| 20 | + { |
| 21 | + return Task.FromResult(city switch |
| 22 | + { |
| 23 | + "Seattle" => 55, |
| 24 | + "Denver" => 65, |
| 25 | + _ => throw new ArgumentException($"Unknown city: {city}"), |
| 26 | + }); |
| 27 | + } |
| 28 | + } |
| 29 | + #endregion |
| 30 | + |
| 31 | + #region PolyTypeWitness |
| 32 | + [GenerateShapeFor<int>] |
| 33 | + partial class Witness; |
| 34 | + #endregion |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task ValidateNBMsgPackSample() |
| 38 | + { |
| 39 | + (IDuplexPipe clientPipe, IDuplexPipe serverPipe) = FullDuplexStream.CreatePipePair(); |
| 40 | + await Task.WhenAll( |
| 41 | + this.NBMsgPackClient(clientPipe, TestContext.Current.CancellationToken), |
| 42 | + this.NBMsgPackServer(serverPipe, TestContext.Current.CancellationToken)) |
| 43 | + .WithCancellation(TestContext.Current.CancellationToken); |
| 44 | + } |
| 45 | + |
| 46 | + IJsonRpcMessageFormatter CreateNBMsgPackFormatter() => |
| 47 | + #region CreateNBMsgPackFormatter |
| 48 | + new NerdbankMessagePackFormatter |
| 49 | + { |
| 50 | + TypeShapeProvider = Witness.GeneratedTypeShapeProvider, |
| 51 | + UserDataSerializer = NerdbankMessagePackFormatter.DefaultSerializer with |
| 52 | + { |
| 53 | + PerfOverSchemaStability = true, |
| 54 | + }, |
| 55 | + }; |
| 56 | + #endregion |
| 57 | + |
| 58 | + async Task NBMsgPackClient(IDuplexPipe pipe, CancellationToken cancellationToken) |
| 59 | + { |
| 60 | + #region NBMsgPackClient |
| 61 | + IWeatherService proxy = JsonRpc.Attach<IWeatherService>( |
| 62 | + new LengthHeaderMessageHandler( |
| 63 | + pipe, |
| 64 | + this.CreateNBMsgPackFormatter())); |
| 65 | + using (proxy as IDisposable) |
| 66 | + { |
| 67 | + int temperature = await proxy.GetTemperatureAsync("Denver", cancellationToken); |
| 68 | + TestContext.Current.TestOutputHelper?.WriteLine($"Temp: {temperature}"); |
| 69 | + } |
| 70 | + #endregion |
| 71 | + } |
| 72 | + |
| 73 | + async Task NBMsgPackServer(IDuplexPipe pipe, CancellationToken cancellationToken) |
| 74 | + { |
| 75 | + #region NBMsgPackServer |
| 76 | + JsonRpc rpc = new( |
| 77 | + new LengthHeaderMessageHandler( |
| 78 | + pipe, |
| 79 | + this.CreateNBMsgPackFormatter())); |
| 80 | + rpc.AddLocalRpcTarget(RpcTargetMetadata.FromShape<IWeatherService>(), new WeatherService(), options: null); |
| 81 | + rpc.StartListening(); |
| 82 | + await rpc.Completion; |
| 83 | + #endregion |
| 84 | + } |
| 85 | +} |
0 commit comments