Follow up question to “Serialize namespace as URI rather than assembly/type”


I asked an earlier
about serializing a namespace as a URI rather than as an assembly/type.  In that question, Tim helped me come up with a solution that works and looks like this:

private void Button_Click(object sender, RoutedEventArgs e)
            //Define a xml schema context with the referenced activity library and interface assemblies
            Assembly activityInterfacesAssembly = Assembly.Load(new AssemblyName(“ActivityInterfaces, Version=, Culture=neutral, PublicKeyToken=77e3577bf486b28b”));
            Assembly activityLibraryAssembly = Assembly.Load(new AssemblyName(“ActivityLibrary1, Version=, Culture=neutral, PublicKeyToken=e638fa7f776c63c5”));
            List referencedAssemblies = new List();
            XamlSchemaContext xsc = new XamlSchemaContext((referencedAssemblies));

            //Load XAML into an activity and run it
            StringReader stringReader = new StringReader(xamlTextBox.Text);
            XamlXmlReader xamlXmlReader = new XamlXmlReader(stringReader);
            Activity workflowRootActivity = ActivityXamlServices.Load(xamlXmlReader);

As noted this does work.  When reviewing the code, I’m confused as to how it works.  I define a new XamlSchemaContext xsc, but then I don’t actually explicitly use it.  If I simply remove the custom XamlSchemaContext  code, then things
fall apart at runtime (can’t load the URI based custom namespaces), so I know it
is working.  I tried to make it a bit more explicit by changing one line above to this:

XamlXmlReader xamlXmlReader = new XamlXmlReader(stringReader, xsc);

However, once I made this change, things actually stopped working because the XAML reader didn’t know how to resolve URIs that are for the ‘built in’ activites.  For example, I get an error during ActivityXamlServices.Load that says:

Cannot create unknown type


So, my question is, how did my original code work and why doesn’t the more explicit variety work?



The code that’s making the difference here is Assembly.Load.When XamlSchemaContext tries to resolve a uri namespace, it looks through its assembly list for XmlnsDefinitionAttribute. If it doesn’t find it, it can’t resolve the namespace.By default, XamlSchemaContext looks at every assembly in the AppDomain; that’s why the load works as soon as you call Assembly.LoadHowever, if you pass a set of assemblies into the XamlSchemaContext constructor, it

Hi Dan,Thanks for the explanation!  That makes sense.  So, I modified my runtime code to look like this:        private void Button_Click(object sender, RoutedEventArgs e)

Sure. Here’s roughly what you’ll want to do (not tested or compiled, but should point you in the right direction…)public class RedirectingSchemaContext : XamlSchemaContext