Skip to content

Commit fd92b9f

Browse files
committed
publish episode 53
1 parent bfb337d commit fd92b9f

File tree

2 files changed

+189
-1
lines changed

2 files changed

+189
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
### 2024
1717

18-
**三月份** : [第 052 期](docs/episode-052.md) :high_brightness: | [第 051 期](docs/episode-051.md)
18+
**四月份** : [第 053 期](docs/episode-053.md) :high_brightness:
19+
20+
**三月份** : [第 052 期](docs/episode-052.md) | [第 051 期](docs/episode-051.md)
1921

2022
**二月份**[第 050 期](docs/episode-050.md)
2123

docs/episode-053.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# .NET 每周分享第 53 期
2+
3+
## 卷首语
4+
5+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/ffb35c45-aaf6-49f5-a9a5-3711daa0f6bf)
6+
7+
`Github Copliot Chat` 现在已经完全支持`Visual Studio`
8+
9+
## 行业咨询
10+
11+
1、[解决方案文件新格式](https://www.youtube.com/watch?v=D0MxmDWk4t0&ab_channel=NickChapsas)
12+
13+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/a015609a-0e83-4af0-9379-147a36d7b535)
14+
15+
`.NET` 项目中工程文件,比如 `csproj` ,其中的内容大家都非常熟悉,但是对于解决方案文件 `sln` 文件,大多数人都望而却步,因为其中的内容太复杂了。在 Visual Studio 新版的 preview 功能,可以选择 `slnx` 的格式,该格式中内容非常简单明了,
16+
比如
17+
18+
```xml
19+
<Solution>
20+
<Project Path="RecordTypes.csproj" />
21+
</Solution>
22+
```
23+
24+
2、[查看 .NET 源码](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollection?view=dotnet-plat-ext-8.0)
25+
26+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/0b9999dc-6045-4276-9ef0-0a1d166b2335)
27+
28+
Microsoft 官方文档增加了一个新功能,当我们查看某一个类的时候,可以点击 `Source` 链接查看源码。
29+
30+
## 文章推荐
31+
32+
1、[ASP.NET Core HttpClient 请求优化](https://www.youtube.com/watch?v=pJDIdIcOH6s&ab_channel=MilanJovanovi%C4%87)
33+
34+
`HttpClient` 是在 `.NET` 应用程序中广泛使用的类,它可以通过 HTTP 协议访问外部资源。其中在 `ASP.NET Core` 中可以更加优雅的方式使用他们。
35+
36+
1. 使用 HttpClient 服务
37+
38+
首先将外部资源封装成一个服务,并且接受一个 `HttpClient` 对象
39+
40+
```csharp
41+
public class GitHubService
42+
{
43+
private readonly HttpClient _httpClient;
44+
public GitHubService(HttpClient httpClient) {
45+
_httpClient = httpClient;
46+
}
47+
public async Task<GithubFile[]?> GetFile(string org, string repo, string folder) {
48+
//...
49+
}
50+
}
51+
```
52+
53+
然后在依赖注入容器中注入该服务,并且配置相应的 `HttpClient` 基本信息
54+
55+
```csharp
56+
builder.Services.AddHttpClient<GitHubService>((sp, httpclient) =>
57+
{
58+
var setting = sp.GetRequiredService<IOptions<GithubSetting>>().Value;
59+
httpclient.BaseAddress = new Uri(setting.BaseUrl);
60+
});
61+
```
62+
63+
2. 使用 Refit
64+
65+
[Refit](https://github.com/reactiveui/refit) 是一个 C# 的开源库,你只需要定义好配置访问资源的接口,该库可以生成相应的实现
66+
67+
```csharp
68+
public interface IGithubApi
69+
{
70+
[Get("/repos/{org}/{repo}/contents/{folder}")]
71+
Task<GithubFile[]?> GetGithubFiles(string org, string repo, string folder);
72+
}
73+
```
74+
75+
这里 `[Get("/repos/{org}/{repo}/contents/{folder}")]` 注解是来自 `Refit` 的定义。然后将 `IGithubApi` 接口注入到容器中
76+
77+
```csharp
78+
builder.Services.AddRefitClient<IGithubApi>()
79+
.ConfigureHttpClient((sp, httpclient) =>
80+
{
81+
var setting = sp.GetRequiredService<IOptions<GithubSetting>>().Value;
82+
httpclient.BaseAddress = new Uri(setting.BaseUrl);
83+
});
84+
```
85+
86+
3. 继承 `DelegatingHandler` 抽象类
87+
88+
如果 `HttpRequest` 还有其他配置信息,比如授权的什么,我们实现一个 `DeletingHandler`
89+
90+
```csharp
91+
public class GithubAuthHandler : DelegatingHandler
92+
{
93+
private readonly GithubSetting _githubSetting;
94+
public GithubAuthHandler(IOptions<GithubSetting> options) {
95+
_githubSetting = options.Value;
96+
}
97+
98+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
99+
request.Headers.Add("Accept", _githubSetting.Accept);
100+
request.Headers.Add("User-Agent", _githubSetting.UserAgent);
101+
return base.SendAsync(request, cancellationToken);
102+
}
103+
}
104+
```
105+
106+
然后在容器中配置该类
107+
108+
```csharp
109+
builder.Services.AddTransient<GithubAuthHandler>();
110+
builder.Services.AddRefitClient<IGithubApi>()
111+
.ConfigureHttpClient((sp, httpclient) =>
112+
{
113+
var setting = sp.GetRequiredService<IOptions<GithubSetting>>().Value;
114+
httpclient.BaseAddress = new Uri(setting.BaseUrl);
115+
}).AddHttpMessageHandler<GithubAuthHandler>();
116+
```
117+
118+
2、[使用 GitHub Action 创建测试报告](https://seankilleen.com/2024/03/beautiful-net-test-reports-using-github-actions/)
119+
120+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/2dc14090-9963-4cfe-a250-9efcf5ebb97e)
121+
122+
`Azure DevOps` 迁移到 `GitHub Action` 的时候,测试报告是丢失的功能,所以这篇文章介绍如何在 `GitHub Action` 中生成`.NET` 应用程序包含测试结果的方法。
123+
124+
3、[.NET Web 开发历史](https://www.irisclasson.com/2024/03/29/the-history-of-.net-web-development-3rd-edition/)
125+
126+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/859e6c1e-1706-4015-987a-fd3f3a99fa65)
127+
128+
这本书介绍了 `.NET` 平台 Web 发展历史。
129+
130+
4、[C# double decimal](https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRjzzgFMBjASwFsBDAGwAIyq76BnCaxgXkc2oBuEgHsIAI3qlmYyaU7c+mOMNwAzEWAAUlKMEaVe/QQcYAeRgUzWTlRIgCUeAN55G75hRoMFjREoA6AiE3DzhZKV9/fiCVPABfPFQCAE4tABIAIhZvJgUQRmcctgV4zIdVZLSs4oZGfMLaji4yiqA=)
131+
132+
`.NET` 中有 `double``decimal` 类型,那么它们的区别是怎样的,这里有一个简单的例子
133+
134+
```csharp
135+
decimal decimalsum = 0m;
136+
double doublesum = 0d;
137+
for(int i = 0; i < 1000; i++)
138+
{
139+
decimalsum += 0.1m;
140+
doublesum += 0.1d;
141+
}
142+
Console.WriteLine($"decimal sum: {decimalsum}");
143+
Console.WriteLine($"decimal sum: {decimalsum}")
144+
```
145+
146+
得到的输出结果如下:
147+
148+
```shell
149+
decimal sum: 100.0
150+
double sum: 99.9999999999986
151+
```
152+
153+
5、[异步高级功能](https://www.youtube.com/watch?v=GQYd6MWKiLI&list=WL&index=1&ab_channel=NDCConferences)
154+
155+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/20ca55f0-f4bd-43fd-8ceb-e1607778c858)
156+
157+
异步 `await` 是 C# 中特色,这个演讲中,作者分享了异步中的高级用法
158+
159+
1. 使用 `await` 而不是 `Wait()` 或者 `Result`
160+
2. 使用 `Fire and Forget` 方式处理无结果任务
161+
3. 避免 `return await` 方法
162+
4. 使用 `ConfigureAwait(false)`
163+
5. 使用 `ConfigureAwait(ConfigureAwaitOptions options)` 方法
164+
6. 使用 `ValueTask`
165+
7. 对于流数据,使用 `IAsyncEnumerable`
166+
8. 使用 `waitAsync(CancellationToken)`
167+
9. 使用 `IAsyncDisposable`
168+
169+
6、[.NET Container 支持](https://devblogs.microsoft.com/dotnet/streamline-container-build-dotnet-8/)
170+
171+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/d0d66b66-737a-4b8b-a875-bbf02567cfe2)
172+
173+
`.NET 8` 是支持 `container` 中最大的一个版本,这篇文章详细介绍了其中的内容,包括
174+
175+
1. 使用 `dotnet publish` 命令发布一个 container 镜像
176+
2. 为不同的 Linux 发行版本创建镜像
177+
3. 创建一个不同语言的镜像
178+
4. ...
179+
180+
## 开源项目
181+
182+
1、[JSON Everthing](https://github.com/gregsdennis/json-everything)
183+
184+
![image](https://github.com/DotNETWeekly-io/DotNetWeekly/assets/11272110/6e68403c-3ba7-405d-90e5-6f3037b34463)
185+
186+
`JSON-Everything``.NET` 下关于 JSON 处理的通用项目,比如 `JSON` schema 的生成和校验, JSON Path 和其他功能。

0 commit comments

Comments
 (0)