/// <summary> /// 注册程序集 /// </summary> /// <param name="assemblyString">程序集名称的长格式</param> public static void RegisterAssembly(string assemblyString) { LogTimeUtil logTimeUtil = new LogTimeUtil(); Assembly assembly = Assembly.Load(assemblyString); Type[] typeArr = assembly.GetTypes(); string iServiceInterfaceName = typeof(IService).FullName; foreach (Type type in typeArr) { Type typeIService = type.GetInterface(iServiceInterfaceName); if (typeIService != null && !type.IsAbstract) { Type[] interfaceTypeArr = type.GetInterfaces(); object obj = Activator.CreateInstance(type); _dict.GetOrAdd(type, obj); foreach (Type interfaceType in interfaceTypeArr) { if (interfaceType != typeof(IService)) { _dict.GetOrAdd(interfaceType, obj); } } } } logTimeUtil.LogTime("ServiceHelper.RegisterAssembly 注册程序集 " + assemblyString + " 耗时"); }
/// <summary> /// 获取键值对 /// </summary> internal static object GetValue(string key) { try { LogTimeUtil log = new LogTimeUtil(); string keyMd5 = GetMD5(key); string path = _folderPath + "\\" + keyMd5 + ".txt"; lock (_dictLocksForReadFile.GetOrAdd(key, key)) { if (File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { CacheData data = (CacheData)formatter.Deserialize(fs); fs.Close(); if (data.expirationSeconds > 0 && DateTime.Now.Subtract(data.updateTime).TotalSeconds > data.expirationSeconds) { File.Delete(path); return(null); } log.LogTime("FileCacheUtil 读缓存 " + key); return(data.value); } } } return(null); } catch (Exception ex) { LogUtil.Error(ex, "FileCacheUtil读缓存错误"); return(null); } }
/// <summary> /// 保存键值对 /// </summary> /// <param name="key">缓存键</param> /// <param name="value">值</param> /// <param name="expirationSeconds">过期时间(秒),0表示永不过期</param> internal static void SetValue(string key, object value, int expirationSeconds = 0) { try { LogTimeUtil log = new LogTimeUtil(); CacheData data = new CacheData(key, value); data.updateTime = DateTime.Now; data.expirationSeconds = expirationSeconds; if (!Directory.Exists(_folderPath)) { Directory.CreateDirectory(_folderPath); } string keyMd5 = GetMD5(key); string path = _folderPath + "\\" + keyMd5 + ".txt"; lock (_dictLocksForWriteFile.GetOrAdd(key, key)) { using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { fs.SetLength(0); formatter.Serialize(fs, data); fs.Close(); } } log.LogTime("FileCacheUtil 写缓存 " + key); } catch (Exception ex) { LogUtil.Error(ex, "FileCacheUtil写缓存错误"); } }
/// <summary> /// 异步下载文件 /// </summary> /// <param name="ftpPath">ftp路径</param> /// <param name="ftpUserId">用户名</param> /// <param name="ftpPassword">密码</param> /// <param name="relativeFilePath">文件相对路径</param> public static async Task <MemoryStream> DownloadFileAsync(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath) { try { FtpWebRequest request = null; LogTimeUtil log = new LogTimeUtil(); request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/"))); request.Credentials = new NetworkCredential(ftpUserId, ftpPassword); request.Method = "RETR"; FtpWebResponse response = (FtpWebResponse)(await request.GetResponseAsync()); Stream responseStream = response.GetResponseStream(); MemoryStream stream = new MemoryStream(); byte[] bArr = new byte[1024 * 1024]; int size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length); while (size > 0) { stream.Write(bArr, 0, size); size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length); } stream.Seek(0, SeekOrigin.Begin); responseStream.Close(); log.LogTime("FtpUtil.DownloadFileAsync 下载 filePath=" + relativeFilePath); return(stream); } catch (Exception ex) { LogUtil.Error("FtpUtil.DownloadFileAsync 异步下载文件 错误"); throw ex; } }
/// <summary> /// 上传文件 /// </summary> /// <param name="ftpPath">ftp路径</param> /// <param name="ftpUserId">用户名</param> /// <param name="ftpPassword">密码</param> /// <param name="relativeFilePath">文件相对路径</param> /// <param name="bArr">文件内容</param> public static void UploadFile(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath, byte[] bArr) { try { FtpWebRequest request = null; LogTimeUtil log = new LogTimeUtil(); request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/"))); request.Credentials = new NetworkCredential(ftpUserId, ftpPassword); request.Method = "STOR"; Stream postStream = request.GetRequestStream(); int pageSize = 1024 * 1024; int index = 0; while (index < bArr.Length) { if (bArr.Length - index > pageSize) { postStream.Write(bArr, index, pageSize); index += pageSize; } else { postStream.Write(bArr, index, bArr.Length - index); index = index + (bArr.Length - index); } } postStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); string result = sr.ReadToEnd(); responseStream.Close(); log.LogTime("FtpUtil.UploadFile 上传 filePath=" + relativeFilePath); } catch (Exception ex) { LogUtil.Error("FtpUtil.UploadFile 上传文件 错误"); throw ex; } }