Skip to content

更新 .NET 9、10 框架版本,提供 AOT 支持,优化网络请求实现方法#268

Open
lindexi wants to merge 19 commits intoqiniu:masterfrom
LindexiFork:master
Open

更新 .NET 9、10 框架版本,提供 AOT 支持,优化网络请求实现方法#268
lindexi wants to merge 19 commits intoqiniu:masterfrom
LindexiFork:master

Conversation

@lindexi
Copy link
Copy Markdown

@lindexi lindexi commented Mar 3, 2026

注: 本更改大量内容由 GPT 提供

  • 更新 .NET9、.NET 10 框架版本。废弃旧框架支持
  • 提供 AOT 支持
  • 优化网络实现,使用 HttpClient 访问网络
  • 部分模块减少内存分配,尽量少分配内存的方式实现
  • 优化代码写法,如命名规范和封装字段

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request represents a significant modernization effort for the SDK. It upgrades the project to the latest .NET frameworks, enabling AOT compilation for improved performance. The core networking and JSON serialization layers have been overhauled to align with modern .NET best practices, enhancing efficiency and maintainability. Additionally, new development guidelines have been established to ensure consistent code quality moving forward.

Highlights

  • Framework Upgrade and AOT Support: The project's target frameworks have been updated to .NET 9 and .NET 10. Crucially, support for Ahead-of-Time (AOT) compilation has been integrated by migrating JSON serialization to System.Text.Json with source generation, significantly improving startup performance and reducing application size.
  • Network Stack Modernization: The legacy HttpWebRequest API has been entirely replaced with the more modern and efficient HttpClient for all network requests. This refactoring enhances performance, reliability, and maintainability of HTTP communications.
  • JSON Serialization Migration: The entire codebase has transitioned from Newtonsoft.Json to System.Text.Json. This change, coupled with source generators, provides native AOT compatibility and generally faster serialization/deserialization.
  • Code Quality and Maintainability Improvements: Numerous code quality enhancements were implemented, including the addition of nullability annotations, robust argument validation using ArgumentNullException.ThrowIfNull and ArgumentException.ThrowIfNullOrWhiteSpace, and the introduction of const fields for HTTP-related constants. Private fields were also consistently renamed with an underscore prefix.
  • New Development Guidelines: A new copilot-instructions.md file has been added, outlining best practices for future development. These guidelines emphasize the use of HttpClient, System.Text.Json with source generation, early input validation, and C# naming conventions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • .github/copilot-instructions.md
    • Added new file with project modernization guidelines.
  • README.md
    • Removed an empty line for minor formatting improvement.
  • src/Qiniu.sln
    • Updated Visual Studio version to 18.3.11512.155 d18.3.
    • Added copilot-instructions.md and README.md to solution items.
  • src/Qiniu/CDN/BandwidthRequest.cs
    • Migrated JSON attributes from Newtonsoft.Json to System.Text.Json.Serialization.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/CDN/BandwidthResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added nullability annotation to Result property.
  • src/Qiniu/CDN/FluxRequest.cs
    • Migrated JSON attributes from Newtonsoft.Json to System.Text.Json.Serialization.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/CDN/FluxResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added nullability annotation to Result property.
  • src/Qiniu/CDN/LogListRequest.cs
    • Migrated JSON attributes from Newtonsoft.Json to System.Text.Json.Serialization.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/CDN/LogListResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added nullability annotation to Result property.
  • src/Qiniu/CDN/PrefetchRequest.cs
    • Migrated JSON attributes from Newtonsoft.Json to System.Text.Json.Serialization.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/CDN/PrefetchResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added nullability annotation to Result property.
  • src/Qiniu/CDN/RefreshRequest.cs
    • Migrated JSON attributes from Newtonsoft.Json to System.Text.Json.Serialization.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/CDN/RefreshResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added nullability annotation to Result property.
  • src/Qiniu/Http/HttpHelper.cs
    • Introduced const fields for content types and status codes.
    • Marked existing static fields as [Obsolete] and redirected to new const fields.
    • Updated string formatting to use interpolated strings.
    • Added new GetUserAgent() and CreateFormDataBoundary() methods to replace obsolete ones.
  • src/Qiniu/Http/HttpManager.cs
    • Refactored to use HttpClient and HttpRequestMessage instead of HttpWebRequest.
    • Updated CreateHttpResult to process HttpResponseMessage.
    • Modified SendRequest to use HttpClient.Send and handle HttpRequestException.
    • Removed HttpWebRequest specific properties and methods.
    • Updated string formatting to use interpolated strings.
    • Changed private fields to readonly and prefixed with _.
  • src/Qiniu/Http/HttpRequestOptions.cs
    • Rewrote methods to create HttpRequestMessage and HttpClientHandler for HttpClient integration.
    • Removed HttpWebRequest-specific properties and methods.
    • Updated header setting logic for HttpRequestMessage.
  • src/Qiniu/Http/HttpResult.cs
    • Added ArgumentNullException.ThrowIfNull for the Shadow method.
    • Initialized RefText to string.Empty and RefInfo to a new dictionary in the constructor.
    • Changed NotRetryableHttpCodes to a private _notRetryableHttpCodes.
  • src/Qiniu/Qiniu.csproj
    • Updated target frameworks to net9.0;net10.0.
    • Enabled nullability (<Nullable>enable</Nullable>).
    • Added AOT compatibility (<IsAotCompatible>true</IsAotCompatible>).
    • Removed Newtonsoft.Json package reference.
  • src/Qiniu/QiniuCSharpSDK.cs
    • Updated conditional compilation for NETCOREAPP.
  • src/Qiniu/Storage/BatchInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Storage/BatchResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/BucketResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/BucketsResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/DomainsResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/DownloadManager.cs
    • Added ArgumentNullException.ThrowIfNull and ArgumentException.ThrowIfNullOrWhiteSpace for input validation.
    • Updated string formatting to use interpolated strings.
  • src/Qiniu/Storage/FetchInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
  • src/Qiniu/Storage/FetchResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/FileInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Storage/ListInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Storage/ListItem.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Storage/ListResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/PfopInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Storage/PfopResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/PrefopResult.cs
    • Migrated JSON deserialization and serialization to QiniuJson.
  • src/Qiniu/Storage/PutPolicy.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
    • Updated JSON serialization to use QiniuJson.Serialize.
  • src/Qiniu/Storage/ResumableUploader.cs
    • Removed Newtonsoft.Json import.
    • Updated private field names to follow C# naming conventions (_config, _httpManager).
    • Added ArgumentException.ThrowIfNullOrWhiteSpace for input validation.
    • Migrated JSON deserialization to QiniuJson.Deserialize.
  • src/Qiniu/Storage/ResumeContext.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
  • src/Qiniu/Storage/ResumeHelper.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added argument validation and improved exception handling for file operations.
    • Updated string formatting to use interpolated strings.
  • src/Qiniu/Storage/ResumeInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Initialized array properties with empty arrays to prevent null reference exceptions.
  • src/Qiniu/Storage/StatResult.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Updated string formatting to use interpolated strings.
  • src/Qiniu/Storage/ZoneHelper.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added argument validation.
    • Updated private static field names to follow C# naming conventions (_zoneCache, _rwLock).
  • src/Qiniu/Storage/ZoneInfo.cs
    • Migrated JSON attributes to System.Text.Json.Serialization.
    • Added JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) to nullable properties.
  • src/Qiniu/Util/ETag.cs
    • Renamed BLOCK_SIZE and BLOCK_SHA1_SIZE to BlockSize and BlockSha1Size (PascalCase const).
    • Added ArgumentException.ThrowIfNullOrWhiteSpace for CalcHash.
    • Improved exception handling for file access errors.
  • src/Qiniu/Util/QETag.cs
    • Marked calcHash as [Obsolete] and redirected to ETag.CalcHash.
  • src/Qiniu/Util/QiniuJson.cs
    • Added new file to provide System.Text.Json serialization utilities and a JsonSerializerContext for AOT.
  • src/Qiniu/Util/UpToken.cs
    • Migrated JSON deserialization to QiniuJson.Deserialize.
    • Added null checks and argument validation for GetAccessKeyFromUpToken and GetBucketFromUpToken.
  • src/Qiniu/packages.config
    • Removed packages.config file, indicating a move to SDK-style project references.
  • src/QiniuTests/Http/HttpRequestOptions.cs
    • Updated unit tests to reflect the transition from HttpWebRequest to HttpRequestMessage and HttpClientHandler.
  • src/QiniuTests/QiniuTests.csproj
    • Updated target framework to net9.0.
    • Removed old test project configurations and references.
    • Removed packages.config file, indicating a move to SDK-style project references.
Activity
  • The pull request description indicates that a significant portion of these changes were generated with the assistance of GPT, highlighting an AI-driven development approach for modernization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 进行了大规模的现代化改造,包括升级 .NET 框架、迁移到 HttpClientSystem.Text.Json、增加 AOT 支持等,整体方向正确,代码质量有很大提升。大部分修改都遵循了现代 C# 的最佳实践,例如使用 readonly 字段、空值检查、字符串插值等。

我发现了一个关于 HttpClient 使用方式的关键问题,可能导致严重性能和稳定性问题。此外,还有一个关于异常处理的建议。请查看具体的审查评论。

Comment thread src/Qiniu/Http/HttpManager.cs
Comment thread src/Qiniu/Util/ETag.cs Outdated
@lindexi lindexi marked this pull request as draft March 3, 2026 03:04
@lindexi lindexi marked this pull request as ready for review March 3, 2026 07:20
@lindexi lindexi mentioned this pull request Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant