/// <summary> /// Adds a <see cref="HttpClientProxy{T}"/> instance to a <see cref="IServiceCollection"/>. /// </summary> /// <typeparam name="T">The proxy type.</typeparam> /// <param name="services">A <see cref="IServiceCollection"/> instance.</param> /// <param name="baseUrlFunc">A function to return the base url.</param> /// <param name="optionAction">A function to configure the proxy options.</param> /// <returns>The <see cref="IServiceCollection"/> instance.</returns> public static IServiceCollection AddHttpClientProxy <T>( this IServiceCollection services, Func <IServiceProvider, string> baseUrlFunc, Action <HttpClientProxyOptions> optionAction = null) where T : class { if (baseUrlFunc == null) { throw new ArgumentNullException(nameof(baseUrlFunc)); } services.AddSingleton <T>( sp => { var options = new HttpClientProxyOptions() { Services = sp }; optionAction?.Invoke(options); var baseUri = baseUrlFunc(sp); var proxy = new HttpClientProxy <T>( baseUri, options); return(proxy.GetProxyObject()); }); return(services); }
/// <summary> /// Initializes a new instance of the <see cref="HttpClientProxy{T}"/> class. /// </summary> /// <param name="baseUri">The base uri.</param> /// <param name="options">Client proxy options.</param> public HttpClientProxy(string baseUri, HttpClientProxyOptions options = null) { Utility.ThrowIfNotInterface(typeof(T), "T"); Utility.ThrowIfArgumentNullOrEmpty(baseUri, nameof(baseUri)); this.options = options ?? new HttpClientProxyOptions(); this.baseUri = baseUri ?? options.BaseUri; this.CheckContractAttribute(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpClientProxy{T}"/> class. /// </summary> /// <param name="options">Client proxy options.</param> public HttpClientProxy(HttpClientProxyOptions options) { Utility.ThrowIfNotInterface(typeof(T), "T"); Utility.ThrowIfArgumentNull(options, nameof(options)); this.options = options ?? new HttpClientProxyOptions(); this.baseUri = options.BaseUri; this.CheckContractAttribute(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpRequestContext"/> class. /// </summary> /// <param name="methodInfo">The methods <see cref="MethodInfo"/>.</param> /// <param name="arguments">The methods arguments.</param> /// <param name="contentType">The content type.</param> /// <param name="options">The proxy options.</param> public HttpRequestContext( MethodInfo methodInfo, object[] arguments, string contentType, HttpClientProxyOptions options) { this.MethodInfo = methodInfo; this.Arguments = arguments; this.contentType = contentType; this.options = options; }