Skip to content

Commit

Permalink
强制启用CA2007规则,任何层级,只要是通用库代码,await时都应该调用ConfigureAwait(false)。fix: http…
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 30, 2024
1 parent 5d64dc8 commit b93a820
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 83 deletions.
152 changes: 151 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ dotnet_style_null_propagation = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
tab_width = 4
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_code_quality_unused_parameters = all:suggestion
dotnet_style_readonly_field = true:suggestion
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_allow_multiple_blank_lines_experimental = false:suggestion
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_namespace_match_folder = true:suggestion

# CSharp code style settings:
[*.cs]
Expand Down Expand Up @@ -109,4 +129,134 @@ csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
[*.cs]
#### 命名样式 ####

# 命名规则

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# 符号规范

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# 命名样式

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
csharp_using_directive_placement = outside_namespace:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_space_around_binary_operators = before_and_after
csharp_indent_labels = flush_left
csharp_style_prefer_primary_constructors = true:suggestion
csharp_prefer_system_threading_lock = true:suggestion
csharp_prefer_static_local_function = true:suggestion
csharp_prefer_static_anonymous_function = true:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
csharp_indent_case_contents = true
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false:suggestion
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false:suggestion
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false:suggestion
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false:suggestion
csharp_style_prefer_switch_expression = true:suggestion
csharp_style_prefer_pattern_matching = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent

[*.vb]
#### 命名样式 ####

# 命名规则

dotnet_naming_rule.interface_should_be_以_i_开始.severity = suggestion
dotnet_naming_rule.interface_should_be_以_i_开始.symbols = interface
dotnet_naming_rule.interface_should_be_以_i_开始.style = 以_i_开始

dotnet_naming_rule.类型_should_be_帕斯卡拼写法.severity = suggestion
dotnet_naming_rule.类型_should_be_帕斯卡拼写法.symbols = 类型
dotnet_naming_rule.类型_should_be_帕斯卡拼写法.style = 帕斯卡拼写法

dotnet_naming_rule.非字段成员_should_be_帕斯卡拼写法.severity = suggestion
dotnet_naming_rule.非字段成员_should_be_帕斯卡拼写法.symbols = 非字段成员
dotnet_naming_rule.非字段成员_should_be_帕斯卡拼写法.style = 帕斯卡拼写法

# 符号规范

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, friend, private, protected, protected_friend, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.类型.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.类型.applicable_accessibilities = public, friend, private, protected, protected_friend, private_protected
dotnet_naming_symbols.类型.required_modifiers =

dotnet_naming_symbols.非字段成员.applicable_kinds = property, event, method
dotnet_naming_symbols.非字段成员.applicable_accessibilities = public, friend, private, protected, protected_friend, private_protected
dotnet_naming_symbols.非字段成员.required_modifiers =

# 命名样式

dotnet_naming_style.以_i_开始.required_prefix = I
dotnet_naming_style.以_i_开始.required_suffix =
dotnet_naming_style.以_i_开始.word_separator =
dotnet_naming_style.以_i_开始.capitalization = pascal_case

dotnet_naming_style.帕斯卡拼写法.required_prefix =
dotnet_naming_style.帕斯卡拼写法.required_suffix =
dotnet_naming_style.帕斯卡拼写法.word_separator =
dotnet_naming_style.帕斯卡拼写法.capitalization = pascal_case

dotnet_naming_style.帕斯卡拼写法.required_prefix =
dotnet_naming_style.帕斯卡拼写法.required_suffix =
dotnet_naming_style.帕斯卡拼写法.word_separator =
dotnet_naming_style.帕斯卡拼写法.capitalization = pascal_case
4 changes: 1 addition & 3 deletions NewLife.Core/Data/DbTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void ReadData(IDataReader dr, Int32[]? fields = null)
public async Task ReadAsync(DbDataReader dr, CancellationToken cancellationToken = default)
{
ReadHeader(dr);
await ReadDataAsync(dr, null, cancellationToken);
await ReadDataAsync(dr, null, cancellationToken).ConfigureAwait(false);
}

