예제 #1
0
        static void SetUpEndpointMappings(EndpointConfigurationElement mappings, Action <Type, string> mappingFunction)
        {
            var mappingElements = mappings.OrderBy(c => !c.IsAssemblyName).ToList();

            foreach (var element in mappingElements)
            {
                if (element.IsAssemblyName)
                {
                    var assemblyName = element.Messages;
                    var assembly     = LoadAssembly(assemblyName);

                    foreach (var type in assembly.GetTypes())
                    {
                        mappingFunction(type, element.Endpoint);
                    }
                }
                else
                {
                    var typeName    = element.Messages;
                    var messageType = Type.GetType(typeName);

                    if (messageType == null)
                    {
                        throw new ConfigurationErrorsException(
                                  string.Format(@"Could not find the message type {0}. If you choose to map a specific message type, please ensure that the type is available for Rebus to load. This requires that the assembly can be found in Rebus' current runtime directory, that the type is available, and that any (of the optional) version and key requirements are matched", typeName));
                    }

                    mappingFunction(messageType, element.Endpoint);
                }
            }
        }
        static void SetUpEndpointMappings(EndpointConfigurationElement mappings, Action<Type, string> mappingFunction)
        {
            var mappingElements = mappings.OrderBy(c => !c.IsAssemblyName).ToList();

            foreach (var element in mappingElements)
            {
                if (element.IsAssemblyName)
                {
                    var assemblyName = element.Messages;
                    var assembly = LoadAssembly(assemblyName);

                    foreach (var type in assembly.GetTypes())
                    {
                        mappingFunction(type, element.Endpoint);
                    }
                }
                else
                {
                    var typeName = element.Messages;
                    var messageType = Type.GetType(typeName);

                    if (messageType == null)
                    {
                        throw new ConfigurationErrorsException(
                            string.Format(@"Could not find the message type {0}. If you choose to map a specific message type, please ensure that the type is available for Rebus to load. This requires that the assembly can be found in Rebus' current runtime directory, that the type is available, and that any (of the optional) version and key requirements are matched", typeName));
                    }

                    mappingFunction(messageType, element.Endpoint);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 创建gRPC信道
        /// </summary>
        /// <param name="endpoint">终结点配置</param>
        /// <returns>gRPC信道</returns>
        private static GrpcChannel CreateGrpcChannel(EndpointElement endpoint)
        {
            //读取配置文件
            EndpointConfigurationElement   endpointConfiguration   = null;
            IList <AuthInterceptorElement> authInterceptorElements = new List <AuthInterceptorElement>();

            if (!string.IsNullOrWhiteSpace(endpoint.EndpointConfiguration))
            {
                endpointConfiguration = GrpcSetting.EndpointConfigurations[endpoint.EndpointConfiguration];
            }
            if (!string.IsNullOrWhiteSpace(endpoint.AuthInterceptors))
            {
                string[] authInterceptorNames = endpoint.AuthInterceptors.Split(',');
                authInterceptorElements = GrpcSetting.AuthInterceptors.Where <KeyValuePair <string, AuthInterceptorElement> >(x => authInterceptorNames.Contains(x.Key)).Select(x => x.Value).ToList();
            }

            //构造身份凭据
            IList <CallCredentials> callCredentials = new List <CallCredentials>();

            foreach (AuthInterceptorElement authInterceptorElement in authInterceptorElements)
            {
                Assembly         assembly        = Assembly.Load(authInterceptorElement.Assembly);
                Type             type            = assembly.GetType(authInterceptorElement.Type);
                IAuthInterceptor authInterceptor = (IAuthInterceptor)Activator.CreateInstance(type);

                CallCredentials callCredential = CallCredentials.FromInterceptor(authInterceptor.AuthIntercept);
                callCredentials.Add(callCredential);
            }
            CallCredentials credentials = null;

            if (callCredentials.Count == 1)
            {
                credentials = callCredentials.Single();
            }
            if (callCredentials.Count > 1)
            {
                credentials = CallCredentials.Compose(callCredentials.ToArray());
            }

            //构造gRPC信道选项
            GrpcChannelOptions channelOptions = new GrpcChannelOptions
            {
                MaxSendMessageSize                   = endpointConfiguration?.MaxSendMessageSize,
                MaxReceiveMessageSize                = endpointConfiguration?.MaxReceiveMessageSize,
                MaxRetryAttempts                     = endpointConfiguration?.MaxRetryAttempts,
                MaxRetryBufferSize                   = endpointConfiguration?.MaxRetryBufferSize,
                MaxRetryBufferPerCallSize            = endpointConfiguration?.MaxRetryBufferPerCallSize,
                DisposeHttpClient                    = endpointConfiguration?.DisposeHttpClient ?? false,
                ThrowOperationCanceledOnCancellation = endpointConfiguration?.ThrowOperationCanceledOnCancellation ?? false
            };

            if (credentials != null)
            {
                channelOptions.Credentials = ChannelCredentials.Create(new SslCredentials(), credentials);
            }

            //创建gRPC信道
            GrpcChannel channel = GrpcChannel.ForAddress(endpoint.Address, channelOptions);

            return(channel);
        }