public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            string boundary = contentType?.Parameters.FirstOrDefault(s => s.Name == "boundary")?.Value;

            if (string.IsNullOrWhiteSpace(boundary))
            {
                boundary = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")));
            }
            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent(boundary);

            foreach (var item in _map)
            {
                object      content         = item.Value;
                HttpContent httpContentPart = null;
                if (content is HttpContent)
                {
                    httpContentPart = (HttpContent)content;
                }
                else if (content is FeignClientHttpRequestContent)
                {
                    httpContentPart = ((FeignClientHttpRequestContent)content).GetHttpContent(contentType, options);
                }
                if (httpContentPart != null)
                {
                    multipartFormDataContent.Add(httpContentPart, item.Key);
                    //multipartFormDataContent.Add(httpContentPart);
                }
            }
            return(multipartFormDataContent);
        }
예제 #2
0
        public FeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, ICacheProvider cacheProvider = null, ILoggerFactory loggerFactory = null)
        {
            FeignOptions = feignOptions;
            _globalFeignClientPipeline    = FeignOptions.FeignClientPipeline as GlobalFeignClientPipeline;
            _serviceIdFeignClientPipeline = _globalFeignClientPipeline?.GetServicePipeline(ServiceId);
            _serviceFeignClientPipeline   = _globalFeignClientPipeline?.GetServicePipeline <TService>();
            _logger = loggerFactory?.CreateLogger(typeof(FeignClientHttpProxy <TService>));
            ServiceDiscoveryHttpClientHandler <TService> serviceDiscoveryHttpClientHandler = new ServiceDiscoveryHttpClientHandler <TService>(this, serviceDiscovery, cacheProvider, _logger);

            if (FeignOptions.AutomaticDecompression.HasValue)
            {
                serviceDiscoveryHttpClientHandler.AutomaticDecompression = feignOptions.AutomaticDecompression.Value;
            }
            if (FeignOptions.UseCookies.HasValue)
            {
                serviceDiscoveryHttpClientHandler.UseCookies = FeignOptions.UseCookies.Value;
            }
            //serviceDiscoveryHttpClientHandler.ShouldResolveService = string.IsNullOrWhiteSpace(Url);
            serviceDiscoveryHttpClientHandler.ShouldResolveService = Url == null;
            serviceDiscoveryHttpClientHandler.AllowAutoRedirect    = false;
            HttpClient = new FeignHttpClient(new FeignDelegatingHandler(serviceDiscoveryHttpClientHandler));
            string baseUrl = serviceDiscoveryHttpClientHandler.ShouldResolveService ? ServiceId ?? "" : Url;

            BaseUrl = BuildBaseUrl(baseUrl);

            InitializingEventArgs <TService> initializingEventArgs = new InitializingEventArgs <TService>(this);

            initializingEventArgs.HttpClient = HttpClient;
            OnInitializing(initializingEventArgs);
            HttpClient = initializingEventArgs.HttpClient;
            if (HttpClient == null)
            {
                throw new ArgumentNullException(nameof(HttpClient));
            }
        }
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            Type type = typeof(T);

            if (!type.IsValueType && Content == null)
            {
                return(null);
            }
            if (type == typeof(byte[]))
            {
                //throw new NotSupportedException();
                return(null);
            }
            if (typeof(Stream).IsAssignableFrom(type))
            {
                //throw new NotSupportedException();
                return(null);
            }

            if (Type.GetTypeCode(type) != TypeCode.Object)
            {
                return(new StringContent(Content.ToString()));
            }

            List <KeyValuePair <string, string> > nameValueCollection = FeignClientUtils.GetObjectStringParameters(Name, Content, options.Converters, options.PropertyNamingPolicy).ToList();
            FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(nameValueCollection);

            return(formUrlEncodedContent);
        }
        /// <summary>
        /// Configure JsonSettings
        /// </summary>
        /// <param name="feignOptions"></param>
        /// <param name="configure"></param>
        public static void ConfigureJsonSettings(this IFeignOptions feignOptions, Action <JsonSerializerSettings> configure)
        {
#if USE_SYSTEM_TEXT_JSON
            configure?.Invoke(Internal.JsonHelper._jsonSerializerOptions);
#else
            configure?.Invoke(Internal.JsonHelper._jsonSerializerSettings);
#endif
        }
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            HttpContent httpContent = RequestFile?.GetHttpContent();

            if (httpContent != null && string.IsNullOrWhiteSpace(httpContent.Headers.ContentDisposition.Name))
            {
                httpContent.Headers.ContentDisposition.Name = Name;
            }
            return(httpContent);
        }
