Issues with Xsd.exe

0
2212

Recently I’ve been assigned to the feature related to XSD files generation. I thought the task easy and immediately started an investigation. Quickly it turned out that the task is easy but issues with xsd.exe are mysterious and really annoying. I managed to overcome them, and this post is on how did I do this.

Starting with Xsd.exe

On my machine xsd.exe can be found in the folder:

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools

I had to generate XSD files based on classes existing in one of the projects that we’re working on. To speed up, I copied xsd.exe to the directory with project’s build output, opened PowerShell window there, and executed command:

.\xsd.exe .\Module.FileHandling.dll

Surprisingly, an error occurred:

As usual, „Error: There was an error processing…” and, more promising: „Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information„. The last one seemed just like a message related to the nasty exception called ReflectionTypeLoadException that I’ve already had problems with… I knew that sometimes it’s very troublesome to determine which libraries are not loaded and where the problem lies. Fortunately, not this time.

Investigation in PowerShell

First, what I tried was to check errors in the PowerShell. Sometimes, when an exception is thrown there, we can dig to it using $Error[0] command. Small example is shown below: I just divided one by zero and execution of $Error[0] shown information about the exception.

But not this time. $Error variable was empty.

Investigation in Visual Studio

Then I started to search in the Internet and I found that Xsd can be easily run from the C# code. I knew what should I do 🙂

  1. Add new console application project to my test solution.
  2. Reference xsd.exe.
  3. In the Main method attach to the AppDomain.CurrentDomain.FirstChanceException event which „occurs when an exception is thrown in managed code before the runtime searches the call stack for an exception handler in the application domain”. My handler should log the errors.
  4. Run the Xsd tool and wait for results.

The code is listed below:

class Program
    {
        static void Main()
        {
            AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
            {
                var error = eventArgs.Exception.Message;
                if (eventArgs.Exception is ReflectionTypeLoadException typeLoadException)
                {
                    error = typeLoadException.
                        LoaderExceptions.
                        Aggregate(error, (current, loaderException) => current + $"{Environment.NewLine}{loaderException.Message}");
                }

                Console.WriteLine(error);
            };


            var arguments = new string[]
            {
                @"C:\Repos\Module.FileHandling\bin\Debug\Module.FileHandling.dll",
                "/t:collectionCorrectionFile",
                @"/o:collectionCorrectionFile"
            };

            Xsd.Main(arguments);
            Console.ReadKey();
        }
    }

Lines 7-12 contain the code responsible for unpacking incoming exceptions and displaying them in the console. First, we assign exception’s message to error variable, and then we check if the exception is of type ReflectionTypeLoadException. If so, we iterate through its property „LoaderExceptions” which is just a collection of the inner exceptions.  We add their messages to the error string and at the end we print it.

Further lines prepare arguments that are next passed to the Xsd.Main method.

Solution

When I started the program, output clearly shows which libraries are missing:

I can now easily find mentioned missing libraries, place them in the /bin folder and run xsd.exe again. The result was correct this time. XSD file has been created.

Keen reader spots that the schema0.xsd file was created but the last line of output contain another exception message. The strange thing is that the FileNotFoundException is thrown because a library that has never existed is missing!

The fate is kind today though, I quickly found the StackOverflow topic that describes this phenomenon and makes me calmer about it 🙂 I can either not add message from this kind of exception to my error string or just live with this message – which I decided to do 🙂

I hope you’re here because you also experienced this problem and my post helped a bit. Have a nice day 🙂

0 0 votes
Article Rating
Subscribe
Powiadom o
guest
0 komentarzy
najstarszy
najnowszy oceniany
Inline Feedbacks
View all comments