diff --git a/WebApiClientCore/Implementations/DefaultApiActionInvoker.cs b/WebApiClientCore/Implementations/DefaultApiActionInvoker.cs index aac509bd..01c9af5f 100644 --- a/WebApiClientCore/Implementations/DefaultApiActionInvoker.cs +++ b/WebApiClientCore/Implementations/DefaultApiActionInvoker.cs @@ -59,9 +59,9 @@ public virtual async Task InvokeAsync(HttpClientContext context, object var requiredUri = context.HttpApiOptions.HttpHost ?? context.HttpClient.BaseAddress; var useDefaultUserAgent = context.HttpApiOptions.UseDefaultUserAgent; using var requestMessage = new HttpApiRequestMessageImpl(requiredUri, useDefaultUserAgent); - var httpContext = new HttpContext(context, requestMessage); - var dataCollection = new DefaultDataCollection(); - var requestContext = new ApiRequestContext(httpContext, this.ActionDescriptor, arguments, dataCollection); + + var httpContext = new HttpContext(context, requestMessage); + var requestContext = new ApiRequestContext(httpContext, this.ActionDescriptor, arguments, new DefaultDataCollection()); return await this.InvokeAsync(requestContext).ConfigureAwait(false); } catch (HttpRequestException) diff --git a/WebApiClientCore/Implementations/DefaultDataCollection.cs b/WebApiClientCore/Implementations/DefaultDataCollection.cs index 6d65b8ad..fba71e1a 100644 --- a/WebApiClientCore/Implementations/DefaultDataCollection.cs +++ b/WebApiClientCore/Implementations/DefaultDataCollection.cs @@ -7,7 +7,7 @@ namespace WebApiClientCore.Implementations { /// - /// 表示延时初始化的数据集合 + /// 表示数据集合 /// [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(DebugView))] @@ -16,12 +16,12 @@ public class DefaultDataCollection : IDataCollection /// /// 数据字典 /// - private readonly Lazy> dictionary = new Lazy>(() => new Dictionary()); + private Dictionary? dictionary; /// /// 获取集合元素的数量 /// - public int Count => this.dictionary.IsValueCreated ? this.dictionary.Value.Count : 0; + public int Count => this.dictionary == null ? 0 : this.dictionary.Count; /// /// 设置值 @@ -30,7 +30,11 @@ public class DefaultDataCollection : IDataCollection /// 值 public void Set(object key, object? value) { - this.dictionary.Value[key] = value; + if (this.dictionary == null) + { + this.dictionary = new Dictionary(); + } + this.dictionary[key] = value; } /// @@ -40,7 +44,7 @@ public void Set(object key, object? value) /// public bool ContainsKey(object key) { - return this.dictionary.IsValueCreated && this.dictionary.Value.ContainsKey(key); + return this.dictionary != null && this.dictionary.ContainsKey(key); } /// @@ -86,14 +90,14 @@ public bool TryGetValue(object key, [MaybeNullWhen(false)] out TValue va /// public bool TryGetValue(object key, out object? value) { - if (this.dictionary.IsValueCreated == false) + if (this.dictionary == null) { value = default; return false; } else { - return this.dictionary.Value.TryGetValue(key, out value); + return this.dictionary.TryGetValue(key, out value); } } @@ -105,14 +109,14 @@ public bool TryGetValue(object key, out object? value) /// public bool TryRemove(object key, out object? value) { - if (this.dictionary.IsValueCreated == false) + if (this.dictionary == null) { value = default; return false; } else { - return this.dictionary.Value.Remove(key, out value); + return this.dictionary.Remove(key, out value); } } @@ -124,7 +128,7 @@ private class DebugView /// /// 查看的对象 /// - private readonly DefaultDataCollection target; + private readonly Dictionary? dictionary; /// /// 查看的内容 @@ -134,9 +138,9 @@ private class DebugView { get { - return this.target.dictionary.IsValueCreated - ? this.target.dictionary.Value.ToArray() - : Array.Empty>(); + return this.dictionary == null + ? Array.Empty>() + : this.dictionary.ToArray(); } } @@ -146,7 +150,7 @@ private class DebugView /// 查看的对象 public DebugView(DefaultDataCollection target) { - this.target = target; + this.dictionary = target.dictionary; } } }