예제 #6
0
        public FeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, ICacheProvider cacheProvider = null, ILoggerFactory loggerFactory = null)
        {
            FeignOptions = feignOptions;
            _globalFeignClientPipeline    = FeignOptions.FeignClientPipeline as GlobalFeignClientPipeline;
            _serviceIdFeignClientPipeline = _globalFeignClientPipeline?.GetServicePipeline(ServiceId);
            _serviceFeignClientPipeline   = _globalFeignClientPipeline?.GetServicePipeline <TService>();
            _logger = loggerFactory?.CreateLogger(typeof(FeignClientHttpProxy <TService>));
            ServiceDiscoveryHttpClientHandler <TService> serviceDiscoveryHttpClientHandler = new ServiceDiscoveryHttpClientHandler <TService>(this, serviceDiscovery, cacheProvider, _logger);

            if (FeignOptions.AutomaticDecompression.HasValue)
            {
                serviceDiscoveryHttpClientHandler.AutomaticDecompression = feignOptions.AutomaticDecompression.Value;
            }
            //serviceDiscoveryHttpClientHandler.ShouldResolveService = string.IsNullOrWhiteSpace(Url);
            serviceDiscoveryHttpClientHandler.ShouldResolveService = Url == null;
            serviceDiscoveryHttpClientHandler.AllowAutoRedirect    = false;
            HttpClient = new FeignHttpClient(new FeignDelegatingHandler(serviceDiscoveryHttpClientHandler));
            string baseUrl = serviceDiscoveryHttpClientHandler.ShouldResolveService ? ServiceId ?? "" : Url;

            if (!baseUrl.StartsWith("http") && baseUrl != "")
            {
                baseUrl = $"http://{baseUrl}";
            }
            if (!string.IsNullOrWhiteSpace(BaseUri))
            {
                if (baseUrl.EndsWith("/"))
                {
                    baseUrl = baseUrl.TrimEnd('/');
                }
                if (BaseUri.StartsWith("/"))
                {
                    baseUrl += BaseUri;
                }
                else
                {
                    baseUrl += "/" + BaseUri;
                }
            }
            if (baseUrl.EndsWith("/"))
            {
                baseUrl = baseUrl.TrimEnd('/');
            }
            BaseUrl = baseUrl;

            InitializingEventArgs <TService> initializingEventArgs = new InitializingEventArgs <TService>(this);

            initializingEventArgs.HttpClient = HttpClient;
            OnInitializing(initializingEventArgs);
            HttpClient = initializingEventArgs.HttpClient;
            if (HttpClient == null)
            {
                throw new ArgumentNullException(nameof(HttpClient));
            }
        }
예제 #7
0
        public FeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, IServiceCacheProvider serviceCacheProvider, ILoggerFactory loggerFactory)
        {
            _feignOptions = feignOptions;
            //_logger = loggerFactory?.CreateLogger(this.GetType());
            _logger = loggerFactory?.CreateLogger(typeof(FeignClientHttpProxy));
            _globalFeignClientPipeline    = _feignOptions?.FeignClientPipeline as GlobalFeignClientPipeline;
            _serviceIdFeignClientPipeline = _globalFeignClientPipeline?.GetServicePipeline(ServiceId);
            ServiceDiscoveryHttpClientHandler serviceDiscoveryHttpClientHandler = new ServiceDiscoveryHttpClientHandler(this, serviceDiscovery, serviceCacheProvider, _logger);

            serviceDiscoveryHttpClientHandler.ShouldResolveService = string.IsNullOrWhiteSpace(Url);
            serviceDiscoveryHttpClientHandler.AllowAutoRedirect    = false;
            HttpClient = new HttpClient(serviceDiscoveryHttpClientHandler);
            string baseUrl = serviceDiscoveryHttpClientHandler.ShouldResolveService ? ServiceId ?? "" : Url;

            if (!baseUrl.StartsWith("http"))
            {
                baseUrl = $"http://{baseUrl}";
            }
            if (!string.IsNullOrWhiteSpace(BaseUri))
            {
                if (baseUrl.EndsWith("/"))
                {
                    baseUrl = baseUrl.TrimEnd('/');
                }
                if (BaseUri.StartsWith("/"))
                {
                    baseUrl += BaseUri;
                }
                else
                {
                    baseUrl += "/" + BaseUri;
                }
            }

            if (baseUrl.EndsWith("/"))
            {
                baseUrl = baseUrl.TrimEnd('/');
            }
            BaseUrl = baseUrl;

            InitializingEventArgs initializingEventArgs = new InitializingEventArgs(this);

            initializingEventArgs.HttpClient = HttpClient;
            OnInitializing(initializingEventArgs);
            HttpClient = initializingEventArgs.HttpClient;
            if (HttpClient == null)
            {
                throw new ArgumentNullException(nameof(HttpClient));
            }
        }
