Skip to content

Commit

Permalink
Update all posts
Browse files Browse the repository at this point in the history
  • Loading branch information
hippieZhou committed Sep 23, 2024
1 parent 18cd7bc commit 17867c5
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 106 deletions.
1 change: 1 addition & 0 deletions src/source/_posts/efcore-usage-daily-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: EFCore Usage Daily Notes
date: 2024-06-06 14:22:52
updated: 2024-06-06 14:22:52
tags: EFCore
---

Expand Down
23 changes: 12 additions & 11 deletions src/source/_posts/git-commands-cheat-sheet.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
---
title: Git commands cheat sheet
date: 2022-02-27 20:55:55
updated: 2022-02-27 20:55:55
tags: Git
---


# Handbook

```bash
# Initialize a new git repository:
git init

# Set configuration values for your settings:
# Set configuration values for your settings:
git config --global user.name <your-name>
git config --global user.email <your-email>
git config --global init.defaultBranch main
Expand All @@ -21,10 +22,10 @@ git clone <repository-url>
# Add a file to the staging area:
git add <file>

# Add all files changes to the staging area:
# Add all files changes to the staging area:
git add

# Check the unstaged changes:
# Check the unstaged changes:
git diff

# Commit the staged changes:
Expand All @@ -34,7 +35,7 @@ git commit --amend --no-edit
# Reset staging area to the last commit:
git reset

# Check the state of the working directory and the staging area:
# Check the state of the working directory and the staging area:
git status

# Remove a file from the index and working directory:
Expand All @@ -46,7 +47,7 @@ git log
# Show changelog with graph
git log --graph

# Check the metadata and content changes of the commit:
# Check the metadata and content changes of the commit:
git show <commit-hash>

# Lists all local and remote branches:
Expand All @@ -67,19 +68,19 @@ git checkout <branch-name>
# Create and switch to a new branch
git checkout -b <branch-name>

# Merge specified branch into the current branch:
# Merge specified branch into the current branch:
git merge <branch-name>

# Create a new connection to a remote repository:
# Create a new connection to a remote repository:
git remote add <name> <repository-url>

# Push the committed changes to a remote repository:
# Push the committed changes to a remote repository:
git push <remote> <branch>

# Download the content from a remote repository:
git pull <remote>

# Cleanup unnecessary files and optimize the local repository:
# Cleanup unnecessary files and optimize the local repository:
git gc

# Temporarily remove uncommitted changes and save them for later use:
Expand All @@ -104,4 +105,4 @@ Host github.com
Port 443
```

Then, run the command `ssh -T [email protected]` to confirm if the issue is fixed.
Then, run the command `ssh -T [email protected]` to confirm if the issue is fixed.
3 changes: 3 additions & 0 deletions src/source/_posts/hello-world.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Hello World
date: 2016-07-01 12:43:22
updated: 2016-07-01 12:43:22
tag: Frontend
---

