diff options
Diffstat (limited to 'src/csharp/Grpc.Examples')
-rw-r--r-- | src/csharp/Grpc.Examples/Grpc.Examples.csproj | 3 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/Grpc.Examples.project.json | 8 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/Grpc.Examples.xproj | 18 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/Math.cs | 12 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/MathExamples.cs | 38 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/MathGrpc.cs | 160 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/MathServiceImpl.cs | 29 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/packages.config | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/project.json | 27 |
9 files changed, 140 insertions, 157 deletions
diff --git a/src/csharp/Grpc.Examples/Grpc.Examples.csproj b/src/csharp/Grpc.Examples/Grpc.Examples.csproj index f0a0aa3a26..3dfa84e896 100644 --- a/src/csharp/Grpc.Examples/Grpc.Examples.csproj +++ b/src/csharp/Grpc.Examples/Grpc.Examples.csproj @@ -39,7 +39,7 @@ <ItemGroup> <Reference Include="Google.Protobuf, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\Google.Protobuf.3.0.0-beta2\lib\portable-net45+netcore45+wpa81+wp8\Google.Protobuf.dll</HintPath> + <HintPath>..\packages\Google.Protobuf.3.0.0-beta3\lib\portable-net45+netcore45+wpa81+wp8\Google.Protobuf.dll</HintPath> </Reference> <Reference Include="nunit.framework"> <HintPath>..\packages\NUnit.3.2.0\lib\net45\nunit.framework.dll</HintPath> @@ -69,6 +69,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <None Include="Grpc.Examples.project.json" /> <None Include="packages.config" /> </ItemGroup> </Project>
\ No newline at end of file diff --git a/src/csharp/Grpc.Examples/Grpc.Examples.project.json b/src/csharp/Grpc.Examples/Grpc.Examples.project.json new file mode 100644 index 0000000000..c2f5bcb163 --- /dev/null +++ b/src/csharp/Grpc.Examples/Grpc.Examples.project.json @@ -0,0 +1,8 @@ +{ + "frameworks": { + "net45": { } + }, + "runtimes": { + "win": { } + } +} diff --git a/src/csharp/Grpc.Examples/Grpc.Examples.xproj b/src/csharp/Grpc.Examples/Grpc.Examples.xproj new file mode 100644 index 0000000000..d1d7e6d981 --- /dev/null +++ b/src/csharp/Grpc.Examples/Grpc.Examples.xproj @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="14.0.25123" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25123</VisualStudioVersion> + <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> + </PropertyGroup> + <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> + <PropertyGroup Label="Globals"> + <ProjectGuid>c77b792d-fc78-4ce2-9522-b40b0803c636</ProjectGuid> + <RootNamespace>Grpc.Examples</RootNamespace> + <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> + <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> + </PropertyGroup> + <PropertyGroup> + <SchemaVersion>2.0</SchemaVersion> + </PropertyGroup> + <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> +</Project>
\ No newline at end of file diff --git a/src/csharp/Grpc.Examples/Math.cs b/src/csharp/Grpc.Examples/Math.cs index 33c4f8d9c0..a17228c8c5 100644 --- a/src/csharp/Grpc.Examples/Math.cs +++ b/src/csharp/Grpc.Examples/Math.cs @@ -34,12 +34,12 @@ namespace Math { "Mw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { - new pbr::GeneratedCodeInfo(typeof(global::Math.DivArgs), global::Math.DivArgs.Parser, new[]{ "Dividend", "Divisor" }, null, null, null), - new pbr::GeneratedCodeInfo(typeof(global::Math.DivReply), global::Math.DivReply.Parser, new[]{ "Quotient", "Remainder" }, null, null, null), - new pbr::GeneratedCodeInfo(typeof(global::Math.FibArgs), global::Math.FibArgs.Parser, new[]{ "Limit" }, null, null, null), - new pbr::GeneratedCodeInfo(typeof(global::Math.Num), global::Math.Num.Parser, new[]{ "Num_" }, null, null, null), - new pbr::GeneratedCodeInfo(typeof(global::Math.FibReply), global::Math.FibReply.Parser, new[]{ "Count" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Math.DivArgs), global::Math.DivArgs.Parser, new[]{ "Dividend", "Divisor" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Math.DivReply), global::Math.DivReply.Parser, new[]{ "Quotient", "Remainder" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Math.FibArgs), global::Math.FibArgs.Parser, new[]{ "Limit" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Math.Num), global::Math.Num.Parser, new[]{ "Num_" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Math.FibReply), global::Math.FibReply.Parser, new[]{ "Count" }, null, null, null) })); } #endregion diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index 6075420974..d260830b94 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -32,6 +32,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Grpc.Core; using Grpc.Core.Utils; namespace Math @@ -109,5 +110,42 @@ namespace Math DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count }); Console.WriteLine("Avg Result: " + result); } + + /// <summary> + /// Shows how to handle a call ending with non-OK status. + /// </summary> + public static async Task HandleErrorExample(Math.MathClient client) + { + try + { + DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }); + } + catch (RpcException ex) + { + Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status)); + } + } + + /// <summary> + /// Shows how to send request headers and how to access response headers + /// and response trailers. + /// </summary> + public static async Task MetadataExample(Math.MathClient client) + { + var requestHeaders = new Metadata + { + { "custom-header", "custom-value" } + }; + + var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders); + + // Get response headers + Metadata responseHeaders = await call.ResponseHeadersAsync; + + var result = await call; + + // Get response trailers after the call has finished. + Metadata responseTrailers = call.GetTrailers(); + } } } diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs index 2d3034d28b..25abc51419 100644 --- a/src/csharp/Grpc.Examples/MathGrpc.cs +++ b/src/csharp/Grpc.Examples/MathGrpc.cs @@ -81,105 +81,14 @@ namespace Math { get { return global::Math.MathReflection.Descriptor.Services[0]; } } - /// <summary>Client for Math</summary> - [System.Obsolete("Client side interfaced will be removed in the next release. Use client class directly.")] - public interface IMathClient - { - /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. - /// </summary> - global::Math.DivReply Div(global::Math.DivArgs request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)); - /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. - /// </summary> - global::Math.DivReply Div(global::Math.DivArgs request, CallOptions options); - /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. - /// </summary> - AsyncUnaryCall<global::Math.DivReply> DivAsync(global::Math.DivArgs request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)); - /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. - /// </summary> - AsyncUnaryCall<global::Math.DivReply> DivAsync(global::Math.DivArgs request, CallOptions options); - /// <summary> - /// DivMany accepts an arbitrary number of division args from the client stream - /// and sends back the results in the reply stream. The stream continues until - /// the client closes its end; the server does the same after sending all the - /// replies. The stream ends immediately if either end aborts. - /// </summary> - AsyncDuplexStreamingCall<global::Math.DivArgs, global::Math.DivReply> DivMany(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)); - /// <summary> - /// DivMany accepts an arbitrary number of division args from the client stream - /// and sends back the results in the reply stream. The stream continues until - /// the client closes its end; the server does the same after sending all the - /// replies. The stream ends immediately if either end aborts. - /// </summary> - AsyncDuplexStreamingCall<global::Math.DivArgs, global::Math.DivReply> DivMany(CallOptions options); - /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib - /// generates up to limit numbers; otherwise it continues until the call is - /// canceled. Unlike Fib above, Fib has no final FibReply. - /// </summary> - AsyncServerStreamingCall<global::Math.Num> Fib(global::Math.FibArgs request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)); - /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib - /// generates up to limit numbers; otherwise it continues until the call is - /// canceled. Unlike Fib above, Fib has no final FibReply. - /// </summary> - AsyncServerStreamingCall<global::Math.Num> Fib(global::Math.FibArgs request, CallOptions options); - /// <summary> - /// Sum sums a stream of numbers, returning the final result once the stream - /// is closed. - /// </summary> - AsyncClientStreamingCall<global::Math.Num, global::Math.Num> Sum(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)); - /// <summary> - /// Sum sums a stream of numbers, returning the final result once the stream - /// is closed. - /// </summary> - AsyncClientStreamingCall<global::Math.Num, global::Math.Num> Sum(CallOptions options); - } - - /// <summary>Interface of server-side implementations of Math</summary> - [System.Obsolete("Service implementations should inherit from the generated abstract base class instead.")] - public interface IMath - { - /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. - /// </summary> - Task<global::Math.DivReply> Div(global::Math.DivArgs request, ServerCallContext context); - /// <summary> - /// DivMany accepts an arbitrary number of division args from the client stream - /// and sends back the results in the reply stream. The stream continues until - /// the client closes its end; the server does the same after sending all the - /// replies. The stream ends immediately if either end aborts. - /// </summary> - Task DivMany(IAsyncStreamReader<global::Math.DivArgs> requestStream, IServerStreamWriter<global::Math.DivReply> responseStream, ServerCallContext context); - /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib - /// generates up to limit numbers; otherwise it continues until the call is - /// canceled. Unlike Fib above, Fib has no final FibReply. - /// </summary> - Task Fib(global::Math.FibArgs request, IServerStreamWriter<global::Math.Num> responseStream, ServerCallContext context); - /// <summary> - /// Sum sums a stream of numbers, returning the final result once the stream - /// is closed. - /// </summary> - Task<global::Math.Num> Sum(IAsyncStreamReader<global::Math.Num> requestStream, ServerCallContext context); - } - /// <summary>Base class for server-side implementations of Math</summary> public abstract class MathBase { /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. + /// Div divides DivArgs.dividend by DivArgs.divisor and returns the quotient + /// and remainder. /// </summary> - public virtual Task<global::Math.DivReply> Div(global::Math.DivArgs request, ServerCallContext context) + public virtual global::System.Threading.Tasks.Task<global::Math.DivReply> Div(global::Math.DivArgs request, ServerCallContext context) { throw new RpcException(new Status(StatusCode.Unimplemented, "")); } @@ -190,17 +99,17 @@ namespace Math { /// the client closes its end; the server does the same after sending all the /// replies. The stream ends immediately if either end aborts. /// </summary> - public virtual Task DivMany(IAsyncStreamReader<global::Math.DivArgs> requestStream, IServerStreamWriter<global::Math.DivReply> responseStream, ServerCallContext context) + public virtual global::System.Threading.Tasks.Task DivMany(IAsyncStreamReader<global::Math.DivArgs> requestStream, IServerStreamWriter<global::Math.DivReply> responseStream, ServerCallContext context) { throw new RpcException(new Status(StatusCode.Unimplemented, "")); } /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib + /// Fib generates numbers in the Fibonacci sequence. If FibArgs.limit > 0, Fib /// generates up to limit numbers; otherwise it continues until the call is /// canceled. Unlike Fib above, Fib has no final FibReply. /// </summary> - public virtual Task Fib(global::Math.FibArgs request, IServerStreamWriter<global::Math.Num> responseStream, ServerCallContext context) + public virtual global::System.Threading.Tasks.Task Fib(global::Math.FibArgs request, IServerStreamWriter<global::Math.Num> responseStream, ServerCallContext context) { throw new RpcException(new Status(StatusCode.Unimplemented, "")); } @@ -209,7 +118,7 @@ namespace Math { /// Sum sums a stream of numbers, returning the final result once the stream /// is closed. /// </summary> - public virtual Task<global::Math.Num> Sum(IAsyncStreamReader<global::Math.Num> requestStream, ServerCallContext context) + public virtual global::System.Threading.Tasks.Task<global::Math.Num> Sum(IAsyncStreamReader<global::Math.Num> requestStream, ServerCallContext context) { throw new RpcException(new Status(StatusCode.Unimplemented, "")); } @@ -217,52 +126,55 @@ namespace Math { } /// <summary>Client for Math</summary> - #pragma warning disable 0618 - public class MathClient : ClientBase<MathClient>, IMathClient - #pragma warning restore 0618 + public class MathClient : ClientBase<MathClient> { + /// <summary>Creates a new client for Math</summary> + /// <param name="channel">The channel to use to make remote calls.</param> public MathClient(Channel channel) : base(channel) { } + /// <summary>Creates a new client for Math that uses a custom <c>CallInvoker</c>.</summary> + /// <param name="callInvoker">The callInvoker to use to make remote calls.</param> public MathClient(CallInvoker callInvoker) : base(callInvoker) { } - ///<summary>Protected parameterless constructor to allow creation of test doubles.</summary> + /// <summary>Protected parameterless constructor to allow creation of test doubles.</summary> protected MathClient() : base() { } - ///<summary>Protected constructor to allow creation of configured clients.</summary> + /// <summary>Protected constructor to allow creation of configured clients.</summary> + /// <param name="configuration">The client configuration.</param> protected MathClient(ClientBaseConfiguration configuration) : base(configuration) { } /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. + /// Div divides DivArgs.dividend by DivArgs.divisor and returns the quotient + /// and remainder. /// </summary> public virtual global::Math.DivReply Div(global::Math.DivArgs request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)) { return Div(request, new CallOptions(headers, deadline, cancellationToken)); } /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. + /// Div divides DivArgs.dividend by DivArgs.divisor and returns the quotient + /// and remainder. /// </summary> public virtual global::Math.DivReply Div(global::Math.DivArgs request, CallOptions options) { return CallInvoker.BlockingUnaryCall(__Method_Div, null, options, request); } /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. + /// Div divides DivArgs.dividend by DivArgs.divisor and returns the quotient + /// and remainder. /// </summary> public virtual AsyncUnaryCall<global::Math.DivReply> DivAsync(global::Math.DivArgs request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken)) { return DivAsync(request, new CallOptions(headers, deadline, cancellationToken)); } /// <summary> - /// Div divides args.dividend by args.divisor and returns the quotient and - /// remainder. + /// Div divides DivArgs.dividend by DivArgs.divisor and returns the quotient + /// and remainder. /// </summary> public virtual AsyncUnaryCall<global::Math.DivReply> DivAsync(global::Math.DivArgs request, CallOptions options) { @@ -289,7 +201,7 @@ namespace Math { return CallInvoker.AsyncDuplexStreamingCall(__Method_DivMany, null, options); } /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib + /// Fib generates numbers in the Fibonacci sequence. If FibArgs.limit > 0, Fib /// generates up to limit numbers; otherwise it continues until the call is /// canceled. Unlike Fib above, Fib has no final FibReply. /// </summary> @@ -298,7 +210,7 @@ namespace Math { return Fib(request, new CallOptions(headers, deadline, cancellationToken)); } /// <summary> - /// Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib + /// Fib generates numbers in the Fibonacci sequence. If FibArgs.limit > 0, Fib /// generates up to limit numbers; otherwise it continues until the call is /// canceled. Unlike Fib above, Fib has no final FibReply. /// </summary> @@ -328,30 +240,10 @@ namespace Math { } } - /// <summary>Creates a new client for Math</summary> - public static MathClient NewClient(Channel channel) - { - return new MathClient(channel); - } - - /// <summary>Creates service definition that can be registered with a server</summary> - #pragma warning disable 0618 - public static ServerServiceDefinition BindService(IMath serviceImpl) - #pragma warning restore 0618 - { - return ServerServiceDefinition.CreateBuilder(__ServiceName) - .AddMethod(__Method_Div, serviceImpl.Div) - .AddMethod(__Method_DivMany, serviceImpl.DivMany) - .AddMethod(__Method_Fib, serviceImpl.Fib) - .AddMethod(__Method_Sum, serviceImpl.Sum).Build(); - } - /// <summary>Creates service definition that can be registered with a server</summary> - #pragma warning disable 0618 public static ServerServiceDefinition BindService(MathBase serviceImpl) - #pragma warning restore 0618 { - return ServerServiceDefinition.CreateBuilder(__ServiceName) + return ServerServiceDefinition.CreateBuilder() .AddMethod(__Method_Div, serviceImpl.Div) .AddMethod(__Method_DivMany, serviceImpl.DivMany) .AddMethod(__Method_Fib, serviceImpl.Fib) diff --git a/src/csharp/Grpc.Examples/MathServiceImpl.cs b/src/csharp/Grpc.Examples/MathServiceImpl.cs index 79c56e57a8..a28020f62f 100644 --- a/src/csharp/Grpc.Examples/MathServiceImpl.cs +++ b/src/csharp/Grpc.Examples/MathServiceImpl.cs @@ -52,23 +52,15 @@ namespace Math public override async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context) { - if (request.Limit <= 0) - { - // keep streaming the sequence until cancelled. - IEnumerator<Num> fibEnumerator = FibInternal(long.MaxValue).GetEnumerator(); - while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext()) - { - await responseStream.WriteAsync(fibEnumerator.Current); - await Task.Delay(100); - } - } + var limit = request.Limit > 0 ? request.Limit : long.MaxValue; + var fibEnumerator = FibInternal(limit).GetEnumerator(); - if (request.Limit > 0) + // Keep streaming the sequence until the call is cancelled. + // Use CancellationToken from ServerCallContext to detect the cancellation. + while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext()) { - foreach (var num in FibInternal(request.Limit)) - { - await responseStream.WriteAsync(num); - } + await responseStream.WriteAsync(fibEnumerator.Current); + await Task.Delay(100); } } @@ -89,6 +81,13 @@ namespace Math static DivReply DivInternal(DivArgs args) { + if (args.Divisor == 0) + { + // One can finish the RPC with non-ok status by throwing RpcException instance. + // Alternatively, resulting status can be set using ServerCallContext.Status + throw new RpcException(new Status(StatusCode.InvalidArgument, "Division by zero")); + } + long quotient = args.Dividend / args.Divisor; long remainder = args.Dividend % args.Divisor; return new DivReply { Quotient = quotient, Remainder = remainder }; diff --git a/src/csharp/Grpc.Examples/packages.config b/src/csharp/Grpc.Examples/packages.config index a424cd2ea0..a70dcbd4c6 100644 --- a/src/csharp/Grpc.Examples/packages.config +++ b/src/csharp/Grpc.Examples/packages.config @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <packages> - <package id="Google.Protobuf" version="3.0.0-beta2" targetFramework="net45" /> + <package id="Google.Protobuf" version="3.0.0-beta3" targetFramework="net45" /> <package id="Ix-Async" version="1.2.5" targetFramework="net45" /> <package id="NUnit" version="3.2.0" targetFramework="net45" /> </packages>
\ No newline at end of file diff --git a/src/csharp/Grpc.Examples/project.json b/src/csharp/Grpc.Examples/project.json new file mode 100644 index 0000000000..7d3f4dcbb1 --- /dev/null +++ b/src/csharp/Grpc.Examples/project.json @@ -0,0 +1,27 @@ +{ + "buildOptions": { + }, + + "dependencies": { + "Grpc.Core": { + "target": "project" + }, + "Google.Protobuf": "3.0.0-beta3" + }, + "frameworks": { + "net45": { + "frameworkAssemblies": { + "System.Runtime": "", + "System.IO": "" + } + }, + "netstandard1.5": { + "imports": [ + "portable-net45" + ], + "dependencies": { + "NETStandard.Library": "1.5.0-rc2-24027" + } + } + } +} |