예제 #8
0
        /// <summary>
        /// Configure JsonSettings
        /// </summary>
        /// <param name="feignOptions"></param>
        /// <param name="configure"></param>
        public static void ConfigureJsonSettings(this IFeignOptions feignOptions, Action <JsonSerializerSettings> configure)
        {
#if USE_SYSTEM_TEXT_JSON
            var setting = feignOptions.JsonProvider as SystemTextJsonProvider;
            if (setting != null)
            {
                configure?.Invoke(setting._jsonSerializerOptions);
            }
#else
            var setting = feignOptions.JsonProvider as NewtonsoftJsonProvider;
            if (setting != null)
            {
                configure?.Invoke(setting._jsonSerializerSettings);
            }
#endif
        }
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            if (RequestFileForm == null)
            {
                return(null);
            }
            string boundary = contentType?.Parameters.FirstOrDefault(s => s.Name == "boundary")?.Value;

            if (string.IsNullOrWhiteSpace(boundary))
            {
                boundary = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")));
            }
            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent(boundary);

            if (RequestFileForm.RequestFiles != null)
            {
                foreach (var requestFile in RequestFileForm.RequestFiles)
                {
                    HttpContent httpContent = requestFile?.GetHttpContent();
                    if (httpContent != null)
                    {
                        multipartFormDataContent.Add(httpContent);
                    }
                }
            }

            //other property
            foreach (var property in RequestFileForm.GetType().GetProperties())
            {
                if (property.GetMethod == null)
                {
                    continue;
                }
                if (typeof(IHttpRequestFile).IsAssignableFrom(property.PropertyType) || property.PropertyType.IsGenericType && property.PropertyType.GenericTypeArguments.Any(s => typeof(IHttpRequestFile).IsAssignableFrom(s)))
                {
                    continue;
                }
                object value = property.GetValue(RequestFileForm);
                if (value == null)
                {
                    continue;
                }
                HttpContent httpContent = new StringContent(value.ToString());
                multipartFormDataContent.Add(httpContent, FeignClientUtils.GetName(property, options.PropertyNamingPolicy));
            }
            return(multipartFormDataContent);
        }
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            Type type = typeof(T);

            if (type == typeof(byte[]) || typeof(Stream).IsAssignableFrom(type))
            {
                //throw new NotSupportedException($"不支持{type.FullName}类型的参数");
                return(null);
            }
            if (contentType == null)
            {
                return(new ObjectStringContent(Content, Encoding.UTF8, "application/json", options.JsonProvider));
            }
            ObjectStringContent content = new ObjectStringContent(Content, options.JsonProvider);

            content.Headers.ContentType = contentType;
            return(content);
        }
 public static TFeignBuilder AddFeignClients <TFeignBuilder>(this TFeignBuilder feignBuilder, IFeignOptions options) where TFeignBuilder : IFeignBuilder
 {
     if (options.Assemblies.Count == 0)
     {
         feignBuilder.AddFeignClients(Assembly.GetEntryAssembly(), options.Lifetime);
     }
     else
     {
         foreach (var assembly in options.Assemblies)
         {
             feignBuilder.AddFeignClients(assembly, options.Lifetime);
         }
     }
     feignBuilder.AddLoggerFactory <DefaultLoggerFactory>();
     feignBuilder.AddServiceCacheProvider <DefaultServiceCacheProvider>();
     feignBuilder.AddServiceDiscovery <DefaultServiceDiscovery>();
     feignBuilder.AddService <IFeignOptions>(options);
     feignBuilder.TypeBuilder.FinishBuild();
     return(feignBuilder);
 }
        public static IDependencyInjectionFeignBuilder AddFeignClients(this IServiceCollection services, IFeignOptions options)
        {
            if (options == null)
            {
                options = new FeignOptions();
            }

            DependencyInjectionFeignBuilder feignBuilder = new DependencyInjectionFeignBuilder();

            feignBuilder.Services = services;
            feignBuilder.Options  = options;
            feignBuilder.AddDefaultFeignClients()
            .AddLoggerFactory <LoggerFactory>()
            //.AddCacheProvider<CacheProvider>()
            .AddCacheProvider <JsonCacheProvider>()
            ;
            return(feignBuilder);
        }
