0

[Microsoft (R) .NET Framework, Version 4.6.81.0]

- - There was an error reflecting type
  'Daff.Lae.TraceCommon.Exceptions.NicePopupException'.
- Cannot serialize member System.Exception.Data of type
  System.Collections.IDictionary, because it implements IDictionary.

From:

namespace Daff.Lae.TraceCommon.Exceptions
{
    [KnownType(typeof(Exception))]
    [XmlSerializerFormat]
    [Serializable]
    public class NicePopupException : Exception
    {
        private NicePopupException() { }

        public NicePopupException(string message) : base(message)
        { }

        public NicePopupException(string message, Exception innerException) : base(message, innerException)
        { }
    }
}

(In desperation) I've tried all variations of [KnownType(typeof(Exception))], [XmlSerializerFormat], [Serializable].

(that is, they are not causing the error).

Seen when:

Visual Studio > Build > Build Daff.Lae.TraceCommon
$ cd to_appropriate_directory
$ xsd.exe .\Daff.Lae.TraceCommon.dll

The problem IDictionary is on Exception as _data.

However if I remove all uses of NicePopupException and change to Exception, there is no such error.

Is there something else I need to add to NicePopupException to get this to work?

HankCa
  • 9,129
  • 8
  • 62
  • 83

1 Answers1

0

I don't have an answer to my question. This is a continuation of my comment but in an answer so I have more space and can format it.

I said in the comment that I have no response and searching (Duck Duck Going!) isn't turning up anything so it would seem we have some unusual situation. Its old code and I'm not going to put any effort into working out the underlying architectural issue.

What I am going to do to get a solution (to wanting to generate a schema for a subset of all the C# classes) is to attempt to generate a schema for each file individually. If any fail (as they do - the Exceptions) then it won't impact the (attempted) generation for every other class.

NOTE that this solution would be mitigated if Window's xsd.exe could take -Include <files> or -Exclude <files> arguments.

Something like this pseudo-script:

for i in find . -name "*cs"
do
    className = $i | Replace '.cs', ''
    xsd .\Daff.Lae.TraceCommon.dll /t:$className 
    mv schema0.xsd $className .xsd
done

As a good diligent SO'er (what we call "a good little vegemite" in Australia :), here is the full solution I wrote in Powershell.

# Powershell script for Document Generation
#
# This generates an XML schema per c# class and combines them in to a unified Trace.xsd file
# We use this in Adobe AEM.  See JIRA:TLAE-38.

# To run this:
# 1. Generate the $DLL - In Visual Studio open a file in Daff.Lae.TraceCommon
# 2. Run Build (menu) > Build Daff.Lae.TraceCommon
#    The file built is $DLL listed below
# 3. Open a powershell in this directory
# 4. .\xsdSchemaAllClasses.ps1 to run this
#    The output will be the $TRACEOUTFILE 
#
# Requirements:
#   1. xsd.exe should be in the $env:Path
# NOTES:
#    This will overwrite $TRACEOUTFILE so make sure that you have copied it elsewhere if you care
# about the previous version and worry that this script may fail.

$OUTDIR="./xsds"
$TRACEOUTFILE = "./trace.xsd"
$TraceCommon="..\..\Src2\Projects\Daff.Lae.TraceCommon"
$DLL = $TraceCommon+"\bin\Debug\Daff.Lae.TraceCommon.dll"
$sourceFilesDir = $TraceCommon+"\ValueObjects"
$encoding = "ASCII"

function setup {
    if ($OUTDIR | Test-Path){
        Remove-Item -Recurse -Force $OUTDIR
    }
    #  | out-null is to prevent the annoying dir listing output
    New-Item -ItemType "directory" $OUTDIR | out-null
    if($TRACEOUTFILE | Test-Path){
        Remove-Item $TRACEOUTFILE
    }
}

function moveFile([string] $inFile, [string] $outFile) {
    if ($inFile | Test-Path) {
        Write-Output "Move file - $inFile to $outFile"
        Move-Item -Path $inFile $outFile
    } else {
        Write-Output "Move file - $inFile to $outFile - INFILE doesnt exist"
    }
}

# Run the SQL Query for each TRACE table and produce clean well-formed and valid XML Schema output
function processFiles {
    $CSFiles = Get-ChildItem -Path $sourceFilesDir  -Recurse -ErrorAction SilentlyContinue *cs #| Select-Object -Property FullName

    $i = 99999; # Debug - Set to max number of files to process
    foreach($item in $CSFiles){
        Write-Output "------------------------------------------------------------------"
        Write-Output "Item: $item"
        $file = Split-Path -Path $item -leaf
        $class = $file.Replace(".cs", "")
        xsd $DLL /t:$class | Out-Null
        $outFile = $OUTDIR+"/"+$class+".xsd"
        moveFile "./schema0.xsd" $outFile
        if (--$i -le 0) {
            break;
        }

    }
}

# We want one Trace XML Schema file made up of all the ones for each table
function combineFiles {
    $SchemaFiles = Get-ChildItem $OUTDIR -Filter *.xsd
    $firstFile = 1
    foreach($sfile in $SchemaFiles) {
        $fname = $sfile | Select-Object -expandproperty name
        $fPath = $OUTDIR+"/"+$fname
        Write-Output "file: $fPath"
        Write-Output "<!-- file: $fPath -->" | Out-File -Append $TRACEOUTFILE -Encoding $encoding
        if ($firstFile) {   # If its the first file
            # Include the XSD header from the first file (and exclude it from all others)
            (Get-Content $fPath |
                    Select-String -Pattern "</xs:schema>" -SimpleMatch -NotMatch |
                    Out-String) |
                    Out-File -Append $TRACEOUTFILE -Encoding $encoding
            $firstFile = 0
        } else {
            Get-Content $fPath |
                Select-String -Pattern "<?xml version" -SimpleMatch -NotMatch |
                Select-String -Pattern "<xs:schema" -SimpleMatch -NotMatch |
                Select-String -Pattern "<xs:import" -SimpleMatch -NotMatch |
                Select-String -Pattern "</xs:schema>" -SimpleMatch -NotMatch |
                Out-File -Append $TRACEOUTFILE -Encoding $encoding
        }
    }
    Write-Output "</xs:schema>" | Out-File -Append $TRACEOUTFILE -Encoding $encoding
}

function main {
    setup
    processFiles
    combineFiles
    Write-Output "------------------------------------------------------------------"
    Write-Output "Output XML Schema file is: $TRACEOUTFILE"
}

main
HankCa
  • 9,129
  • 8
  • 62
  • 83
  • Did this solve IDictionary serialization exception? Do check this: https://stackoverflow.com/questions/8342719/why-cant-a-dictionarykey-value-be-serialized-or-can-it – Prateek Shrivastava Mar 13 '18 at 01:58
  • No it doesn't solve the custom exception problem. IDictionary doesn't seem to be the issue as Exception itself will serialize. This answer is just a workaround. – HankCa Mar 13 '18 at 22:03