/// <summary>读取数据</summary>
Expand Down Expand Up @@ -554,10 +554,8 @@ public async Task WriteXml(Stream stream)
await writer.WriteStartElementAsync(null, "Table", null).ConfigureAwait(false);
for (var i = 0; i < cs.Length; i++)
{
//await writer.WriteElementStringAsync(null, Columns[i], null, row[i] + "");
await writer.WriteStartElementAsync(null, cs[i], null).ConfigureAwait(false);

//writer.WriteValue(row[i]);
if (ts[i] == typeof(Boolean))
writer.WriteValue(row[i].ToBoolean());
else if (ts[i] == typeof(DateTime))
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Core/Http/DnsHttpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DnsHttpHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var uri = request.RequestUri;
if (uri == null) return await base.SendAsync(request, cancellationToken);
if (uri == null) return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

// 调用自定义DNS解析器
var addrs = Resolver?.Resolve(uri.Host);
Expand Down Expand Up @@ -55,6 +55,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
request.RequestUri = builder.Uri;
}

return await base.SendAsync(request, cancellationToken);
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
28 changes: 14 additions & 14 deletions NewLife.Core/Http/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public static async Task<String> PostJsonAsync(this HttpClient client, String re

//if (headers == null && client.DefaultRequestHeaders.Accept.Count == 0) client.DefaultRequestHeaders.Accept.ParseAdd("application/json");

return await PostAsync(client, requestUri, content, headers, cancellationToken);
return await PostAsync(client, requestUri, content, headers, cancellationToken).ConfigureAwait(false);
}

/// <summary>同步提交Json</summary>
Expand Down Expand Up @@ -373,7 +373,7 @@ public static async Task<String> PostXmlAsync(this HttpClient client, String req
//if (headers == null && client.DefaultRequestHeaders.Accept.Count == 0) client.DefaultRequestHeaders.Accept.ParseAdd("application/xml");
//client.AddHeaders(headers);

return await PostAsync(client, requestUri, content, headers, cancellationToken);
return await PostAsync(client, requestUri, content, headers, cancellationToken).ConfigureAwait(false);
}

/// <summary>同步提交Xml</summary>
Expand Down Expand Up @@ -441,7 +441,7 @@ public static async Task<String> PostFormAsync(this HttpClient client, String re
#endif
}

return await PostAsync(client, requestUri, content, headers, cancellationToken);
return await PostAsync(client, requestUri, content, headers, cancellationToken).ConfigureAwait(false);
}

/// <summary>同步提交表单,名值对传输字典参数</summary>
Expand Down Expand Up @@ -479,7 +479,7 @@ public static async Task<String> PostMultipartFormAsync(this HttpClient client,
content.Add(new StringContent(item.Value.ToJson()), item.Key);
}

return await PostAsync(client, requestUri, content, null, cancellationToken);
return await PostAsync(client, requestUri, content, null, cancellationToken).ConfigureAwait(false);
}

/// <summary>同步获取字符串</summary>
Expand Down Expand Up @@ -527,11 +527,11 @@ private static async Task<String> PostAsync(HttpClient client, String requestUri
var filter = Filter;
try
{
if (filter != null) await filter.OnRequest(client, request, null, cancellationToken);
if (filter != null) await filter.OnRequest(client, request, null, cancellationToken).ConfigureAwait(false);

var response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (filter != null) await filter.OnResponse(client, response, request, cancellationToken);
if (filter != null) await filter.OnResponse(client, response, request, cancellationToken).ConfigureAwait(false);

#if NET5_0_OR_GREATER
var result = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -549,7 +549,7 @@ private static async Task<String> PostAsync(HttpClient client, String requestUri
// 跟踪异常
span?.SetError(ex, null);

if (filter != null) await filter.OnError(client, ex, request, cancellationToken);
if (filter != null) await filter.OnError(client, ex, request, cancellationToken).ConfigureAwait(false);

throw;
}
Expand Down Expand Up @@ -641,7 +641,7 @@ public static async Task<String> UploadFileAsync(this HttpClient client, String
}
}

return await PostAsync(client, requestUri, content, null, cancellationToken);
return await PostAsync(client, requestUri, content, null, cancellationToken).ConfigureAwait(false);
}
#endregion

Expand All @@ -660,15 +660,15 @@ public static async Task ConsumeAndPushAsync(this WebSocket socket, IProducerCon
{
while (!token.IsCancellationRequested && socket.Connected)
{
var msg = await queue.TakeOneAsync(30, token);
var msg = await queue.TakeOneAsync(30, token).ConfigureAwait(false);
if (msg != null)
{
var buf = onProcess != null ? onProcess(msg) : msg.GetBytes();
socket.Send(buf, WebSocketMessageType.Text);
}
else
{
await Task.Delay(100, token);
await Task.Delay(100, token).ConfigureAwait(false);
}
}
}
Expand All @@ -689,7 +689,7 @@ public static async Task ConsumeAndPushAsync(this WebSocket socket, IProducerCon
/// <param name="topic">主题</param>
/// <param name="source">取消通知源</param>
/// <returns></returns>
public static async Task ConsumeAndPushAsync(this WebSocket socket, ICache host, String topic, CancellationTokenSource source) => await ConsumeAndPushAsync(socket, host.GetQueue<String>(topic), null, source);
public static Task ConsumeAndPushAsync(this WebSocket socket, ICache host, String topic, CancellationTokenSource source) => ConsumeAndPushAsync(socket, host.GetQueue<String>(topic), null, source);

/// <summary>从队列消费消息并推送到WebSocket客户端</summary>
/// <param name="socket">WebSocket实例</param>
Expand All @@ -705,7 +705,7 @@ public static async Task ConsumeAndPushAsync(this System.Net.WebSockets.WebSocke
{
while (!token.IsCancellationRequested && socket.State == System.Net.WebSockets.WebSocketState.Open)
{
var msg = await queue.TakeOneAsync(30, token);
var msg = await queue.TakeOneAsync(30, token).ConfigureAwait(false);
if (msg != null)
{
var buf = onProcess != null ? onProcess(msg) : msg.GetBytes();
Expand All @@ -715,7 +715,7 @@ public static async Task ConsumeAndPushAsync(this System.Net.WebSockets.WebSocke
}
else
{
await Task.Delay(100, token);
await Task.Delay(100, token).ConfigureAwait(false);
}
}
}
Expand All @@ -736,7 +736,7 @@ public static async Task ConsumeAndPushAsync(this System.Net.WebSockets.WebSocke
/// <param name="topic">主题</param>
/// <param name="source">取消通知源</param>
/// <returns></returns>
public static async Task ConsumeAndPushAsync(this System.Net.WebSockets.WebSocket socket, ICache host, String topic, CancellationTokenSource source) => await ConsumeAndPushAsync(socket, host.GetQueue<String>(topic), null, source);
public static Task ConsumeAndPushAsync(this System.Net.WebSockets.WebSocket socket, ICache host, String topic, CancellationTokenSource source) => ConsumeAndPushAsync(socket, host.GetQueue<String>(topic), null, source);

/// <summary>阻塞等待WebSocket关闭</summary>
/// <param name="socket">WebSocket实例</param>
Expand Down
6 changes: 4 additions & 2 deletions NewLife.Core/Http/HttpTraceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

// 如果父级已经做了ApiHelper.Invoke埋点,这里不需要再做一次
var parent = DefaultSpan.Current;
if (parent != null && parent.Tag == uri + "" || request.Headers.Contains("traceparent")) return await base.SendAsync(request, cancellationToken);
if (parent != null && parent.Tag == uri + "" || request.Headers.Contains("traceparent"))
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

using var span = Tracer?.NewSpan(request);
try
{
var response = await base.SendAsync(request, cancellationToken);
// 任何层级,只要是通用库代码,await时都应该调用ConfigureAwait(false)
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

span?.AppendTag(response);

Expand Down
4 changes: 2 additions & 2 deletions NewLife.Core/Http/TinyHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected virtual async Task<IOwnerPacket> SendDataAsync(Uri? uri, IPacket? requ
rs = await SendDataAsync(null, null).ConfigureAwait(false);
}

res.Body = await ReadChunkAsync(rs);
res.Body = await ReadChunkAsync(rs).ConfigureAwait(false);
}

// 断开连接
Expand Down Expand Up @@ -381,7 +381,7 @@ private Boolean ParseChunk(Span<Byte> data, out Int32 offset, out Int32 octets)
var baseAddress = BaseAddress ?? throw new ArgumentNullException(nameof(BaseAddress));
var request = BuildRequest(baseAddress, method, action, args);

using var rs = await SendAsync(request);
using var rs = await SendAsync(request).ConfigureAwait(false);

if (rs == null || rs.Body == null || rs.Body.Length == 0) return default;

Expand Down
Loading

0 comments on commit b93a820

Please sign in to comment.