예제 #13
0
        public static IAutofacFeignBuilder AddFeignClients(this ContainerBuilder containerBuilder, IFeignOptions options)
        {
            if (options == null)
            {
                options = new FeignOptions();
            }
            AutofacFeignBuilder feignBuilder = new AutofacFeignBuilder();

            feignBuilder.ContainerBuilder = containerBuilder;
            feignBuilder.Options          = options;
            feignBuilder.AddDefaultFeignClients();
            return(feignBuilder);
        }
예제 #14
0
        public static ICastleWindsorFeignBuilder AddFeignClients(this IWindsorContainer windsorContainer, IFeignOptions options)
        {
            if (options == null)
            {
                options = new FeignOptions();
            }

            CastleWindsorFeignBuilder feignBuilder = new CastleWindsorFeignBuilder();

            feignBuilder.WindsorContainer = windsorContainer;
            feignBuilder.Options          = options;
            feignBuilder.AddDefaultFeignClients();
            return(feignBuilder);
        }
예제 #15
0
 /// <summary>
 /// 根据指定的媒体类型获取对应的HttpContent
 /// </summary>
 /// <param name="contentType"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public abstract HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options);
예제 #16
0
 /// <summary>
 /// 获取随请求一起发送的HttpContent
 /// </summary>
 /// <returns></returns>
 public HttpContent GetHttpContent(IFeignOptions options)
 {
     return(RequestContent?.GetHttpContent(ContentType, options));
 }
예제 #17
0
 public JsonMediaTypeFormatter(string mediaType, IFeignOptions options)
 {
     MediaType = mediaType;
     _options  = options;
 }
 public FallbackFeignClientHttpProxy(TFallback fallback, IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, ICacheProvider cacheProvider = null, ILoggerFactory loggerFactory = null) : base(feignOptions, serviceDiscovery, cacheProvider, loggerFactory)
 {
     Fallback = fallback;
 }
예제 #19
0
 public JsonMediaTypeFormatter(IFeignOptions options) : this(Constants.MediaTypes.APPLICATION_JSON, options)
 {
 }
 public TestFeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, IServiceCacheProvider serviceCacheProvider, Feign.Logging.ILoggerFactory loggerFactory) : base(feignOptions, serviceDiscovery, serviceCacheProvider, loggerFactory)
 {
 }
 public FallbackFeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, IServiceCacheProvider serviceCacheProvider, ILoggerFactory loggerFactory, TFallback fallback) : base(feignOptions, serviceDiscovery, serviceCacheProvider, loggerFactory, fallback)
 {
     Fallback = fallback;
     _serviceTypeFeignClientPipeline = _globalFeignClientPipeline?.GetServicePipeline <TService>();
 }
예제 #22
0
 public FallbackFactoryFeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, IServiceCacheProvider serviceCacheProvider, ILoggerFactory loggerFactory, TFallbackFactory fallbackFactory) : base(feignOptions, serviceDiscovery, serviceCacheProvider, loggerFactory, GetFallback(fallbackFactory))
 {
     FallbackFactory = fallbackFactory;
 }
예제 #23
0
 public FallbackFeignClientHttpProxy(IFeignOptions feignOptions, IServiceDiscovery serviceDiscovery, IServiceCacheProvider serviceCacheProvider, ILoggerFactory loggerFactory, object fallback) : base(feignOptions, serviceDiscovery, serviceCacheProvider, loggerFactory)
 {
     _fallback = fallback;
 }