diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index add2a12b9c865..24fdc57b0b1bd 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -19,6 +19,7 @@ using System; using System.Text.Json.Serialization; +using System.Threading; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.BrowsingContext; @@ -29,19 +30,13 @@ internal BrowsingContext(BiDi bidi, string id) { BiDi = bidi; Id = id; - - _logModule = new Lazy(() => new BrowsingContextLogModule(this, BiDi.Log)); - _networkModule = new Lazy(() => new BrowsingContextNetworkModule(this, BiDi.Network)); - _scriptModule = new Lazy(() => new BrowsingContextScriptModule(this, BiDi.Script)); - _storageModule = new Lazy(() => new BrowsingContextStorageModule(this, BiDi.Storage)); - _inputModule = new Lazy(() => new BrowsingContextInputModule(this, BiDi.InputModule)); } - private readonly Lazy _logModule; - private readonly Lazy _networkModule; - private readonly Lazy _scriptModule; - private readonly Lazy _storageModule; - private readonly Lazy _inputModule; + private BrowsingContextLogModule? _logModule; + private BrowsingContextNetworkModule? _networkModule; + private BrowsingContextScriptModule? _scriptModule; + private BrowsingContextStorageModule? _storageModule; + private BrowsingContextInputModule? _inputModule; internal string Id { get; } @@ -49,19 +44,19 @@ internal BrowsingContext(BiDi bidi, string id) public BiDi BiDi { get; } [JsonIgnore] - public BrowsingContextLogModule Log => _logModule.Value; + public BrowsingContextLogModule Log => _logModule ?? Interlocked.CompareExchange(ref _logModule, new BrowsingContextLogModule(this, BiDi.Log), null) ?? _logModule; [JsonIgnore] - public BrowsingContextNetworkModule Network => _networkModule.Value; + public BrowsingContextNetworkModule Network => _networkModule ?? Interlocked.CompareExchange(ref _networkModule, new BrowsingContextNetworkModule(this, BiDi.Network), null) ?? _networkModule; [JsonIgnore] - public BrowsingContextScriptModule Script => _scriptModule.Value; + public BrowsingContextScriptModule Script => _scriptModule ?? Interlocked.CompareExchange(ref _scriptModule, new BrowsingContextScriptModule(this, BiDi.Script), null) ?? _scriptModule; [JsonIgnore] - public BrowsingContextStorageModule Storage => _storageModule.Value; + public BrowsingContextStorageModule Storage => _storageModule ?? Interlocked.CompareExchange(ref _storageModule, new BrowsingContextStorageModule(this, BiDi.Storage), null) ?? _storageModule; [JsonIgnore] - public BrowsingContextInputModule Input => _inputModule.Value; + public BrowsingContextInputModule Input => _inputModule ?? Interlocked.CompareExchange(ref _inputModule, new BrowsingContextInputModule(this, BiDi.InputModule), null) ?? _inputModule; public Task NavigateAsync(string url, NavigateOptions? options = null) {