强制启用CA2007规则,任何层级,只要是通用库代码,await时都应该调用ConfigureAwait(false)。fix: https://github.com/NewLifeX/NewLife.XCode/issues/55。参考https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007

This commit is contained in:
石头 2024-11-30 12:48:31 +08:00
parent 5d64dc81e1
commit b93a820372
20 changed files with 239 additions and 82 deletions

View File

@ -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]
@ -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

View File

@ -103,7 +103,7 @@ public class DbTable : IEnumerable<DbRow>, ICloneable, IAccessor
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>
@ -554,10 +554,8 @@ public class DbTable : IEnumerable<DbRow>, ICloneable, IAccessor
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))

View File

@ -22,7 +22,7 @@ public class DnsHttpHandler : DelegatingHandler
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);
@ -55,6 +55,6 @@ public class DnsHttpHandler : DelegatingHandler
request.RequestUri = builder.Uri;
}
return await base.SendAsync(request, cancellationToken);
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}

View File

@ -342,7 +342,7 @@ public static class HttpHelper
//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>
@ -373,7 +373,7 @@ public static class HttpHelper
//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>
@ -441,7 +441,7 @@ public static class HttpHelper
#endif
}
return await PostAsync(client, requestUri, content, headers, cancellationToken);
return await PostAsync(client, requestUri, content, headers, cancellationToken).ConfigureAwait(false);
}
/// <summary>同步提交表单,名值对传输字典参数</summary>
@ -479,7 +479,7 @@ public static class HttpHelper
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>
@ -527,11 +527,11 @@ public static class HttpHelper
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);
@ -549,7 +549,7 @@ public static class HttpHelper
// 跟踪异常
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;
}
@ -641,7 +641,7 @@ public static class HttpHelper
}
}
return await PostAsync(client, requestUri, content, null, cancellationToken);
return await PostAsync(client, requestUri, content, null, cancellationToken).ConfigureAwait(false);
}
#endregion
@ -660,7 +660,7 @@ public static class HttpHelper
{
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();
@ -668,7 +668,7 @@ public static class HttpHelper
}
else
{
await Task.Delay(100, token);
await Task.Delay(100, token).ConfigureAwait(false);
}
}
}
@ -689,7 +689,7 @@ public static class HttpHelper
/// <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>
@ -705,7 +705,7 @@ public static class HttpHelper
{
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();
@ -715,7 +715,7 @@ public static class HttpHelper
}
else
{
await Task.Delay(100, token);
await Task.Delay(100, token).ConfigureAwait(false);
}
}
}
@ -736,7 +736,7 @@ public static class HttpHelper
/// <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>

View File

@ -28,12 +28,14 @@ public class HttpTraceHandler : DelegatingHandler
// 如果父级已经做了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);

View File

@ -230,7 +230,7 @@ public class TinyHttpClient : DisposeBase
rs = await SendDataAsync(null, null).ConfigureAwait(false);
}
res.Body = await ReadChunkAsync(rs);
res.Body = await ReadChunkAsync(rs).ConfigureAwait(false);
}
// 断开连接
@ -381,7 +381,7 @@ public class TinyHttpClient : DisposeBase
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;

View File

