post

Doxygen

Doxygen is a cross platform open source code documentation tool which I’ve used for both Visual C++ and C# .NET projects. It produces awesome documentation and is very easy to use. Configuration is straight forward using the DoxyWizard application.

Doxygen can be configured to use Graphviz to produce graphs. After installing Graphiz you’ll need to configure it’s path in DoxyWizard.

It supports multiple documentation conventions, the one I use most frequently is the Xml Comments found in .NET, for example:

/// <summary>
/// Creates a Command which will get the last auto-generated Id in a table.
/// Note: Client is responsible for deleting the command.
/// <summary>
/// <param name="table">The name of the database table.</param>
/// <returns>A pointer to a Command.</returns>
/// <exception cref="DataException">Thrown if the table is empty.</exception>
Command * CreateGetLastIdCommand(const std::string & table);

Produces this documentation:

doxygen

For more information on the .NET Xml Comments, see this excellent cheat sheet.

post

Troubleshooting WCF Performance

This is an old post, from a previous website of mine. I was working at RWWA at the time.

A small configuration setting can make a big difference.

I’m currently working on a C# test application which dispatches real units of work through an Oracle Service Bus (OSB) to C++ services on an AIX box. The application uses the Supervisor pattern where a single dispatcher, using xmpp, sends work to multiple workers on different PCs. The multi-threaded workers connect through WCF to the OSB.

Despite the xmpp dispatcher (the single bottleneck) being able to dispatch 200 actions a second (as per SLA), the workers were only dispatching a few a second to the OSB.

After eliminating everything except the simplest thing, I turned to google for help, and discovered that there is a per-appdomain outbound connection limitation default of two for each remote server. It can be adjusted via the system.net/connectionManagement configuration section. This simple change was all that was required:

<configuration>
  <system.net>
    <connectionManagement>
      <add address=”*” maxconnection=”30″/>
    </connectionManagement>
  </system.net>
</configuration>

Read more about WPF Performance on this excellent blog

post

Activator.CreateInstance

One thing I love about using C# is reflection, it adds another paradigm to the toolkit. Recently, I revisted my old friend Activator.CreateInstance which is perfect for scenarios where you want to use generic code to create specific types which take variable parameters in their constructors.

For example, I wrote a simple application which plugs into our build pipeline. It does a few things, such as backup and compress the existing solution, copies generated code over the top of existing code, applies any patches, and then rewrites part of the project file. For various reasons, I decided to use a modified command pattern. Each command implemented the Execute method, but had different constructors which were used to setup any data the command, which would run later, required.

With a CommandManager, using Activator.CreateInstance and leveraging the params object[] feature of C#, I was able to encapsulate actions into isolated commands, yet execute them as a generic set at the desired time.

This is the CommandManager:

namespace PatchServer{
    using System;
    using Interface;
    using System.Collections.Generic;

    public class CommandManager:ICommand{
        #region task

        public class Task{
            #region properties

            public string Description{ get;set; }

            public ICommand Command{ get;set; }

            #endregion
        }

        #endregion

        #region private

        private readonly List<Task> _tasks=new List<Task>();

        #endregion

        #region public methods

        public void Register<T>(string description, params object[] args) where T:ICommand{
            var command=(ICommand)Activator.CreateInstance(typeof(T), args);
            _tasks.Add(new Task(){ Description=description,Command=command });
        }

        public void Execute(){
            foreachvar task in _tasks){
                Console.WriteLine(task.Description);
                task.Command.Execute();
            }
        }

        #endregion
    }
}

And here is how I used it, first create the manager:

var manager=new CommandManager();

Then register some commands, for example:

manager.Register<Archiver>(
    "Backing up previous code.",
    Settings.Default.BackupFolder,
    Settings.Default.ZipName,
    Settings.Default.Folder
);

manager.Register<XCopy>(
    "Patching new code.",
    "/S /Y /R",
    Settings.Default.PatchCodeFolder
    Settings.Default.NewCodeFolder);

And when ready, execute them:

manager.Execute();

Occasionally, I like all exceptions to be thrown through a common class. That way I can add logging, etc. Again, Activator.CreateInstance comes in very handy:

public static void Throw<T>(string format, params object[] args)where T:Exception,new(){
    string message=string.Format(format,args);
    var exception=(T)Activator.CreateInstance(typeof(T),message);

    // do any logging etc. here

    throw exception;
}

Of course, you could make it more generic as in the previous example.

Anyways, this is how I would use it:

AppException.Throw<LoginException>("Username ‘{0}’ is not valid.",username);

There are countless other situations it can be used in, for more information visit:
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

Follow

Get every new post delivered to your Inbox.