mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-21 00:56:28 +08:00
52df519e50
# Description `AddClient(configurationFile)` (#4328) loads the supplied configuration XML lazily on the first DI session connect, and the `ClientSuppliedConfigurationProvider` behind it was reachable only through internals. Hosted clients whose user interface needs the `ApplicationConfiguration` **before any session exists** therefore had no public way to trigger the load or to read the configuration back — the concrete blocker for moving the 14 WinForms sample clients and the GDS client off the classic eager `ApplicationInstance` path (OPCFoundation/UA-.NETStandard-Samples#794 / OPCFoundation/UA-.NETStandard-Samples#808). Two small additions, exactly as designed in #4340: **1. `OpcUaClientOptions.ConfigurationProvider` is now public** (`get`; the setter stays `internal`). It is already typed as the public `IOpcUaApplicationConfigurationProvider`, so consumers get everything they need with **no new types**: `GetAsync(ct)` as an explicit load trigger that does not connect a session, `Configuration` for the loaded document, and `Application` (`IApplicationInstance`) for the applications that manage their own instance certificate. Registering the supplied provider as `IOpcUaApplicationConfigurationProvider` in DI was deliberately *not* done — `OpcUaServerHostedService` binds to the last registered interface when it has no document of its own, so in a combined client+server host a client-only document would silently capture the server. **2. `OpcUaClientOptions.LoadConfigurationOnStart` — an opt-in eager load for hosted applications.** It registers an internal `ClientConfigurationLoaderHostedService` that awaits `GetAsync` during host start, the client-side twin of `ReverseConnectManagerHostedService`. When `host.StartAsync()` returns, the document is loaded and validated, the application-instance certificate is ensured, `Configuration` is published onto the resolved options (exactly as the connect path does), and a load failure fails the host start instead of surfacing on the first connect. It is registered ahead of the reverse-connect hosted service, so the configuration is complete before anything consumes it. The flag applies to every provider source — a document supplied via `ConfigurationFile` / `ConfigurationStream` and a shared `ConfigureApplication(...)` provider alike — and is a no-op for an explicit `Configuration`, which is already complete. It is bindable from `OpcUa:Client:LoadConfigurationOnStart`. Applications without a Generic Host call `GetAsync` on the provider directly instead; both routes share the provider's single-flight load, so the configuration is never loaded twice. A WinForms client then becomes: ```csharp services.AddOpcUa().AddClient("MyClient.Config.xml", o => o.LoadConfigurationOnStart = true); await host.StartAsync(); ApplicationConfiguration configuration = host.Services .GetRequiredService<OpcUaClientOptions>() .ConfigurationProvider!.Configuration; ``` **Reviewer note:** because the flag decides a *service registration*, it must be set inside the `AddClient(...)` callback or bound from the configuration section — setting it on the resolved options afterwards has no effect. This is called out in the XML docs and in `DependencyInjection.md`. **Coverage:** three tests added to `ClientConfigurationFileTests` for the supplied-document path (eager load on start including the certificate bootstrap and `Application` surface, load failure surfacing on start, no hosted service registered without the flag) and a new `ClientEagerConfigurationLoadTests` fixture for the provider-agnostic behavior (shared `ConfigureApplication(...)` provider awaited exactly once, explicit-`Configuration` no-op, host-startup-token cancellation aborting the load, configuration-section binding, and an end-to-end `host.StartAsync()` test). All 331 `ClientBuilder` tests pass (8 new), as do the 185 server hosting tests including the combined client+server fixtures. `Opc.Ua.Client` builds warning-free on all target frameworks; the test project builds clean on net10.0 and net472. **Docs:** new "Client: loading the configuration eagerly" section in `docs/DependencyInjection.md` and a pointer in the `Opc.Ua.Client` NuGet README. With these in place the sample clients can switch in a follow-up to OPCFoundation/UA-.NETStandard-Samples#808. ## Related Issues - Fixes #4340 - Builds on #4328 ## Checklist _Put an `x` in the boxes that apply. You can complete these step by step after opening the PR._ - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>