Expand Down Expand Up @@ -70,6 +71,8 @@ More info: [Writing](https://hexo.io/docs/writing.html)
$ hexo s
# or
$ hexo server
# or
hexo clean && hexo g && hexo s
```

More info: [Server](https://hexo.io/docs/server.html)
Expand Down
1 change: 1 addition & 0 deletions src/source/_posts/how-to-enable-ssh-service-on-ubuntu.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: How to enable SSH service on Ubuntu
date: 2021-10-17 12:43:22
updated: 2021-10-17 12:43:22
tags: SSH
---

Expand Down
5 changes: 3 additions & 2 deletions src/source/_posts/how-to-fix-runtimebinderexception.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: How to fix RuntimeBinderException
date: 2023-04-13 20:27:39
updated: 2023-04-13 20:27:39
tags: C#
---

Expand Down Expand Up @@ -41,7 +42,7 @@ catch (Exception e)

# 解决方案

因为匿名类型默认是 Internal 的访问级别。这就意味着如果是同一个程序集中通过Dynamic 类型来访问这个匿名对象是没有问题,但是如果跨程序集就会导致 RuntimeBinder 无法识别这种类型,从而也就引发了 **RuntimeBinderException** 异常。解决这种问题有 2 种方法:
因为匿名类型默认是 Internal 的访问级别。这就意味着如果是同一个程序集中通过 Dynamic 类型来访问这个匿名对象是没有问题,但是如果跨程序集就会导致 RuntimeBinder 无法识别这种类型,从而也就引发了 **RuntimeBinderException** 异常。解决这种问题有 2 种方法:

- 修改返回类型为强类型,取消匿名类型
- 添加 InternalsVisibleTo 属性,让 Internal 级别的对象对外暴露(如下图所示)
Expand All @@ -50,4 +51,4 @@ catch (Exception e)

# 相关参考

- [C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly](https://stackoverflow.com/questions/2630370/c-sharp-dynamic-cannot-access-properties-from-anonymous-types-declared-in-anot)
- [C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly](https://stackoverflow.com/questions/2630370/c-sharp-dynamic-cannot-access-properties-from-anonymous-types-declared-in-anot)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: How to integrate Opentelemetry with Quartz.net in Worker
date: 2024-05-08 13:57:49
updated: 2024-05-08 13:57:49
tags: Opentelemetry
---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
title: How to keep payload process in ASP.NET Core consistent with ASP.NET
date: 2023-07-25 22:15:59
updated: 2023-07-25 22:15:59
tags: ASP.NET Core
---

# 问题背景

最近主要做的一项工作是需要将 XXX Api 中 Engagements 相关的业务接口从 ASP.NET 框架迁移至 ASP.NET Core 中。得益于团队前期已经把基础依赖部分的实现代码都已经迁到到 netstandard2.0,所以 Controller 级别的接口迁移也就不需要很多工作量,基本就是修改一下路由定义和对应的返回值类型就差不多了。再把所有相关的 Controller 都迁移完之后按照团队的测试策略来讲的话,也就可以部署到 QA 环境进行测试。在第一轮的测试过程中,QA人员反馈测试相对比较顺利,没有发现什么明显的 bug。本以为能安心接着修改 Engagements 对应的测试,但是却突然收到了其他团队的反馈,说有一个接口出问题,错误日志如下图所示:
最近主要做的一项工作是需要将 XXX Api 中 Engagements 相关的业务接口从  ASP.NET  框架迁移至  ASP.NET Core 中。得益于团队前期已经把基础依赖部分的实现代码都已经迁到到  netstandard2.0,所以 Controller 级别的接口迁移也就不需要很多工作量,基本就是修改一下路由定义和对应的返回值类型就差不多了。再把所有相关的 Controller 都迁移完之后按照团队的测试策略来讲的话,也就可以部署到 QA 环境进行测试。在第一轮的测试过程中,QA 人员反馈测试相对比较顺利,没有发现什么明显的 bug。本以为能安心接着修改 Engagements 对应的测试,但是却突然收到了其他团队的反馈,说有一个接口出问题,错误日志如下图所示:

![Error](/images/how-to-keep-payload-process-in-asp-net-core-consistent-with-asp-net/error01.png)

Expand All @@ -16,24 +17,24 @@ tags: ASP.NET Core

```C#
# ASP.NET Core
public class XXXServicesController : ControllerBase
{
[HttpPut("fee/xxx/{engagementId}/services")]
public class XXXServicesController : ControllerBase
{
[HttpPut("fee/xxx/{engagementId}/services")]
public IActionResult Save([FromRoute]long engagementId, [FromBody] BatchUpdateEngagementTypeOfServiceRequest request)
{
return Ok(request);
}
{
return Ok(request);
}
}

# ASP.NET
public class EngagementServicesController : ApiController
{
[HttpPut]
[Route("fee/xxx/{engagementId}/services")]
# ASP.NET
public class EngagementServicesController : ApiController
{
[HttpPut]
[Route("fee/xxx/{engagementId}/services")]
public HttpResponseMessage Save(long engagementId, BatchUpdateEngagementTypeOfServiceRequest request)
{
return Request.CreateResponse(request);
}
{
return Request.CreateResponse(request);
}
}
```

Expand All @@ -60,25 +61,25 @@ Payload 参数如下所示:
}
],
"servicesToUpdate":[

],
"idsToBeDeleted":[

]
}
```

发现在 payload 相同的情况下,传到 ASP.NET 中就可以正常解析,但是传到 APS.NET Core 中时就会导致转化 request 对象为 null,通过对比 payload 格式和定义的 DTO 差异,发现 actionRequiredLeadTime 后端实际定义的类型为 int? 类型,但是前端传递的值却是 N/A ,显然是类型不匹配导致的问题。为了让日志里面的错误更加具体,我在 ASP.NET Core 中将序列化异常的回调事件注册上:

```C#
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, errorCtx.Message);
args.ErrorContext.Handled = false;
};
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, errorCtx.Message);
args.ErrorContext.Handled = false;
};
});
```

Expand All @@ -94,7 +95,6 @@ services.AddControllers().AddNewtonsoftJson(options =>

![Controller Execute](/images/how-to-keep-payload-process-in-asp-net-core-consistent-with-asp-net/controllerexecute.png)


通过上图我们可以看到,无论是 ASP.NET Core 还是 ASP.NET ,对于 payload 转后端对象的逻辑都是由 Model Binding 这一层来实现的,所以就需要这里面的不同实现,通过查看源码可以找出有这一段逻辑。

# 解决方案
Expand All @@ -109,7 +109,7 @@ public class BatchUpdateEngagementTypeOfServiceRequestBinder : IModelBinder
{
this.logger = logger;
}

public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
Expand Down Expand Up @@ -170,23 +170,23 @@ public class CustomRequestEntityBinderProvider : IModelBinderProvider
```

```C#
services.AddControllers(options =>
{
options.OutputFormatters.RemoveType<StringOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
options.ModelBinderProviders.Insert(0, new CustomRequestEntityBinderProvider());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, $"can not process request body with incorrect type:{errorCtx.Message}");
args.ErrorContext.Handled = false;
};
});
services.AddControllers(options =>
{
options.OutputFormatters.RemoveType<StringOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
options.ModelBinderProviders.Insert(0, new CustomRequestEntityBinderProvider());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, $"can not process request body with incorrect type:{errorCtx.Message}");
args.ErrorContext.Handled = false;
};
});
```

## 方案二:CustomValueTypeJsonConverter
## 方案二:CustomValueTypeJsonConverter

```C#
# ValueTypeJsonConverter.cs
Expand Down Expand Up @@ -227,22 +227,22 @@ public class ValueTypeJsonConverter : JsonConverter
```

```C#
services.AddControllers(options =>
{
options.OutputFormatters.RemoveType<StringOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new ValueTypeJsonConverter());
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, $"can not process request body with incorrect type:{errorCtx.Message}");
args.ErrorContext.Handled = true;
};
services.AddControllers(options =>
{
options.OutputFormatters.RemoveType<StringOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new ValueTypeJsonConverter());
options.SerializerSettings.Error += (sender, args) =>
{
var errorCtx = args.ErrorContext.Error;
LogManager.GetLogger(nameof(Program)).Log(LogLevel.Error, errorCtx, $"can not process request body with incorrect type:{errorCtx.Message}");
args.ErrorContext.Handled = true;
};
});
```

# 总结

>
>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: How to make ItemsView support IncrementalLoading in WinUI3
date: 2024-09-22 21:40:01
updated: 2024-09-22 21:40:01
tags: WinUI3
---

Expand Down Expand Up @@ -106,7 +107,7 @@ public class IncrementalLoadingBehavior : Behavior<ItemsView>
private void OnViewChanged(object? sender, ScrollViewerViewChangedEventArgs e)
{
if (scrollViewer != null &&
scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight - 100)
scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight - 100)
{
LoadMoreItemsCommand?.Execute(null);
}
Expand Down Expand Up @@ -139,4 +140,4 @@ public class IncrementalLoadingBehavior : Behavior<ItemsView>
</ItemsView.ItemTemplate>
</ItemsView>
</ScrollViewer>
```
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: How to use multiple github accounts on Mac
date: 2024-09-06 09:21:42
updated: 2024-09-06 09:21:42
tags: Git
---

Expand Down
3 changes: 2 additions & 1 deletion src/source/_posts/mef-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: MEF Notes
date: 2022-02-17 21:21:18
updated: 2022-02-17 21:21:18
tags: WPF
---

Expand Down Expand Up @@ -57,4 +58,4 @@ var configuration = new ContainerConfiguration()
.WithAssemblies(assemblies);
using var container = configuration.CreateContainer();
_extWindowsList = container.GetExports<ExportFactory<Window, IDictionary<string, object>>>();
```
```
Loading

0 comments on commit 17867c5

Please sign in to comment.