DocumentBuilder maintains private static collections that BuildDocument mutates.
One such collection is NamespaceAttributes which can cause the API to fail when used concurrently, with exceptions like:
System.InvalidOperationException: Duplicate attribute.
at System.Xml.Linq.XElement.AddAttributeSkipNotify(XAttribute a)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at OpenXmlPowerTools.DocumentBuilder.BuildDocument(List`1 sources, WordprocessingDocument output)
For now I've worked around this by creating a wrapper that synchronizes access to it but the contention introduced is not ideal.
Easiest solution I can see would be to make a non-static DocumentBuilder whilst leaving the current public API static as is (as not to break existing code). The public API would operate on a new instance so data is never shared between thread contexts.
If you'd welcome a pull request let me know and I'll see what I can do.
Closing all issues as this repo is being archived and will no longer be maintained by Microsoft. The project is licensed for continued use and development by forking to your own repo.
DocumentBuilder
maintains private static collections thatBuildDocument
mutates. One such collection isNamespaceAttributes
which can cause the API to fail when used concurrently, with exceptions like:For now I've worked around this by creating a wrapper that synchronizes access to it but the contention introduced is not ideal.
Easiest solution I can see would be to make a non-static
DocumentBuilder
whilst leaving the current public API static as is (as not to break existing code). The public API would operate on a new instance so data is never shared between thread contexts.If you'd welcome a pull request let me know and I'll see what I can do.