Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side: Exception handling middleware / Interceptors #22

Closed
GoNextGroup opened this issue Oct 8, 2019 · 7 comments
Closed

Server side: Exception handling middleware / Interceptors #22

GoNextGroup opened this issue Oct 8, 2019 · 7 comments

Comments

@GoNextGroup
Copy link

Hello.

Is it possible to add here any mechanism to realize interceptors and exceptions handling on the client and server side?

Thank you.

@mgravell
Copy link
Member

mgravell commented Oct 8, 2019

Are you talking about something that already exists in the main gRPC library, that I'm not exposing - or are you talking about something bespoke and new?

For the first: indeed I haven't done anything extra there; show me the API and I'll consider it

For the second: what would you propose?

@mythz
Copy link
Contributor

mythz commented Oct 8, 2019

You'll likely want to expose it in your AddCodeFirstGrpc ext method:

public static void AddCodeFirstGrpc(this IServiceCollection services, Action<GrpcServiceOptions> configureOptions=null)
{
    services.AddGrpc(configureOptions);
    services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(CodeFirstServiceMethodProvider<>)));
}

So it can be added like:

services.AddCodeFirstGrpc(options => options.Interceptors.Add<MyInterceptor>());

@GoNextGroup
Copy link
Author

GoNextGroup commented Oct 9, 2019

Yeah, It will be very good to wide AddCodeFirstGrpc with "configureOptions", but it's not critical.
I can use the code like this for it:

    public static class GZipCodeFirstGrpc
    {
        public static void AddGZipCodeFirstGrpc(this IServiceCollection services)
        {
            var gzipCompressionProvider = new GzipCompressionProvider(CompressionLevel.Optimal);
            Action<GrpcServiceOptions> configureOptions = e =>
            {
                e.CompressionProviders.Add(gzipCompressionProvider);
                e.Interceptors.Add(typeof(TestInterceptor));
            };
            services.Configure(configureOptions).AddCodeFirstGrpc();
        }
    }

But I have no idea how to add interceptors via protobuf-net.Grpc on the client side.

And in general, interceptors cannot be configured to catch exceptions which are generated by a code.
ASP.NET Core has great mechanism of controllers' exceptions catching by the custom exception filters. But it doesn't work with the grpc calls (I've tried to realize).

The main idea is connected with the DRY principle - I don't want to repeat try-catch blocks in all grpc calls, but I want to realize it once. It could be realized on the server side by wrapping in try-catch UnaryServerHandler method in the Interceptor and adding it through AddCodeFirstGrpc on the server side, but no current way to add on the client side. And, of course, If I use ASP.NET Core + gRPC, I want to use well-working exception handling mechanism which is realized in the ASP.NET Core.

Maybe, do you know the way to add ASP,NET Core exception handling (have a code examples - it's an offtop)? And also it could be great to add mechanism of interceptors adding on the client side.

Thank you.

@GoNextGroup
Copy link
Author

GoNextGroup commented Oct 10, 2019

Potentially, It could be added here (interceptors for the grpc client) in the

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TService CreateGrpcService<TService>(this ChannelBase client, ClientFactory? clientFactory = null)
            where TService : class
                    => (clientFactory ?? ClientFactory.Default).CreateClient<TService>(client.CreateCallInvoker());

You could rewrite it like:

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TService CreateGrpcService<TService>(this ChannelBase client, ClientFactory? clientFactory = null, params Interceptor[] interceptors)
            where TService : class
                    => (clientFactory ?? ClientFactory.Default).CreateClient<TService>(interceptors.Length > 0 ? client.CreateCallInvoker().Intercept(interceptors) : client.CreateCallInvoker());

or


        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TService CreateGrpcService<TService>(this ChannelBase client, ClientFactory? clientFactory = null, params Interceptor[] interceptors)
            where TService : class
                    => (clientFactory ?? ClientFactory.Default).CreateClient<TService>(interceptors.Length > 0 ? client.Intercept(interceptors) : client.CreateCallInvoker());

I'll try to run this code to check what's better to use.

@mgravell
Copy link
Member

1.0.13 added an overload that allows this configuration at the server, both on the input and the return

I think that covers everything here, so I'm marking it as completed by 18ef9be - if I'm wrong, ping me and we can reopen.

@GoNextGroup
Copy link
Author

Marc, I think, you also should add interceptors on the client side. Earlier I wrote the code examples which could be used:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TService CreateGrpcService<TService>(this ChannelBase client, ClientFactory? clientFactory = null, params Interceptor[] interceptors)
            where TService : class
                    => (clientFactory ?? ClientFactory.Default).CreateClient<TService>(interceptors.Length > 0 ? client.CreateCallInvoker().Intercept(interceptors) : client.CreateCallInvoker());

or

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TService CreateGrpcService<TService>(this ChannelBase client, ClientFactory? clientFactory = null, params Interceptor[] interceptors)
            where TService : class
                    => (clientFactory ?? ClientFactory.Default).CreateClient<TService>(interceptors.Length > 0 ? client.Intercept(interceptors) : client.CreateCallInvoker());

On the server side Action<GrpcServiceOptions> is really enough.

@mgravell mgravell changed the title Exception handling middleware / Interceptors Server side: Exception handling middleware / Interceptors Oct 16, 2019
@mgravell
Copy link
Member

@GoNextGroup k; let's consider this (#22) "server-side", and we'll take the client-side conversation to #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants