Example #1
0
    public void OnServiceMethodDiscovery(ServiceMethodProviderContext <TService> context)
    {
        var bindMethodInfo = BindMethodFinder.GetBindMethod(typeof(TService));

        // Invoke BindService(ServiceBinderBase, BaseType)
        if (bindMethodInfo != null)
        {
            // The second parameter is always the service base type
            var serviceParameter = bindMethodInfo.GetParameters()[1];

            ServiceDescriptor?serviceDescriptor = null;
            try
            {
                serviceDescriptor = ServiceDescriptorHelpers.GetServiceDescriptor(bindMethodInfo.DeclaringType !);
            }
            catch (Exception ex)
            {
                Log.ServiceDescriptorError(_logger, typeof(TService), ex);
            }

            if (serviceDescriptor != null)
            {
                var binder = new JsonTranscodingProviderServiceBinder <TService>(
                    context,
                    new ReflectionServiceInvokerResolver <TService>(serviceParameter.ParameterType),
                    serviceDescriptor,
                    _globalOptions,
                    _serviceOptions,
                    _loggerFactory,
                    _serviceActivator,
                    _JsonTranscodingOptions);

                try
                {
                    bindMethodInfo.Invoke(null, new object?[] { binder, null });
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Error binding gRPC service '{typeof(TService).Name}'.", ex);
                }
            }
        }
        else
        {
            Log.BindMethodNotFound(_logger, typeof(TService));
        }
    }
    private JsonTranscodingProviderServiceBinder <DynamicService> CreateJsonTranscodingBinder <TRequest, TResponse>(
        MethodDescriptor methodDescriptor,
        ServiceMethodProviderContext <DynamicService> context,
        DynamicServiceInvokerResolver invokerResolver)
        where TRequest : class, IMessage, new()
        where TResponse : class, IMessage, new()
    {
        var JsonTranscodingOptions = _serviceProvider.GetRequiredService <IOptions <GrpcJsonTranscodingOptions> >().Value;
        var binder = new JsonTranscodingProviderServiceBinder <DynamicService>(
            context,
            invokerResolver,
            methodDescriptor.Service,
            _serviceProvider.GetRequiredService <IOptions <GrpcServiceOptions> >().Value,
            _serviceProvider.GetRequiredService <IOptions <GrpcServiceOptions <DynamicService> > >().Value,
            _serviceProvider.GetRequiredService <ILoggerFactory>(),
            _serviceProvider.GetRequiredService <IGrpcServiceActivator <DynamicService> >(),
            JsonTranscodingOptions);

        return(binder);
    }