@ -80,7 +80,7 @@ public class TokenHttpFilter : IHttpFilter
var token = Token;
if (token == null || Expire < now)
{
token = await SendAuth(client, cancellationToken);
token = await SendAuth(client, cancellationToken).ConfigureAwait(false);
if (token != null)
{
Token = token;
@ -96,7 +96,7 @@ public class TokenHttpFilter : IHttpFilter
{
try
{
token = await SendRefresh(client, cancellationToken);
token = await SendRefresh(client, cancellationToken).ConfigureAwait(false);
if (token != null)
{
Token = token;
@ -140,7 +140,7 @@ public class TokenHttpFilter : IHttpFilter
username = UserName,
password = pass,
clientId = ClientId,
}, cancellationToken);
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>发起刷新令牌请求</summary>
@ -158,7 +158,7 @@ public class TokenHttpFilter : IHttpFilter
grant_type = "refresh_token",
refresh_token = Token.RefreshToken,
clientId = ClientId,
}, cancellationToken);
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>编码密码在传输中保护安全一般使用RSA加密</summary>

View File

@ -82,7 +82,7 @@ public class EasyClient : IObjectStorage
if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
var client = GetClient();
var rs = await client.PutAsync<ObjectInfo>(BaseAction + $"Put?id={HttpUtility.UrlEncode(id)}", data);
var rs = await client.PutAsync<ObjectInfo>(BaseAction + $"Put?id={HttpUtility.UrlEncode(id)}", data).ConfigureAwait(false);
if (rs == null) return null;
rs.Data ??= data;
@ -99,7 +99,7 @@ public class EasyClient : IObjectStorage
if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
var client = GetClient();
var rs = await client.GetAsync<IPacket>(BaseAction + "Get", new { id });
var rs = await client.GetAsync<IPacket>(BaseAction + "Get", new { id }).ConfigureAwait(false);
if (rs == null) return null;
return new ObjectInfo { Name = id, Data = rs };
@ -113,7 +113,7 @@ public class EasyClient : IObjectStorage
if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
var client = GetClient();
return await client.GetAsync<String>(BaseAction + "GetUrl", new { id });
return await client.GetAsync<String>(BaseAction + "GetUrl", new { id }).ConfigureAwait(false);
}
/// <summary>删除文件对象</summary>
@ -124,7 +124,7 @@ public class EasyClient : IObjectStorage
if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
var client = GetClient();
return await client.DeleteAsync<Int32>(BaseAction + "Delete", new { id });
return await client.DeleteAsync<Int32>(BaseAction + "Delete", new { id }).ConfigureAwait(false);
}
/// <summary>搜索文件</summary>
@ -137,7 +137,7 @@ public class EasyClient : IObjectStorage
//if (searchPattern.IsNullOrEmpty()) throw new ArgumentNullException(nameof(searchPattern));
var client = GetClient();
var rs = await client.GetAsync<IList<ObjectInfo>>(BaseAction + "Search", new { pattern, start, count });
var rs = await client.GetAsync<IList<ObjectInfo>>(BaseAction + "Search", new { pattern, start, count }).ConfigureAwait(false);
return rs?.Cast<IObjectInfo>().ToList();
}
#endregion

View File

@ -209,14 +209,14 @@ public class Host : DisposeBase, IHost
RegisterExit((s, e) => Close(s as String ?? s?.GetType().Name));
await StartAsync(source.Token);
await StartAsync(source.Token).ConfigureAwait(false);
XTrace.WriteLine("Application started. Press Ctrl+C to shut down.");
await _life.Task;
await _life.Task.ConfigureAwait(false);
XTrace.WriteLine("Application is shutting down...");
await StopAsync(source.Token);
await StopAsync(source.Token).ConfigureAwait(false);
XTrace.WriteLine("Stopped!");
}
@ -314,7 +314,7 @@ public abstract class BackgroundService : IHostedService, IDisposable
}
finally
{
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken)).ConfigureAwait(continueOnCapturedContext: false);
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken)).ConfigureAwait(false);
}
}
}

View File

@ -541,7 +541,7 @@ public class TcpSession : SessionBase, ISocketSession
}
}
return await base.ReceiveAsync(cancellationToken);
return await base.ReceiveAsync(cancellationToken).ConfigureAwait(false);
}
internal override Boolean OnReceiveAsync(SocketAsyncEventArgs se)

View File

@ -169,7 +169,7 @@ public class Upgrade
var sw = Stopwatch.StartNew();
var web = CreateClient();
await web.DownloadFileAsync(url, file, cancellationToken);
await web.DownloadFileAsync(url, file, cancellationToken).ConfigureAwait(false);
sw.Stop();
WriteLog("下载完成!大小{0:n0}字节,耗时{1:n0}ms", file.AsFile().Length, sw.ElapsedMilliseconds);

View File

