How do I use multiple .Net generated libraries from a single client

Accessing multiple .Net generated libraries from a single client can cause issues with the use of the static Default XmlSerializationContext and other static data.


When you run the application you get an Exception as follows:

System.TypeInitializationException was unhandled
Message=The type initializer for 'xxxxxxxx.Registration' threw an exception.
InnerException: System.ArgumentException
Message=An item with the same key has already been added.


This issue is by design as it highlights namespace clashes which may be a problem when using the libraries.


There are a number of options to work around this issue.

Option 1: (Best Option)
The simplest fix for this is to generate only 1 library by creating a super schema which imports your other schema:

Option 2:
Remove usage of the XmlSerializationContext.Default as this is a static class which would be used across all libraries.

To do this, open file Enumerations.cs, and comment out all lines within the ##HAND_CODED_BLOCK that add NamespaceAliases etc. to the XmlSerializationContext.Default and instead create a library specific XmlSerializationContext.

e.g. Create your own static

static public LiquidTechnologies.Runtime.Net40.XmlSerializationContext myLibraryAContext = new LiquidTechnologies.Runtime.Net40.XmlSerializationContext();
myLibraryAContext.A.NamespaceAliases.Add("xs", "");
myLibraryAContext.A.NamespaceAliases.Add("...", "..."); // add your own namespaces

Now anytime you call ToXML... and FromXml... methods within your code, you need to also pass myLibraryAContext as a parameter.

You would replicate this for library B, C, D.

Option 3:
Depending on your schemas, you may be able to get away with just changing the way the XmlSerializationContext .Default namespace items are added.

To do this, open file Enumerations.cs and change all lines within the ##HAND_CODED_BLOCK such as

myLibraryAContext.A.NamespaceAliases.Add("xs", "");


myLibraryAContext.A.NamespaceAliases["xs"] = "";

However, this is not recommended as you may get duplicate namespace and other clashes with static data.