XFrameBack 1.0.6
dotnet add package XFrameBack --version 1.0.6
NuGet\Install-Package XFrameBack -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XFrameBack" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XFrameBack" Version="1.0.6" />
<PackageReference Include="XFrameBack" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XFrameBack --version 1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XFrameBack, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XFrameBack@1.0.6
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XFrameBack&version=1.0.6
#tool nuget:?package=XFrameBack&version=1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
XFrameBack
.NET Standard 2.0 通用工具库,用于 Web 后端和桌面开发的技术积累。
概述
XFrameBack 是一个面向 .NET 开发的工具类库,提供了涵盖加密解密、网络请求、数据处理、缓存、任务调度等多个领域的基础工具。所有代码以静态类方法为主,方便直接调用,无需复杂的依赖注入配置。
支持的框架
- .NET Standard 2.0+
- .NET Framework 4.6.1+
- .NET Core 2.0+
- .NET 5/6/7/8
依赖包
| 包名 | 版本 | 说明 |
|---|---|---|
| BouncyCastle.NetCore | 2.2.1 | 国密算法支持 |
| Hprose.RPC | 3.0.22 | 远程过程调用 |
| log4net | 3.3.1 | 日志记录 |
| Newtonsoft.Json | 13.0.4 | JSON 序列化 |
| RestSharp | 114.0.0 | REST 客户端 |
| SharpZipLib | 1.4.2 | 压缩解压 |
| System.Drawing.Common | 10.0.8 | 图形处理 |
| System.Text.Encoding.CodePages | 10.0.8 | 编码支持 |
| Ude.NetStandard | 1.2.0 | 编码检测 |
| ZXing.Net | 0.16.11 | 二维码生成 |
目录结构
XFrameBack/
├── Crypto/ # 加密解密模块
│ ├── AES.cs # AES 对称加密
│ ├── MD5.cs # MD5 哈希
│ ├── RSAUtil.cs # RSA 非对称加密
│ ├── XXTEA.cs # XXTEA 轻量加密
│ └── GM/ # 国密算法
│ ├── SM2.cs # SM2 非对称加密
│ ├── SM3.cs # SM3 哈希算法
│ ├── SM4.cs # SM4 对称加密
│ ├── SM9.cs # SM9 标识加密
│ └── ...
├── Helpers/ # 辅助工具类
│ ├── MemoryCache.cs # 内存缓存
│ ├── NamedLock.cs # 命名锁
│ ├── ObjectPool.cs # 对象池
│ ├── ObjectExtensions.cs # 对象扩展方法
│ ├── PriorityQueue.cs # 优先级队列
│ └── ...
├── Net/ # 网络模块
│ ├── HttpHelper.cs # HTTP 请求工具
│ └── HproseHelper.cs # Hprose RPC 客户端
├── Utils/ # 通用工具类
│ ├── JsonHelper.cs # JSON 序列化
│ ├── FileUtil.cs # 文件操作
│ ├── XmlDocumentUtil.cs # XML 处理
│ ├── Compress/ # 压缩解压
│ ├── DataConvert/ # 数据转换
│ ├── HashAlgorithms/ # 哈希算法
│ └── ...
├── Tasks/ # 任务调度模块
│ ├── TaskTimeout.cs # 任务超时控制
│ ├── CacheTask.cs # 缓存任务
│ └── ...
├── XLog/ # 日志模块
│ └── SystemLogger.cs # 系统日志
└── Models/ # 数据模型
└── PagedList.cs # 分页列表
功能模块
1. 加密模块 (Crypto)
提供标准加密算法和国密算法的实现。
标准算法:
// AES 加密
string encrypted = AES.Encrypt("原始内容", "密钥16-32位", "向量IV");
// AES 解密
string decrypted = AES.Decrypt(encrypted, "密钥16-32位", "向量IV");
// MD5 哈希
string hash = MD5.MD5Encrypt("待加密内容");
// RSA 加密/解密
string rsaEncrypted = RSAUtil.RSAEncrypt(content, publicKey);
string rsaDecrypted = RSAUtil.RSADecrypt(rsaEncrypted, privateKey);
国密算法(GM):
// SM2 加密/解密
string sm2Encrypted = SM2Utils.Encrypt(plainText, publicKey);
string sm2Decrypted = SM2Utils.Decrypt(sm2Encrypted, privateKey);
// SM3 哈希
string sm3Hash = SM3.Encrypt(input);
// SM4 加密/解密
string sm4Encrypted = SM4Utils.Encrypt(plainText, key, mode);
2. 网络模块 (Net)
// POST 请求
string postResult = HttpHelper.HttpPost(url, postData, contentType);
// 异步 POST 请求
string asyncPostResult = await HttpHelper.HttpPostAsync(url, postData, contentType);
// GET 请求
string getResult = HttpHelper.HttpGet(url);
3. 工具模块 (Utils)
JSON 处理:
// 序列化
string json = JsonHelper.SerializeObject(obj);
// 反序列化
T result = JsonHelper.JsonToObject<T>(json);
// 列表反序列化
List<T> list = JsonHelper.JsonStringToList<T>(json);
// 扩展方法
string json = obj.ToJson();
T obj = json.ToObject<T>();
文件操作:
// 检测文件是否存在
bool exists = FileUtil.IsExistFile(filePath);
// 检测目录是否存在
bool dirExists = FileUtil.IsExistDirectory(directoryPath);
// 读取文件内容
string content = FileUtil.ReadFile(filePath);
// 写入文件
FileUtil.WriteFile(filePath, content);
压缩解压:
// ZIP 压缩
ZipTool.CompressDirectory(sourceDir, zipFilePath);
// ZIP 解压
ZipTool.ExtractZip(zipFilePath, destDir);
哈希算法:
// 一致性哈希
var hash = new ConsistentHash();
hash.AddNode(node1);
hash.AddNode(node2);
string targetNode = hash.GetNode(key);
4. 缓存模块 (Helpers)
// 设置缓存
MemoryCache<string>.Set("key", "value");
// 获取缓存
string value = MemoryCache<string>.Get("key");
// 获取或创建缓存
string value = MemoryCache<string>.GetOrAdd("key", k => ExpensiveOperation(k));
// 设置过期时间
MemoryCache<string>.Expiration = TimeSpan.FromMinutes(10);
// 清除缓存
MemoryCache<string>.Remove("key");
MemoryCache<string>.Clear();
5. 任务模块 (Tasks)
// 任务超时控制
await TaskTimeout.RunAsync(task, TimeSpan.FromSeconds(30));
// 带超时回调
await TaskTimeout.RunAsync(task, TimeSpan.FromSeconds(30), onTimeout);
6. 数据模型 (Models)
// 分页列表创建(从 IQueryable)
var pagedList = await PagedList<T>.CreateAsyncJson(query, pageIndex, pageSize);
// 分页列表创建(从 IList)
var pagedList = await PagedList<T>.CreateAsync(list, pageIndex, pageSize);
// 分页属性
int totalCount = pagedList.TotalCount;
int totalPages = pagedList.TotalPages;
bool hasNext = pagedList.HasNextPage;
bool hasPrevious = pagedList.HasPreviousPage;
7. 日志模块 (XLog)
// 配置日志
SystemLogger.Configure(configFilePath);
// 记录日志
SystemLogger.Info("信息日志");
SystemLogger.Error("错误日志", exception);
使用示例
Web API 项目
// 在 Controller 中使用
[HttpGet]
public IActionResult GetUsers([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 10)
{
var users = _userRepository.Query();
var pagedList = await PagedList<User>.CreateAsyncJson(users, pageIndex, pageSize);
return Ok(pagedList);
}
国密加密通信
// SM4 加密通信内容
var encrypted = SM4Utils.Encrypt(requestData, sm4Key, SM4CipherMode.CBC);
var response = await HttpHelper.HttpPostAsync(apiUrl, encrypted, "application/json");
数据缓存
// 缓存频繁访问的数据
public UserInfo GetUserInfo(int userId)
{
var cacheKey = $"user_info_{userId}";
return MemoryCache<UserInfo>.GetOrAdd(cacheKey, k => LoadUserFromDb(userId));
}
安装
NuGet 安装
Install-Package XFrameBack
项目引用
将 XFrameBack.csproj 添加到你的解决方案中,配置目标框架为 netstandard2.0 或其他兼容框架。
命名空间
所有类位于 XFrameBack 命名空间下:
XFrameBack.Crypto- 加密模块XFrameBack.Helpers- 辅助工具类XFrameBack.Net- 网络模块XFrameBack.Utils- 通用工具类XFrameBack.Tasks- 任务模块XFrameBack.Log- 日志模块XFrameBack.Models- 数据模型
注意事项
- 线程安全:MemoryCache 等类已实现线程安全,但存储的对象需要自行保证线程安全
- 国密算法:使用国密算法时需注意相关法规要求
- 密钥管理:生产环境中勿硬编码密钥,建议使用环境变量或密钥管理服务
- 超时设置:网络请求建议根据实际需求设置合理超时时间
版本历史
- v1.0.6 - 统一命名空间,解决 XFrame 和 XFrame.Core 不一致问题
- v1.0.5 - 持续完善工具类
- v1.0.4 - 增加国密算法支持
- v1.0.3 - 基础工具类完善
- v1.0.2 - 初期版本
许可证
内部积累,公开使用。
作者
zorrowm
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- BouncyCastle.NetCore (>= 2.2.1)
- Hprose.RPC (>= 3.0.22)
- log4net (>= 3.3.1)
- Newtonsoft.Json (>= 13.0.4)
- RestSharp (>= 114.0.0)
- SharpZipLib (>= 1.4.2)
- System.Drawing.Common (>= 10.0.8)
- System.Text.Encoding.CodePages (>= 10.0.8)
- Ude.NetStandard (>= 1.2.0)
- ZXing.Net (>= 0.16.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
修改命名空间不一致的问题(之前存在XFrame和XFrame.Core)