From f127a7238c435e69a4b39d39119e676508de263a Mon Sep 17 00:00:00 2001 From: yubaolee Date: Fri, 8 Jul 2016 22:52:40 +0800 Subject: [PATCH] add missing files --- Infrastructure/Cache/CacheContext.cs | 44 +++++ Infrastructure/Cache/EnyimMemcachedContext.cs | 49 +++++ Infrastructure/Cache/SessionContext.cs | 61 ++++++ Infrastructure/HttpHelper.cs | 182 ++++++++++++++++++ Infrastructure/UriUtil.cs | 74 +++++++ OpenAuth.App/packages.config | 7 + 6 files changed, 417 insertions(+) create mode 100644 Infrastructure/Cache/CacheContext.cs create mode 100644 Infrastructure/Cache/EnyimMemcachedContext.cs create mode 100644 Infrastructure/Cache/SessionContext.cs create mode 100644 Infrastructure/HttpHelper.cs create mode 100644 Infrastructure/UriUtil.cs create mode 100644 OpenAuth.App/packages.config diff --git a/Infrastructure/Cache/CacheContext.cs b/Infrastructure/Cache/CacheContext.cs new file mode 100644 index 00000000..3d0e66e4 --- /dev/null +++ b/Infrastructure/Cache/CacheContext.cs @@ -0,0 +1,44 @@ +using System; + +namespace Infrastructure.Cache +{ + /// + /// 策略模式缓存组件,可实现动态插拔 + /// + public abstract class CacheContext : IDisposable + { + /// + /// 初始化缓存组件 + /// + public abstract void Init(); + + /// + /// 获取缓存项 + /// + /// 缓存对象类型 + /// 键 + /// 缓存对象 + public abstract T Get(string key) where T : class; + + /// + /// 设置缓存项 + /// + /// 缓存对象类型 + /// 键 + /// 缓存对象 + /// true成功,false失败 + public abstract bool Set(string key, T t) where T : class; + + /// + /// 移除一个缓存项 + /// + /// 缓存项key + /// true成功,false失败 + public abstract bool Remove(string key); + + /// + /// 释放缓存组件 + /// + public abstract void Dispose(); + } +} \ No newline at end of file diff --git a/Infrastructure/Cache/EnyimMemcachedContext.cs b/Infrastructure/Cache/EnyimMemcachedContext.cs new file mode 100644 index 00000000..4944b4f5 --- /dev/null +++ b/Infrastructure/Cache/EnyimMemcachedContext.cs @@ -0,0 +1,49 @@ +// *********************************************************************** +// Assembly : Infrastructure +// Author : yubaolee +// Created : 06-21-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 06-21-2016 +// Contact : +// File: EnyimMemcachedContext.cs +// *********************************************************************** + + +using Enyim.Caching; +using Enyim.Caching.Memcached; + +namespace Infrastructure.Cache +{ + public sealed class EnyimMemcachedContext : CacheContext + { + private readonly MemcachedClient _memcachedClient = new MemcachedClient("memcached"); + + public override void Init() + { + } + + public override T Get(string key) + { + return _memcachedClient.Get(key); + } + + public override bool Set(string key, T t) + { + return _memcachedClient.Store(StoreMode.Set, key, t); + } + + public override bool Remove(string key) + { + return _memcachedClient.Remove(key); + } + + public override void Dispose() + { + if (_memcachedClient != null) + { + _memcachedClient.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/Infrastructure/Cache/SessionContext.cs b/Infrastructure/Cache/SessionContext.cs new file mode 100644 index 00000000..4b774f53 --- /dev/null +++ b/Infrastructure/Cache/SessionContext.cs @@ -0,0 +1,61 @@ +// *********************************************************************** +// Assembly : Infrastructure +// Author : yubaolee +// Created : 06-21-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 06-21-2016 +// Contact : +// File: EnyimMemcachedContext.cs +// *********************************************************************** + + +using System; +using System.Web; + +namespace Infrastructure.Cache +{ + public sealed class SessionContext : CacheContext + { + + public override void Init() + { + } + + public override T Get(string key) + { + return (T) HttpContext.Current.Application[key]; + } + + public override bool Set(string key, T t) + { + try + { + HttpContext.Current.Application[key] = t; + return true; + } + catch (Exception) + { + return false; + } + } + + public override bool Remove(string key) + { + try + { + HttpContext.Current.Application[key] = null; + return true; + } + catch (Exception) + { + return false; + } + } + + public override void Dispose() + { + + } + } +} \ No newline at end of file diff --git a/Infrastructure/HttpHelper.cs b/Infrastructure/HttpHelper.cs new file mode 100644 index 00000000..1aa0c52f --- /dev/null +++ b/Infrastructure/HttpHelper.cs @@ -0,0 +1,182 @@ +// *********************************************************************** +// Assembly : Infrastructure +// Author : yubaolee +// Created : 06-21-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 06-21-2016 +// Contact : +// File: HttpHelper.cs +// *********************************************************************** + +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; + +namespace Infrastructure +{ + /// + /// WebApi请求帮助类 + /// + public class HttpHelper + { + private readonly HttpClient _httpClient; + private readonly string _baseUriAddress; + + #region 构造 + + /// + /// + /// 请求的基地址 + public HttpHelper(string baseAddress = "") + { + if (string.IsNullOrEmpty(baseAddress)) + { + this._baseUriAddress = ConfigurationManager.AppSettings["CommonApiUriString"]; + } + else + { + this._baseUriAddress = baseAddress; + } + _httpClient = new HttpClient {BaseAddress = new Uri(_baseUriAddress)}; + + + } + + /// + /// 创建带用户信息的请求客户端 + /// + /// 用户账号 + /// 用户密码,当WebApi端不要求密码验证时,可传空串 + /// The URI string. + public HttpHelper(string userName, string pwd = "", string baseAddress = "") + : this(baseAddress) + { + if (!string.IsNullOrEmpty(userName)) + { + _httpClient.DefaultRequestHeaders.Authorization = CreateBasicCredentials(userName, pwd); + + } + } + + #endregion + + public string Post(string requestUrl) + { + var result = _httpClient.PostAsync(requestUrl, new StringContent("")); + return result.Result.Content.ReadAsStringAsync().Result; + } + + + /// + /// Post数据 返回string类型 + /// + /// + /// 实体 + /// 例如/api/Files/UploadFile + /// + public string Post(T entity, string requestUri) + { + HttpResponseMessage respsonse = RequestPost(entity, requestUri); + return respsonse.Content.ReadAsStringAsync().Result; + } + /// + /// Post数据 返回byte[]类型 用于批量下载的时候请求接口就直接返回文件字节以供下载 + /// + /// + public byte[] PostGetByte(T entity, string requestUri) + { + HttpResponseMessage respsonse = RequestPost(entity, requestUri); + return respsonse.Content.ReadAsByteArrayAsync().Result; + } + + /// + /// 以Post方式请求数据 返回HttpResponseMessage + /// + /// 请求传入的对象类型 + /// 请求传入的对象 + /// 请求地址 + /// + private HttpResponseMessage RequestPost(T entity, string requestUri) + { + string request = string.Empty; + if (entity != null) + request = JsonHelper.Instance.Serialize(entity); + HttpContent httpContent = new StringContent(request); + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + requestUri = string.Concat(_baseUriAddress, requestUri); + var result = _httpClient.PostAsync(requestUri, httpContent); + return result.Result; + } + + /// + /// Get请求数据 + /// yubaolee 2016-3-3 重构与post同样异步调用 + /// + /// 参数字典 + /// 例如/api/Files/UploadFile + /// + public string Get(Dictionary parameters, string requestUri) + { + string strParam = String.Empty; + if (parameters != null) + { + strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value)); + requestUri = string.Concat(_baseUriAddress, requestUri, '?', strParam); + } + else + { + requestUri = string.Concat(_baseUriAddress, requestUri); + } + + using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri)) + { + return _httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result; + + } + + + } + /// + /// 发送一个没有参数的HTTP请求 + /// yubaolee 2016-3-3 重构引用现有的get方法 + /// + /// The request URI. + /// System.String. + public string Get(string requestUri) + { + return Get(null, requestUri); + } + /// + /// Get请求数据 + /// + /// 参数字典 + /// 例如/api/Files/UploadFile + /// 实体对象 + public T Get(Dictionary parameters, string requestUri) where T : class + { + string jsonString = Get(parameters, requestUri); + if (string.IsNullOrEmpty(jsonString)) + return null; + + return JsonHelper.Instance.Deserialize(jsonString); + } + + private AuthenticationHeaderValue CreateBasicCredentials(string userName, string password) + { + string toEncode = userName + ":" + password; + // The current HTTP specification says characters here are ISO-8859-1. + // However, the draft specification for the next version of HTTP indicates this encoding is infrequently + // used in practice and defines behavior only for ASCII. + Encoding encoding = Encoding.GetEncoding("utf-8"); + byte[] toBase64 = encoding.GetBytes(toEncode); + string parameter = Convert.ToBase64String(toBase64); + + return new AuthenticationHeaderValue("Basic", parameter); + } + } +} diff --git a/Infrastructure/UriUtil.cs b/Infrastructure/UriUtil.cs new file mode 100644 index 00000000..7c60e434 --- /dev/null +++ b/Infrastructure/UriUtil.cs @@ -0,0 +1,74 @@ +// *********************************************************************** +// Assembly : Infrastructure +// Author : yubaolee +// Created : 06-21-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 06-22-2016 +// Contact : +// File: UriUtil.cs +// *********************************************************************** + +using System; +using System.Collections.Specialized; +using System.Web; + +namespace Infrastructure +{ + /// + /// URl帮助类 + /// + public class UriUtil + { + /// + /// 在URL后面追加参数 + /// + /// + /// + /// + /// + public static string GetAppendedQueryString(string url, string key, string value) + { + if (url.Contains("?")) + { + url = string.Format("{0}&{1}={2}", url, key, value); + } + else + { + url = string.Format("{0}?{1}={2}", url, key, value); + } + + return url; + } + + public static string RemoveParameter(string url, string key) + { + + url = url.ToLower(); + key = key.ToLower(); + if (!url.Contains(key + "=")) return url; + + Uri uri = new Uri(url); + NameValueCollection collection = HttpUtility.ParseQueryString(uri.Query); + if (collection.Count == 0) return url; + + var val = collection[key]; + string fragmentToRemove = string.Format("{0}={1}",key , val); + + String result = url.ToLower().Replace("&" + fragmentToRemove, string.Empty).Replace("?" + fragmentToRemove, string.Empty); + return result; + } + + /// + /// 根据URL的相对地址获取决定路径 + /// eg: /Home/About ==>http://192.168.0.1/Home/About + /// + /// System.String. + public static string GetAbsolutePathForRelativePath(string relativePath) + { + HttpRequest Request = HttpContext.Current.Request; + string returnUrl = string.Format("{0}{1}",Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty) , VirtualPathUtility.ToAbsolute(relativePath)); + return returnUrl; + } + } +} \ No newline at end of file diff --git a/OpenAuth.App/packages.config b/OpenAuth.App/packages.config new file mode 100644 index 00000000..4bc06fc1 --- /dev/null +++ b/OpenAuth.App/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file