azure-activedirectory-identitymodel-extensions-for-dotnet icon indicating copy to clipboard operation
azure-activedirectory-identitymodel-extensions-for-dotnet copied to clipboard

WriteXmlnsAttribute in DelegatingXmlDictionaryWriter throws exception

Open gislikonrad opened this issue 5 years ago • 2 comments

The stack trace

System.ArgumentNullException: Value cannot be null. (Parameter 'localName') at System.Xml.XmlBaseWriter.StartAttribute(String& prefix, String localName, String ns, XmlDictionaryString xNs) at System.Xml.XmlBaseWriter.WriteStartAttribute(String prefix, String localName, String namespaceUri) at System.Xml.XmlDictionaryAsyncCheckWriter.WriteStartAttribute(String prefix, String localName, String ns) at System.Xml.XmlWriter.WriteAttributeString(String prefix, String localName, String ns, String value) at Microsoft.IdentityModel.Xml.DelegatingXmlDictionaryWriter.WriteXmlnsAttribute(String prefix, String namespace)

This happens because the parameters aren't forwarded correctly to the InternalWriter and TracingWriter as seen here.

The correct implementation would be:

        public override void WriteXmlnsAttribute(string prefix, string @namespace)
        {
            UseInnerWriter.WriteXmlnsAttribute(prefix, @namespace);
            TracingWriter?.WriteXmlnsAttribute(prefix, @namespace);
            InternalWriter?.WriteXmlnsAttribute(prefix, @namespace);
        }

or if you want to keep using the WriteAttributeString method:

        public override void WriteXmlnsAttribute(string prefix, string @namespace)
        {
            UseInnerWriter.WriteXmlnsAttribute(prefix, @namespace);
            TracingWriter?.WriteAttributeString("xmlns", prefix, null, @namespace);
            InternalWriter?.WriteAttributeString("xmlns", prefix, null, @namespace);
        }

gislikonrad avatar Sep 24 '20 14:09 gislikonrad

If anybody out there has this error in their code, a workaround would be to call WriteAttributeString("xmlns", prefix, null, @namespace); on their DelegatingXmlDictionaryWriter instead.

gislikonrad avatar Sep 24 '20 14:09 gislikonrad

@gislikonrad thanks can you provide the code sample that exhibits this?

brentschmaltz avatar Sep 29 '20 20:09 brentschmaltz

closing due to inactivity, as we need sample code. Please reopen if we can get a repro and this still happens on the latest 7x version. thank you.

jennyf19 avatar Mar 27 '24 18:03 jennyf19