Skip to content

Commit

Permalink
fix #25; new overload of AddCodeFirstGrpc that allows configuration…
Browse files Browse the repository at this point in the history
… of `configureOptions`, and exposes the `IGrpcServerBuilder`
  • Loading branch information
mgravell committed Oct 14, 2019
1 parent 8acc2cd commit 18ef9be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/pb-net-grpc/Server_CS/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public class Startup
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddCodeFirstGrpc();
services.AddCodeFirstGrpc(config =>
{
config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
14 changes: 11 additions & 3 deletions src/protobuf-net.Grpc.AspNetCore/ServicesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Grpc.AspNetCore.Server.Model;
using Grpc.AspNetCore.Server;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using ProtoBuf.Grpc.Configuration;
using System;
using System.Collections.Generic;

namespace ProtoBuf.Grpc.Server
Expand All @@ -16,10 +18,16 @@ public static class ServicesExtensions
/// <summary>
/// Registers a provider that can recognize and handle code-first services
/// </summary>
public static void AddCodeFirstGrpc(this IServiceCollection services)
public static void AddCodeFirstGrpc(this IServiceCollection services) => AddCodeFirstGrpc(services, null);

/// <summary>
/// Registers a provider that can recognize and handle code-first services
/// </summary>
public static IGrpcServerBuilder AddCodeFirstGrpc(this IServiceCollection services, Action<GrpcServiceOptions>? configureOptions)
{
services.AddGrpc();
var builder = configureOptions == null ? services.AddGrpc() : services.AddGrpc(configureOptions);
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(CodeFirstServiceMethodProvider<>)));
return builder;
}

private sealed class CodeFirstServiceMethodProvider<TService> : IServiceMethodProvider<TService> where TService : class
Expand Down

0 comments on commit 18ef9be

Please sign in to comment.