@ -105,7 +105,7 @@ public class WebSocketClient : TcpSession
/// <returns></returns>
public virtual async Task<WebSocketMessage?> ReceiveMessageAsync(CancellationToken cancellationToken = default)
{
using var rs = await base.ReceiveAsync(cancellationToken);
using var rs = await base.ReceiveAsync(cancellationToken).ConfigureAwait(false);
if (rs == null) return null;
var msg = new WebSocketMessage();

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<TargetFrameworks>net45;net461;net462;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net5.0-windows;net6.0-windows;net7.0;net7.0-windows;net8.0;net8.0-windows;net9.0;net9.0-windows</TargetFrameworks>
<TargetFrameworks>net45;net461;net462;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net5.0-windows;net6.0-windows;net7.0;net7.0-windows;net8.0;net8.0-windows;net9.0;net9.0-windows</TargetFrameworks>
<AssemblyName>NewLife.Core</AssemblyName>
<RootNamespace>NewLife</RootNamespace>
<AssemblyTitle>NewLife组件核心库</AssemblyTitle>
@ -20,6 +20,7 @@
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\Doc\newlife.snk</AssemblyOriginatorKeyFile>
<NoWarn>1701;1702;NU5104;NU1505;NETSDK1138;CS7035</NoWarn>
<WarningsAsErrors>CA2007</WarningsAsErrors>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
@ -77,7 +78,7 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.6.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net45'">
<!--<PackageReference Include="System.Net.Http" Version="4.3.4" />-->
@ -225,4 +226,4 @@
</ItemGroup>
</Project>
</Project>

View File

@ -26,7 +26,7 @@ public static class ApiHelper
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public static async Task<TResult?> GetAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => await client.InvokeAsync<TResult>(HttpMethod.Get, action, args, null, "data", cancellationToken);
public static Task<TResult?> GetAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => client.InvokeAsync<TResult>(HttpMethod.Get, action, args, null, "data", cancellationToken);
/// <summary>同步获取参数构造在Url</summary>
/// <typeparam name="TResult">响应类型优先原始字节数据字典返回整体Object返回data没找到data时返回整体字典其它对data反序列化</typeparam>
@ -43,7 +43,7 @@ public static class ApiHelper
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public static async Task<TResult?> PostAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => await client.InvokeAsync<TResult>(HttpMethod.Post, action, args, null, "data", cancellationToken);
public static Task<TResult?> PostAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => client.InvokeAsync<TResult>(HttpMethod.Post, action, args, null, "data", cancellationToken);
/// <summary>同步提交参数Json打包在Body</summary>
/// <typeparam name="TResult">响应类型优先原始字节数据字典返回整体Object返回data没找到data时返回整体字典其它对data反序列化</typeparam>
@ -60,7 +60,7 @@ public static class ApiHelper
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public static async Task<TResult?> PutAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => await client.InvokeAsync<TResult>(HttpMethod.Put, action, args, null, "data", cancellationToken);
public static Task<TResult?> PutAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => client.InvokeAsync<TResult>(HttpMethod.Put, action, args, null, "data", cancellationToken);
/// <summary>异步删除,等待返回结果</summary>
/// <typeparam name="TResult">响应类型优先原始字节数据字典返回整体Object返回data没找到data时返回整体字典其它对data反序列化</typeparam>
@ -69,7 +69,7 @@ public static class ApiHelper
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public static async Task<TResult?> DeleteAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => await client.InvokeAsync<TResult>(HttpMethod.Delete, action, args, null, "data", cancellationToken);
public static Task<TResult?> DeleteAsync<TResult>(this HttpClient client, String action, Object? args = null, CancellationToken cancellationToken = default) => client.InvokeAsync<TResult>(HttpMethod.Delete, action, args, null, "data", cancellationToken);
/// <summary>异步调用,等待返回结果</summary>
/// <typeparam name="TResult">响应类型优先原始字节数据字典返回整体Object返回data没找到data时返回整体字典其它对data反序列化</typeparam>
@ -105,20 +105,20 @@ public static class ApiHelper
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);
return await ProcessResponse<TResult>(response, null, dataName);
return await ProcessResponse<TResult>(response, null, dataName).ConfigureAwait(false);
}
catch (Exception ex)
{
// 跟踪异常
span?.SetError(ex, args);
if (filter != null) await filter.OnError(client, ex, request, cancellationToken);
if (filter != null) await filter.OnError(client, ex, request, cancellationToken).ConfigureAwait(false);
throw;
}
@ -219,7 +219,7 @@ public static class ApiHelper
/// <param name="response">Http响应消息</param>
/// <param name="dataName">数据字段名称默认data。同一套rpc体系不同接口的code/message一致但data可能不同</param>
/// <returns></returns>
public static async Task<TResult?> ProcessResponse<TResult>(HttpResponseMessage response, String dataName = "data") => await ProcessResponse<TResult>(response, null, dataName);
public static Task<TResult?> ProcessResponse<TResult>(HttpResponseMessage response, String dataName = "data") => ProcessResponse<TResult>(response, null, dataName);
/// <summary>处理响应。统一识别code/message</summary>
/// <typeparam name="TResult">响应类型优先原始字节数据字典返回整体Object返回data没找到data时返回整体字典其它对data反序列化</typeparam>

