Example #1
0
        /// <summary>
        /// 使用WebApi动态代理客户端
        /// </summary>
        /// <param name="service"></param>
        /// <param name="optionAction"></param>
        public static IServiceCollection AddWebApiProxy(this IServiceCollection service, Action <WebApiProxyOptions> optionAction)
        {
            AppDomain.CurrentDomain.UpdateExcutingAssemblies();

            var option = new WebApiProxyOptions();

            optionAction(option);

            foreach (var o in option.WebApiProxies)
            {
                var types = o.GetType().Assembly.GetTypes().Where(x =>
                                                                  x.IsInterface && x.GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any());

                foreach (var type in types)
                {
                    var proxy = ProxyGenerator.CreateInterfaceProxyWithoutTarget(type, new HttpApiClient(o.BaseUrl ?? option.ProxyHost));
                    service.AddSingleton(type, x => proxy);
                }
            }

            foreach (var type in option.RegisteredServices)
            {
                if (type.Value.IsInterface && type.Value.GetMethods()
                    .SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any())
                {
                    var proxy = ProxyGenerator.CreateInterfaceProxyWithoutTarget(type.Value, new HttpApiClient(type.Key ?? option.ProxyHost));
                    service.AddSingleton(type.Value, x => proxy);
                }
            }

            return(service);
        }
        public static IServiceCollection AddSocketServer(this IServiceCollection services, Action <WebApiProxyOptions> optionAction)
        {
            AppDomain.CurrentDomain.UpdateExcutingAssemblies();

            var options = new WebApiProxyOptions();

            optionAction(options);

            if (!(options.EndPoint is IPEndPoint ipEndpoint))
            {
                throw new ArgumentException("服务端只能使用IPEndPoint");
            }

            var listener   = new TcpListener();
            var middleware = listener.Use <FastMiddleware>();

            foreach (var type in AppDomain.CurrentDomain.GetAllTypes().Where(x => !x.IsInterface))
            {
                var serviceTypes = options.RegisteredServices.Where(x => x.Value.IsAssignableFrom(type));
                if (serviceTypes.Any())
                {
                    middleware.BindService(type);
                }
            }

            listener.Start(ipEndpoint, 128);

            return(services);
        }
Example #3
0
        /// <summary>
        /// 使用WebApi动态代理客户端
        /// </summary>
        /// <param name="service"></param>
        /// <param name="optionAction"></param>
        public static IServiceCollection AddWebApiProxy(this IServiceCollection service, Action <WebApiProxyOptions> optionAction)
        {
            AppDomain.CurrentDomain.UpdateExcutingAssemblies();

            var option = new WebApiProxyOptions();

            optionAction(option);

            foreach (var o in option.WebApiProxies)
            {
                var types = o.GetType().Assembly.GetTypes().Where(x =>
                                                                  x.IsInterface && x.GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any());

                foreach (var type in types)
                {
                    service.AddSingleton(type, x =>
                    {
                        var httpclient = x.GetService <IHttpClient>()
                                         ?? new HttpClientAdapter(new HttpClient(new HttpClientHandler
                        {
                            UseProxy = false,
                        })
                        {
                            Timeout = TimeSpan.FromSeconds(10)
                        });

                        return(ProxyGenerator.CreateInterfaceProxyWithoutTarget(type, new HttpApiClient(httpclient, o.BaseUrl ?? option.ProxyHost)));
                    });
                }
            }

            foreach (var type in option.RegisteredServices)
            {
                if (type.Value.IsInterface)
                {
                    service.AddSingleton(type.Value, x =>
                    {
                        var httpclient = x.GetService <IHttpClient>()
                                         ?? new HttpClientAdapter(new HttpClient(new HttpClientHandler
                        {
                            UseProxy = false,
                        })
                        {
                            Timeout = TimeSpan.FromSeconds(10)
                        });

                        return(ProxyGenerator.CreateInterfaceProxyWithoutTarget(type.Value, new HttpApiClient(httpclient, type.Key ?? option.ProxyHost)));
                    });
                }
            }

            return(service);
        }
        /// <summary>
        /// 使用WebApi动态代理客户端
        /// </summary>
        /// <param name="service"></param>
        /// <param name="optionAction"></param>
        public static IServiceCollection AddWebApiProxy(this IServiceCollection service, Action <WebApiProxyOptions> optionAction)
        {
            AppDomain.CurrentDomain.UpdateExcutingAssemblies();

            var option = new WebApiProxyOptions();

            optionAction(option);

            var proxyService = new List <Type>();

            var proxyGeneratorBuilder = new ProxyGeneratorBuilder();

            foreach (var o in option.WebApiProxies)
            {
                var types = o.GetType().Assembly.GetTypes().Where(x =>
                                                                  x.IsInterface && x.GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any());

                proxyGeneratorBuilder.Configure(config =>
                {
                    config.Interceptors.AddTyped(typeof(HttpApiClient), new object[] { o.BaseUrl ?? option.ProxyHost });
                });

                proxyService.AddRange(types);
            }

            foreach (var type in option.RegisteredServices)
            {
                if (type.Value.IsInterface && type.Value.GetMethods()
                    .SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any())
                {
                    proxyGeneratorBuilder.Configure(config =>
                    {
                        config.Interceptors.AddTyped(typeof(HttpApiClient), new object[] { type.Key ?? option.ProxyHost });
                    });

                    proxyService.Add(type.Value);
                }
            }

            var proxyGenerator = proxyGeneratorBuilder.Build();

            foreach (var type in proxyService)
            {
                var proxy = proxyGenerator.CreateInterfaceProxy(type);
                service.AddSingleton(type, x => proxy);
            }

            return(service);
        }
        /// <summary>
        /// 使用WebApi动态代理客户端
        /// </summary>
        /// <param name="service"></param>
        /// <param name="optionAction"></param>
        public static IServiceCollection AddSocketProxy(this IServiceCollection service, Action <WebApiProxyOptions> optionAction)
        {
            AppDomain.CurrentDomain.UpdateExcutingAssemblies();

            var option = new WebApiProxyOptions();

            optionAction(option);

            foreach (var type in option.RegisteredServices)
            {
                if (type.Value.IsInterface)
                {
                    var proxy = ProxyGenerator.CreateInterfaceProxyWithoutTarget(type.Value, option.EndPoint == null ? new SocketClient(option.ProxyHost) : new SocketClient(option.EndPoint));
                    service.AddSingleton(type.Value, x => proxy);
                }
            }

            return(service);
        }
        public static IShriekBuilder AddWebApiProxy(this IShriekBuilder builder, Action <WebApiProxyOptions> optionAction)
        {
            var option = new WebApiProxyOptions();

            optionAction(option);

            foreach (var o in option.WebApiProxies)
            {
                var types = o.GetType().Assembly.GetTypes().Where(x =>
                                                                  x.IsInterface && x.GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any());

                builder.Services.AddDynamicProxy(config =>
                {
                    config.Interceptors.AddTyped(typeof(HttpApiClient), new object[] { o.BaseUrl }, method => types.Any(t => t.IsAssignableFrom(method.DeclaringType)));
                });
            }

            return(builder);
        }