From a3abb32d4b87185001a46c0373a5e9c614186552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enner=20P=C3=A9rez?= Date: Sat, 31 Dec 2016 01:42:08 -0400 Subject: [PATCH] Support.Web.Methods Due compatibility build error: Can't use with .NET 4.0 target --- .../Sample.Application.csproj | 28 +- Samples/Sample.Application/app.config | 4 +- Samples/Sample.Application/packages.config | 3 +- Support.Net/Support.Net.csproj | 28 +- Support.Net/app.config | 4 +- Support.Net/packages.config | 3 +- Support.Web/Methods.cs | 300 +++++++++--------- Support.Web/Support.Web.csproj | 28 +- Support.Web/app.config | 4 +- Support.Web/packages.config | 3 +- 10 files changed, 179 insertions(+), 226 deletions(-) diff --git a/Samples/Sample.Application/Sample.Application.csproj b/Samples/Sample.Application/Sample.Application.csproj index 7231d7a..1eb57b9 100644 --- a/Samples/Sample.Application/Sample.Application.csproj +++ b/Samples/Sample.Application/Sample.Application.csproj @@ -48,33 +48,17 @@ - - ..\..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.IO.dll + + ..\..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll True - - ..\..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.dll + + ..\..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll True - - ..\..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Extensions.dll - True - - - ..\..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Primitives.dll - True - - - ..\..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.WebRequest.dll - True - - - ..\..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Runtime.dll - True - - - ..\..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Threading.Tasks.dll + + ..\..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll True diff --git a/Samples/Sample.Application/app.config b/Samples/Sample.Application/app.config index 40cecd8..4d47203 100644 --- a/Samples/Sample.Application/app.config +++ b/Samples/Sample.Application/app.config @@ -4,11 +4,11 @@ - + - + diff --git a/Samples/Sample.Application/packages.config b/Samples/Sample.Application/packages.config index 14041bb..56da543 100644 --- a/Samples/Sample.Application/packages.config +++ b/Samples/Sample.Application/packages.config @@ -1,8 +1,7 @@  - + - \ No newline at end of file diff --git a/Support.Net/Support.Net.csproj b/Support.Net/Support.Net.csproj index 73a177d..6f48dc7 100644 --- a/Support.Net/Support.Net.csproj +++ b/Support.Net/Support.Net.csproj @@ -46,33 +46,17 @@ - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.IO.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll True - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll True - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Extensions.dll - True - - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Primitives.dll - True - - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.WebRequest.dll - True - - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Runtime.dll - True - - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Threading.Tasks.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll True diff --git a/Support.Net/app.config b/Support.Net/app.config index 40cecd8..4d47203 100644 --- a/Support.Net/app.config +++ b/Support.Net/app.config @@ -4,11 +4,11 @@ - + - + diff --git a/Support.Net/packages.config b/Support.Net/packages.config index 14041bb..56da543 100644 --- a/Support.Net/packages.config +++ b/Support.Net/packages.config @@ -1,8 +1,7 @@  - + - \ No newline at end of file diff --git a/Support.Web/Methods.cs b/Support.Web/Methods.cs index c31e496..d7420a1 100644 --- a/Support.Web/Methods.cs +++ b/Support.Web/Methods.cs @@ -5,8 +5,8 @@ using System.Threading.Tasks; using System.Web; using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; +//using System.Net.Http; +//using System.Net.Http.Headers; using System.IO; using System.ComponentModel; using System.Reflection; @@ -24,151 +24,155 @@ namespace Web public static class Methods { - [DefaultValue(DecompressionMethods.GZip)] - public static DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip; - - [DefaultValue("text/plain")] - public static string Accept { get; set; } = "text/plain"; - - internal static HttpClient getHttpClient(string token = "") - { - var handler = new HttpClientHandler(); - handler.AutomaticDecompression = AutomaticDecompression; - - HttpClient client = new HttpClient(handler); - - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Accept)); - - switch (handler.AutomaticDecompression) - { - case DecompressionMethods.GZip: - client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); - break; - case DecompressionMethods.Deflate: - client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); - break; - default: - break; - } - - if (!string.IsNullOrEmpty(token)) - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); - - return client; - } - internal static async Task processHttpResponseMessage(HttpResponseMessage response) - { - object result = null; - - if (typeof(R) == typeof(string)) - result = await response.Content.ReadAsStringAsync(); - - if (typeof(R) == typeof(Stream)) - result = await response.Content.ReadAsStreamAsync(); - - if (typeof(R) == typeof(byte[])) - result = await response.Content.ReadAsByteArrayAsync(); - - if (result == null) - { - var path = Path.Combine(Assembly.GetEntryAssembly().DirectoryPath(), "Newtonsoft.Json.dll"); - if (File.Exists(path)) - { - var newtonsoftJson = Assembly.LoadFile(path); - var jsonConvert = newtonsoftJson.GetType("Newtonsoft.Json.JsonConvert"); - - var data = await response.Content.ReadAsStringAsync(); - var methods = from item in jsonConvert.GetMethods() - where item.Name == "DeserializeObject" && item.IsGenericMethodDefinition - select item; - var generic = methods.FirstOrDefault().MakeGenericMethod(typeof(R)); - result = (R)generic.Invoke(null, new object[] { data }); - } - } - - if (result != null) - return (R)result; - else - return default(R); - } - - public static async Task GetAsync(string url, string token = "") - { - try - { - var client = getHttpClient(token); - var response = await client.GetAsync(url); - return await processHttpResponseMessage(response); - } - catch (Exception ex) - { - ex.Message.DebugThis(); - return default(R); - } - } - public async static Task GetAsync(string url, string token = "") - { - return await GetAsync(url, token); - } - - public async static Task PostAsync(string url, T model = default(T), HttpContent content = null, string token = "") - { - try - { - var client = getHttpClient(token); - var response = await client.PostAsync(url, content); - return await processHttpResponseMessage(response); - } - catch (Exception ex) - { - ex.Message.DebugThis(); - return default(R); - } - } - public async static Task PostAsync(string url, T model = default(T), HttpContent content = null, string token = "") - { - return await PostAsync(url, model, content, token); - } - - public async static Task PutAsync(string url, T model = default(T), HttpContent content = null, string token = "") - { - try - { - - var client = getHttpClient(token); - var response = await client.PutAsync(url, content); - return await processHttpResponseMessage(response); - - } - catch (Exception ex) - { - ex.Message.DebugThis(); - return default(R); - } - } - public async static Task PutAsync(string url, T model = default(T), HttpContent content = null, string token = "") - { - return await PutAsync(url, model, content, token); - } - - public static async Task DeleteAsync(string url, string token = "") - { - try - { - var client = getHttpClient(token); - var response = await client.DeleteAsync(url); - return await processHttpResponseMessage(response); - } - catch (Exception ex) - { - ex.Message.DebugThis(); - return default(R); - } - } - public async static Task DeleteAsync(string url, string token = "") - { - return await DeleteAsync(url, token); - } + // Due compatibility build error: Can't use with .NET 4.0 target + + #region Deprecated + //[DefaultValue(DecompressionMethods.GZip)] + //public static DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip; + + //[DefaultValue("text/plain")] + //public static string Accept { get; set; } = "text/plain"; + + //internal static HttpClient getHttpClient(string token = "") + //{ + // var handler = new HttpClientHandler(); + // handler.AutomaticDecompression = AutomaticDecompression; + + // HttpClient client = new HttpClient(handler); + + // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Accept)); + + // switch (handler.AutomaticDecompression) + // { + // case DecompressionMethods.GZip: + // client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); + // break; + // case DecompressionMethods.Deflate: + // client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); + // break; + // default: + // break; + // } + + // if (!string.IsNullOrEmpty(token)) + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + + // return client; + //} + //internal static async Task processHttpResponseMessage(HttpResponseMessage response) + //{ + // object result = null; + + // if (typeof(R) == typeof(string)) + // result = await response.Content.ReadAsStringAsync(); + + // if (typeof(R) == typeof(Stream)) + // result = await response.Content.ReadAsStreamAsync(); + + // if (typeof(R) == typeof(byte[])) + // result = await response.Content.ReadAsByteArrayAsync(); + + // if (result == null) + // { + // var path = Path.Combine(Assembly.GetEntryAssembly().DirectoryPath(), "Newtonsoft.Json.dll"); + // if (File.Exists(path)) + // { + // var newtonsoftJson = Assembly.LoadFile(path); + // var jsonConvert = newtonsoftJson.GetType("Newtonsoft.Json.JsonConvert"); + + // var data = await response.Content.ReadAsStringAsync(); + // var methods = from item in jsonConvert.GetMethods() + // where item.Name == "DeserializeObject" && item.IsGenericMethodDefinition + // select item; + // var generic = methods.FirstOrDefault().MakeGenericMethod(typeof(R)); + // result = (R)generic.Invoke(null, new object[] { data }); + // } + // } + + // if (result != null) + // return (R)result; + // else + // return default(R); + //} + + //public static async Task GetAsync(string url, string token = "") + //{ + // try + // { + // var client = getHttpClient(token); + // var response = await client.GetAsync(url); + // return await processHttpResponseMessage(response); + // } + // catch (Exception ex) + // { + // ex.Message.DebugThis(); + // return default(R); + // } + //} + //public async static Task GetAsync(string url, string token = "") + //{ + // return await GetAsync(url, token); + //} + + //public async static Task PostAsync(string url, T model = default(T), HttpContent content = null, string token = "") + //{ + // try + // { + // var client = getHttpClient(token); + // var response = await client.PostAsync(url, content); + // return await processHttpResponseMessage(response); + // } + // catch (Exception ex) + // { + // ex.Message.DebugThis(); + // return default(R); + // } + //} + //public async static Task PostAsync(string url, T model = default(T), HttpContent content = null, string token = "") + //{ + // return await PostAsync(url, model, content, token); + //} + + //public async static Task PutAsync(string url, T model = default(T), HttpContent content = null, string token = "") + //{ + // try + // { + + // var client = getHttpClient(token); + // var response = await client.PutAsync(url, content); + // return await processHttpResponseMessage(response); + + // } + // catch (Exception ex) + // { + // ex.Message.DebugThis(); + // return default(R); + // } + //} + //public async static Task PutAsync(string url, T model = default(T), HttpContent content = null, string token = "") + //{ + // return await PutAsync(url, model, content, token); + //} + + //public static async Task DeleteAsync(string url, string token = "") + //{ + // try + // { + // var client = getHttpClient(token); + // var response = await client.DeleteAsync(url); + // return await processHttpResponseMessage(response); + // } + // catch (Exception ex) + // { + // ex.Message.DebugThis(); + // return default(R); + // } + //} + //public async static Task DeleteAsync(string url, string token = "") + //{ + // return await DeleteAsync(url, token); + //} + #endregion } @@ -176,4 +180,4 @@ public async static Task DeleteAsync(string url, string token = "") #if PORTABLE } #endif -} +} \ No newline at end of file diff --git a/Support.Web/Support.Web.csproj b/Support.Web/Support.Web.csproj index a9146e0..25a4318 100644 --- a/Support.Web/Support.Web.csproj +++ b/Support.Web/Support.Web.csproj @@ -46,33 +46,17 @@ - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.IO.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll True - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll True - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Extensions.dll - True - - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.Primitives.dll - True - - - ..\packages\Microsoft.Net.Http.2.2.20\lib\net40\System.Net.Http.WebRequest.dll - True - - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Runtime.dll - True - - - ..\packages\Microsoft.Bcl.1.1.8\lib\net40\System.Threading.Tasks.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll True diff --git a/Support.Web/app.config b/Support.Web/app.config index 40cecd8..4d47203 100644 --- a/Support.Web/app.config +++ b/Support.Web/app.config @@ -4,11 +4,11 @@ - + - + diff --git a/Support.Web/packages.config b/Support.Web/packages.config index 14041bb..56da543 100644 --- a/Support.Web/packages.config +++ b/Support.Web/packages.config @@ -1,8 +1,7 @@  - + - \ No newline at end of file