View File

@ -170,7 +170,7 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> GetAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(HttpMethod.Get, action, args);
public Task<TResult?> GetAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(HttpMethod.Get, action, args);
/// <summary>同步获取参数构造在Url</summary>
/// <param name="action">服务操作</param>
@ -182,7 +182,7 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> PostAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(HttpMethod.Post, action, args);
public Task<TResult?> PostAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(HttpMethod.Post, action, args);
/// <summary>同步提交参数Json打包在Body</summary>
/// <param name="action">服务操作</param>
@ -194,27 +194,27 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> PutAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(HttpMethod.Put, action, args);
public Task<TResult?> PutAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(HttpMethod.Put, action, args);
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
/// <summary>异步修改参数Json打包在Body</summary>
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> PatchAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(HttpMethod.Patch, action, args);
public Task<TResult?> PatchAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(HttpMethod.Patch, action, args);
#else
/// <summary>异步修改参数Json打包在Body</summary>
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> PatchAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(new HttpMethod("Patch"), action, args);
public Task<TResult?> PatchAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(new HttpMethod("Patch"), action, args);
#endif
/// <summary>异步删除参数Json打包在Body</summary>
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public async Task<TResult?> DeleteAsync<TResult>(String action, Object? args = null) => await InvokeAsync<TResult>(HttpMethod.Delete, action, args);
public Task<TResult?> DeleteAsync<TResult>(String action, Object? args = null) => InvokeAsync<TResult>(HttpMethod.Delete, action, args);
/// <summary>异步调用,等待返回结果</summary>
/// <typeparam name="TResult"></typeparam>
@ -242,10 +242,10 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
var filter = Filter;
try
{
var msg = await SendAsync(request, cancellationToken);
var msg = await SendAsync(request, cancellationToken).ConfigureAwait(false);
var jsonHost = JsonHost ?? ServiceProvider?.GetService<IJsonHost>() ?? JsonHelper.Default;
return await ApiHelper.ProcessResponse<TResult>(msg, CodeName, DataName, jsonHost);
return await ApiHelper.ProcessResponse<TResult>(msg, CodeName, DataName, jsonHost).ConfigureAwait(false);
}
catch (Exception ex)
{
@ -258,7 +258,7 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
{
if (_currentService != null)
{
if (client != null && filter != null) await filter.OnError(client, ex, this, cancellationToken);
if (client != null && filter != null) await filter.OnError(client, ex, this, cancellationToken).ConfigureAwait(false);
ex.Source = _currentService.Address + "/" + action;
}
@ -268,7 +268,7 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
}
else if (ex is HttpRequestException or TaskCanceledException)
{
if (client != null && filter != null) await filter.OnError(client, ex, this, cancellationToken);
if (client != null && filter != null) await filter.OnError(client, ex, this, cancellationToken).ConfigureAwait(false);
if (++i >= svrs.Count)
{
span?.SetError(ex, null);
@ -290,13 +290,13 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public async Task<TResult?> InvokeAsync<TResult>(String action, Object? args, CancellationToken cancellationToken)
public Task<TResult?> InvokeAsync<TResult>(String action, Object? args, CancellationToken cancellationToken)
{
var method = HttpMethod.Post;
if (args == null || args.GetType().IsBaseType() || action.StartsWithIgnoreCase("Get") || action.ToLower().Contains("/get"))
method = HttpMethod.Get;
return await InvokeAsync<TResult>(method, action, args, null, cancellationToken);
return InvokeAsync<TResult>(method, action, args, null, cancellationToken);
}
/// <summary>同步调用,阻塞等待</summary>
@ -374,7 +374,7 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
service.CreateTime = DateTime.Now;
}
var rs = await SendOnServiceAsync(request, service, client, cancellationToken);
var rs = await SendOnServiceAsync(request, service, client, cancellationToken).ConfigureAwait(false);
// 调用成果,当前服务点可用
Current = service;
@ -497,11 +497,11 @@ public class ApiHttpClient : DisposeBase, IApiClient, IConfigMapping, ILogFeatur
protected virtual async Task<HttpResponseMessage> SendOnServiceAsync(HttpRequestMessage request, Service service, HttpClient client, CancellationToken cancellationToken)
{
var filter = Filter;
if (filter != null) await filter.OnRequest(client, request, this, cancellationToken);
if (filter != null) await filter.OnRequest(client, request, this, cancellationToken).ConfigureAwait(false);
var response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (filter != null) await filter.OnResponse(client, response, this, cancellationToken);
if (filter != null) await filter.OnResponse(client, response, this, cancellationToken).ConfigureAwait(false);
//// 业务层只会返回200 OK
//response.EnsureSuccessStatusCode();

View File

@ -302,7 +302,7 @@ public class TimerScheduler : ILogFeature
}
var func = timer.Method.As<Func<Object?, Task>>(target);
await func!(timer.State);
await func!(timer.State).ConfigureAwait(false);
}
catch (ThreadAbortException) { throw; }
catch (ThreadInterruptedException) { throw; }

View File

@ -77,7 +77,7 @@ public static class PluginHelper
//};
using var client = CreateClient(linkName);
var dir = Path.GetDirectoryName(file);
var file2 = client.DownloadLinkAndExtract(urls, linkName, dir!);
var file2 = client.DownloadLinkAndExtract(urls, linkName, dir!, true);
//client.TryDispose();
}
if (!File.Exists(file))

