# Description Fix source-generated configuration types whose explicit namespace references constants emitted by the same generator pass. Roslyn cannot resolve those constants while `[DataType]` attributes are analyzed, so the generated XML codec silently fell back to `urn:memorybuffer` / `urn:testdata` while the shipped configuration uses the model namespace URIs. This change: - adds `MODELGEN021` to fail when an explicitly supplied `[DataType.Namespace]` expression cannot be resolved; - uses compile-time-available model namespace URIs for the MemoryBuffer and TestData configuration types; - restores production-shaped XML fixtures and exercises the `UpdateExtension<T>()` / `ParseExtension<T>()` path; - documents the source-generator namespace requirement and migration guidance. Targeted validation passed: - `TypeGeneratorTests`: 17 tests on `net10.0` and `net48`; - `ConfigurationRoundTripTests`: 4 tests on `net10.0` and `net48`. ## Related Issues - Fixes #4014 ## Checklist - [ ] 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. - [x] I have addressed **all** PR feedback received.
5.3 KiB
Configuration and State Persistence
When to read this: Read this for
ApplicationConfigurationchanges, the removed Data-Contract serializer, Newtonsoft removal fromOpc.Ua.Core, the newParseExtension/UpdateExtensionsignature, and session / browser state persistence.
Configuration
Data Contract Serializer support removed
Because Data Contract serialization is not AOT compliant and does not support trimming, all use of DataContract in the configuration has been removed. Instead, the source generator enables generating IEncodeable implementations using the DataType and DataTypeField attributes which are now consequently used for all configuration. Because the configuration is now IEncodeable the existing encoders and decoders (in particular the new XmlParser which parses Xml and allows out of order fields) compliant with Part 6 can be used to serialize and deserialize all configuration and configuration extensions.
Generated Data types still support DataContract based serialization, however, consider this a deprecated feature.
All configuration DTO classes (ApplicationConfiguration, ServerConfiguration, TraceConfiguration, TransportConfiguration, ServerSecurityPolicy, OAuth2ServerSettings, OAuth2Credential, GlobalDiscoveryServerConfiguration, CertificateGroupConfiguration, BrowserOptions, etc.) migrated from [DataContract]/[DataMember] to source-generated [DataType]/[DataTypeField] attributes and are now partial classes.
ApplicationConfiguration.LoadWithNoValidationusesXmlParser/IEncodeable.Decode(). Existing XML config files should remain loadable.- Browser and session state persistence switched from XML to OPC UA Binary encoding. Old persisted files cannot be loaded — delete and re-save.
SecuredApplicationusesSecuredApplicationEncodinghelpers instead ofDataContractSerializer.
Change code as follows:
- Replace
[DataContract(Namespace = ...)]with[DataType(Namespace = ...)]and[DataMember(...)]with[DataTypeField(...)]on custom configuration subtypes. - If the old namespace expression references a
Namespacesconstant generated from a model file in the same project, replace it with the URI literal or aconst stringfrom ordinary source. Same-run generated constants are unavailable while[DataType]attributes are analyzed and now produceMODELGEN021. - Add the
partialkeyword to any subclass of these configuration types. - Custom configuration extension types must implement
IEncodeable(the[DataType]source generator handles this automatically forpartialclasses). - Code using reflection to inspect
[DataContract]/[DataMember]attributes must switch to[DataType]/[DataTypeField].
Newtonsoft.Json removed from Opc.Ua.Core
Newtonsoft.Json is no longer a dependency of Opc.Ua.Core. Projects relying on its transitive availability must add an explicit reference:
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
ParseExtension/UpdateExtension signature changed
ParseExtension<T>() and UpdateExtension<T>() now require T to implement IEncodeable. New delegate-based overloads were added for custom decoding:
// Generic overload (T must implement IEncodeable)
var config = configuration.ParseExtension<MyConfig>();
// Delegate overload for custom decoding
var config = configuration.ParseExtension<MyConfig>(
new XmlQualifiedName("MyConfig", myNamespace),
decoder => { var c = new MyConfig(); c.Decode(decoder); return c; });
ExtensionObject array helpers changed
ExtensionObject.ToArray(object, Type) and ToList<T>(object) removed. Use extensionObjects.GetStructuresOf<T>() or ExtensionObject.ToArray<T>(ArrayOf<ExtensionObject>).
IJsonEncodeable interface removed
The IJsonEncodeable interface and the entire "Default JSON Encoding" infrastructure have been removed. OPC UA JSON encoding is handled by the JsonEncoder/JsonDecoder classes which do not require per-type encoding node IDs — those classes are unaffected by this change.
Migration steps:
-
Remove
IJsonEncodeablefrom any custom class that implements it:- public class MyType : IEncodeable, IJsonEncodeable + public class MyType : IEncodeable -
Remove the
JsonEncodingIdproperty from those classes:- public ExpandedNodeId JsonEncodingId => ...;
Session and Browser State Persistence
Breaking Change: Persistence switched from DataContractSerializer XML to IEncoder and IDecoder. BrowserState, SessionState, SessionOptions, SubscriptionState, and MonitoredItemState are annotated with [DataType] and use the standard Encode/Decode methods generated by the source generator.
To register the state types with the encodeable factory:
context.Factory.Builder.AddOpcUaClientDataTypes();
The encoding format for session state has changed. Existing persisted session state files cannot be loaded by the new
SessionConfiguration.Create()method. Handle restore failures and re-persist the new session state.
See also
- Related: packages.md, certificates.md, identity.md.
- 2.0 migration index — analyzer quick-start + symptom → sub-doc table.
- Migration Guide — landing page across versions.