|
| 1 | +# .NET 每周分享第 37 期 |
| 2 | + |
| 3 | +## 卷首语 |
| 4 | + |
| 5 | +VS Code C# 官方插件 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +之前 VS Code 中的 `C#` 支持都是由 `OmniSharp` 提供的。但是官方并没有明确的 `C#` 支持,现在 `C# Dev Kit` 插件弥补了这个缺失。 |
| 10 | +安装完成后,你可与在 `VS code` 中体验 `VS` 开发的感觉: |
| 11 | + |
| 12 | +1. 创建项目 |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +2. 解决方案视图 |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +3. 运行单测 |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +4. Debug |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +注意,这个插件不是开源免费的,商业使用需要购买许可证。 |
| 29 | + |
| 30 | +## 行业资讯 |
| 31 | + |
| 32 | +1、[CLI 版的 Upgrade Assistant](https://devblogs.microsoft.com/dotnet/upgrade-assistant-cli/) |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +`.NET Upgrade Assistant` 是`.NET`应用程序的升级工具,比如从`.NET Framework`迁移到`.NET`, 或者不同的 `.NET`版本升级,之前这个是作为`Visual Studio`的一个插件,现在提供了`CLI` 版本。 |
| 37 | + |
| 38 | +## 文章推荐 |
| 39 | + |
| 40 | +1、[动态构建查询表达式](https://code-maze.com/dynamic-queries-expression-trees-csharp/) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +在 `C#` LINQ 中,除了我们显式地写查询语句,比如 `persons.Where(p => p.FirstName == "feng")` 之外,我们还可以通过 `Experssion` 的方式动态构建查询语句。这样查询就可以在动态运行时执行,达到数据驱动的的目的,比如 |
| 45 | + |
| 46 | +```csharp |
| 47 | +static Expression<Func<Person, bool>> CreateEqualExpression(string propertyName, object value) |
| 48 | +{ |
| 49 | + var param = Expression.Parameter(typeof(Person), "p"); |
| 50 | + |
| 51 | + var member = Expression.Property(param, propertyName); |
| 52 | + |
| 53 | + var constant = Expression.Constant(value); |
| 54 | + |
| 55 | + var body = Expression.Equal(member, constant); |
| 56 | + |
| 57 | + return Expression.Lambda<Func<Person, bool>>(body, param); |
| 58 | +} |
| 59 | + |
| 60 | +var expression = CreateEqualExpression("FirstName", "feng"); |
| 61 | +persion.Where(expression); |
| 62 | +``` |
| 63 | + |
| 64 | +2、[ASP.NET Core 应用程序部署到 Linux Nginx 中](https://code-maze.com/deploy-aspnetcore-linux-nginx/) |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +这篇文章介绍了如何将一个 ASP.NET Core 的应用程序部署到 `Linux` 中,并且使用 `Nginx` 作为反向代理 |
| 69 | + |
| 70 | +3、[ASP.NET Core 健康检查](https://www.youtube.com/watch?v=p2faw9DCSsY&ab_channel=NickChapsas) |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +在 `ASP.NET Core` 应用程序中,我们需要检查服务的健康状态,比如 `database`, 外部依赖服务等等。`ASP.NET Core` 提供了 `HealthCheck` 的组件,可以帮助我们完成这样工作。 |
| 75 | + |
| 76 | +```csharp |
| 77 | +// 实现 |
| 78 | +public class DatabaseHealthCheck : IHealthCheck |
| 79 | +{ |
| 80 | + public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + string connectionString = "Server=localhost,1433;database=TestDb;User Id=SA;Password=xxxxxx"; |
| 85 | + using var connection = new SqlConnection(connectionString); |
| 86 | + connection.Open(); |
| 87 | + var command = connection.CreateCommand(); |
| 88 | + command.CommandText = "Select 1"; |
| 89 | + command.ExecuteScalar(); |
| 90 | + return Task.FromResult(HealthCheckResult.Healthy()); |
| 91 | + } |
| 92 | + catch (Exception e) |
| 93 | + { |
| 94 | + return Task.FromResult(HealthCheckResult.Unhealthy()); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +// 注册服务 |
| 99 | +builder.Services.AddHealthChecks() |
| 100 | + .AddCheck<DatabaseHealthCheck>("database"); |
| 101 | + |
| 102 | +// 定义路由 |
| 103 | +app.MapHealthChecks("/_health", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions |
| 104 | +{ |
| 105 | + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +4、[.NET StringComparer 优化](https://blog.ndepend.com/net-micro-optimization-and-refactoring-trick/) |
| 110 | + |
| 111 | +下面两个 `Dictionary<string, string>` 对象 |
| 112 | + |
| 113 | +```csharp |
| 114 | +var dict1 = new Dictionary<string, string>(); |
| 115 | +var dict2 = new Dictionary<string, string>(StringComparer.Ordinal); |
| 116 | +``` |
| 117 | + |
| 118 | +它们在 `.NET framework` 和 `.NET Core` 中性能表现是不一样的,也就是说,在 `.NET Core` 中 `dict1` 的性能比 `dict2` 好。这是为什么呢?在这个[ Github issue](https://github.com/dotnet/runtime/issues/29714#issuecomment-497537103) 中解释说,在 `dict2` 中使用的是 `EqualityComparer<string>.Default`, 它其实并不是 `StringComparer.Ordinal` 类型,它避免了随机哈希操作直到必要的时候。 |
| 119 | + |
| 120 | +5、[使用连接池优化性能](https://devblogs.microsoft.com/premier-developer/the-art-of-http-connection-pooling-how-to-optimize-your-connections-for-peak-performance/) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +这是微软工程师帮助科技工程从 `on-prem` 向 `cloud` 转型过程中,遇到了并发性的问题,通过连接池 `connection pool` 技术,解决了客户的问题 |
| 125 | + |
| 126 | +```csharp |
| 127 | +builder.setKeepAliveStrategy((response, context) -> { |
| 128 | +String header = response.getFirstHeader("Keep-Alive").getValue(); |
| 129 | +if (header == null) { |
| 130 | +header = response.getFirstHeader("Connection").getValue(); |
| 131 | +} |
| 132 | +if (header != null && header.equalsIgnoreCase("keep-alive")) { |
| 133 | +return 30 * 1000; // keep the connection alive for 30 seconds |
| 134 | +} |
| 135 | +return -1; // close the connection otherwise |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +6、[.NET Native AOT](https://ericsink.com/native_aot/index.html) |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +这是 `.NET Native AOT` 介绍的网站,作者在这里会分享 `Native AOT` 细节。 |
| 144 | + |
| 145 | +## 开源项目 |
| 146 | + |
| 147 | +1、[.NET roadmap 查询](https://themesof.net/) |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +这是一个非常有意思的项目,它收集了 .NET 的运行时库,工具以及其他的基础设施中的 issue,Task 和 Milestone 以方便查询。 |
| 152 | + |
| 153 | +2、[serilog](https://github.com/serilog/serilog) |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +Serilog 是 `.NET` 社区著名地日志库,相对于 `Microsoft.Extensions.Logging` , 它是一个结构化日志输出库。 |
0 commit comments