View File

@ -100,8 +100,6 @@ public class WebClientX : DisposeBase
}
// 发送请求
//var task = content != null ? http.PostAsync(address, content) : http.GetAsync(address);
//var rs = await task;
using var ctx = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(Timeout).Token, cancellationToken);
var rs = await http.SendAsync(request, ctx.Token).ConfigureAwait(false);
@ -170,7 +168,7 @@ public class WebClientX : DisposeBase
{
address = CheckAuth(address);
var rs = await SendAsync(address, null, cancellationToken);
var rs = await SendAsync(address, null, cancellationToken).ConfigureAwait(false);
return await rs.ReadAsStringAsync().ConfigureAwait(false);
}
@ -182,7 +180,7 @@ public class WebClientX : DisposeBase
{
address = CheckAuth(address);
var rs = await SendAsync(address, null, cancellationToken);
var rs = await SendAsync(address, null, cancellationToken).ConfigureAwait(false);
fileName.EnsureDirectory(true);
using var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
await rs.CopyToAsync(fs).ConfigureAwait(false);

View File

@ -108,10 +108,10 @@ public class OssClient : IObjectStorage
var http = GetClient();
var rs = await http.SendAsync(request).ConfigureAwait(false);
return await ApiHelper.ProcessResponse<TResult>(rs);
return await ApiHelper.ProcessResponse<TResult>(rs).ConfigureAwait(false);
}
private async Task<IDictionary<String, Object?>?> GetAsync(String action, Object? args = null) => await InvokeAsync<IDictionary<String, Object?>>(HttpMethod.Get, action, args);
private Task<IDictionary<String, Object?>?> GetAsync(String action, Object? args = null) => InvokeAsync<IDictionary<String, Object?>>(HttpMethod.Get, action, args);
#endregion
#region Bucket操作
@ -121,7 +121,7 @@ public class OssClient : IObjectStorage
{
SetBucket(null);
var rs = await GetAsync("/");
var rs = await GetAsync("/").ConfigureAwait(false);
var bs = rs?["Buckets"] as IDictionary<String, Object>;
var bk = bs?["Bucket"];
@ -141,7 +141,7 @@ public class OssClient : IObjectStorage
{
SetBucket(null);
var rs = await GetAsync("/", new { prefix, marker, maxKeys });
var rs = await GetAsync("/", new { prefix, marker, maxKeys }).ConfigureAwait(false);
var bs = rs?["Buckets"] as IDictionary<String, Object>;
var bk = bs?["Bucket"] as IList<Object>;
@ -164,7 +164,7 @@ public class OssClient : IObjectStorage
{
SetBucket(BucketName);
var rs = await GetAsync("/");
var rs = await GetAsync("/").ConfigureAwait(false);
var contents = rs?["Contents"];
if (contents is IList<Object> list) return list?.Select(e => (e as IDictionary<String, Object?>)!["Key"] + "").ToArray();
@ -182,7 +182,7 @@ public class OssClient : IObjectStorage
{
SetBucket(BucketName);
var rs = await GetAsync("/", new { prefix, marker, maxKeys });
var rs = await GetAsync("/", new { prefix, marker, maxKeys }).ConfigureAwait(false);
var contents = rs?["Contents"];
if (contents is not IList<Object> list) return null;
@ -207,7 +207,7 @@ public class OssClient : IObjectStorage
var content = data.TryGetArray(out var segment) ?
new ByteArrayContent(segment.Array!, segment.Offset, segment.Count) :
new ByteArrayContent(data.ReadBytes());
var rs = await InvokeAsync<IPacket>(HttpMethod.Put, "/" + objectName, content);
var rs = await InvokeAsync<IPacket>(HttpMethod.Put, "/" + objectName, content).ConfigureAwait(false);
return new ObjectInfo { Name = objectName, Data = rs };
}
@ -219,7 +219,7 @@ public class OssClient : IObjectStorage
{
SetBucket(BucketName);
var rs = await InvokeAsync<IPacket>(HttpMethod.Get, "/" + objectName);
var rs = await InvokeAsync<IPacket>(HttpMethod.Get, "/" + objectName).ConfigureAwait(false);
return new ObjectInfo { Name = objectName, Data = rs };
}
@ -236,7 +236,7 @@ public class OssClient : IObjectStorage
{
SetBucket(BucketName);
var rs = await InvokeAsync<Object>(HttpMethod.Delete, "/" + objectName);
var rs = await InvokeAsync<Object>(HttpMethod.Delete, "/" + objectName).ConfigureAwait(false);
return rs != null ? 1 : 0;
}

View File

@ -4,6 +4,7 @@ using NewLife.Log;
using NewLife.Reflection;
using NewLife.Remoting;
using NewLife.Threading;
using NewLife.Web;
namespace Zero.Desktop;
@ -139,6 +140,13 @@ public partial class FrmMain : Form
private void btnDownloadPlugin_Click(object sender, EventArgs e)
{
var file = Setting.Current.PluginPath.CombinePath("System.Data.SQLite.dll");
if (File.Exists(file)) File.Delete(file);
// 下载时内部存在同步调用异步这里不应该卡死UI线程
var linkName = "System.Data.SQLite.win-x64,System.Data.SQLite.win,System.Data.SQLite_net80,System.Data.SQLite_net70,System.Data.SQLite_net60,System.Data.SQLite_net50,System.Data.SQLite_netstandard21,System.Data.SQLite_netstandard20,System.Data.SQLite";
//var type = PluginHelper.LoadPlugin("", "", "System.Data.SQLite", "");
var type = PluginHelper.LoadPlugin("System.Data.SQLite.SQLiteFactory", null, "System.Data.SQLite.dll", linkName);
XTrace.WriteLine("Type={0}", type);
}
}