Category Archives: C#

C# Unit Test Getting Started with Complete Code

This article is a supplement of the basic introduction of C# Unit Test from MSDN. As you may find, although technically correct, the MSDN article is confusing here and there, probably because it was written by several people with different assumptions. Another defect of the MSDN article is that the code samples are either too much – so it cannot focus on the point, or too little – so it loses its connection with other essential parts of a complete Unit Test solution. That’s why I am writing this article, with complete code sample tested in VS2013 that you can download.

There are three Sections of this article, with three corresponding VS solutions in the code sample: Basic Unit Test, Unit Test with Stub, and Unit Test with Shim. You should follow the order if you are new to this topic.

Basic Unit Test

Please read the MSDN Unit Test Basics article first, EXCEPT the “Additional tools” section. And refer the “UnitTest” sample in my code.

Note that there are only two projects in the VS solution: Accounts project with only one code class CheckingAccount.cs, and AccountsTest project with only one test class CheckingAccountTest.cs. The mention of BankDB and BankDBTest projects, as well as other interfaces or classes in the MSDN article should all be ignored for now.

Now it should be easy to get the point of Unit Test.

Unit Test with Stub

Now go back to read the “Additional tools” section of the MSDN Unit Test Basics article, esp. the last part “Isolating unit test methods with Microsoft Fakes”. Does it make much sense? I would not be suprised if you think it doesn’t. In fact, there are even errors in the code samples in this part: “var stubBankDb = new MyBank.Stubs.StubIBankDb();” is from an older version of Microsoft Fakes (called Microsoft Moles, just in case you are wondering). In VS2013 it should be “var stubBankDb = new MyBank.Fakes.StubIBankDb();”. Now you understand what I was talking about at the beginning of this article.

OK. Now go ahead and read the MSDN Microsoft Fakes article. This one should make much more sense. And refer to the “UnitTestWithStub” sample in my code.

The addition of a new project BankDB represents a different component that introduces other dependencies, like network connection, database connection, certian files that should be available in the client’s computer on runtime, etc. This is where Stubs are supposed to work. Note that the code samples in the MSDN Fakes article are no longer the Bank/Account ones, while I am still keeping the same context in my code sample.

If you try to follow the article by clicking “Add Fakes Assembly”, you will find the following error message. Deleting the reference is not enough, you may need to delete the Fakes\BankDb.fakes file as well.

7-16-2015 3-12-39 PM

Unit Test with Shim 

The Shim sample code should be pretty straight forward. It’s basically highjacking a call to an external reference and return a preset value.

One problem is that VS2013 Shim generation with System reference seems to be not working well with .net Framework 4.5(.x), with a strange error regarding System.Security.Cryptography. Changing the target framework to 4.6 would work. Or, try VS2015.

“-529697949 (0xe06d7363) Microsoft C++ Exception” with Revit 2015+ API

ERROR: “The program Revit.exe has exited with code -529697949 (0xe06d7363) Microsoft C++ Exception.”

I guess while developing with Revit 2015+ API, you hit this error when you are trying to start Revit in VS to debug? You may want to go to VS – Tools – Options – Debugging, simply check “Use Managed Compatibility Mode”. And try again. Good luck!

4-21-2014 3-28-30 PM

4-21-2014 4-58-58 PM


This is not yet another getting started tutorial on Revit API, but rather a concise checklist and minimum development environment that one can test and verify the system is working before diving into more complex code.

0. the prerequisites:

Make sure Revit 2015/2016 and Visual Studio 2012/2013 is installed. Revit 2015 will not work with older Visual Studios.

1. the project:

Create a new “Class Library” project in “Visual C#” with “.NET Framework 4.5”.

2. the references:

Add RevitAPI.dll, RevitAPIUI.dll and System.Windows.Forms as project references.

Change the “Copy Local” property of RevitAPI.dll and RevitAPIUI.dll to false.

3. the class:

Inherit the Autodesk.Revit.UI.IExternalCommand interface.

Add the following class attribute code before the class declaration.


4. the method:

Implement the Execute method of the inherited interface.

public Autodesk.Revit.UI.Result Execute(
    Autodesk.Revit.UI.ExternalCommandData commandData,
    ref string message,
    Autodesk.Revit.DB.ElementSet elements)
    System.Windows.Forms.MessageBox.Show("Hello World");
    return Autodesk.Revit.UI.Result.Succeeded;

5. the assembly:

Press F6 and VS should report “Build succeeded”.

Note the output path of the dll file shown in the Output window.

6. the GUID:

Create a unique ID for your command.

7. the .addin file

Create an empty text file in: C:\Users\yourUserName\AppData\Roaming\Autodesk\Revit\Addins\201X

(or C:\ProgramData\Autodesk\Revit\Addins\201X).

Change the file name to anyNameWillWork.addin.

Open the file and copy or type the following code:

<?xml version="1.0" encoding="utf-8"?>
    <AddIn Type="Command">

8. DONE!

Now start Revit and you should see an “Add-Ins” tab at the end of Revit tabs, in which there is an “External Tools” button with the newly created External Command.

9. (optional) the debugging:

Point the “Start external program” to the installed revit.exe.

Go to Tools – Options – Debugging, check “Use Managed Compatibility Mode”.

Set a break point in your code.

Press F5 in VS and Revit should be started.

Activate your plugin and you break point should be hit.

~~~~~~~~~~END of original post~~~~~~~~~~

~~~~~~~~~~Update 2015.5.15~~~~~~~~~~


A GitHub repository is created, with the complete Test project for Revit 2016 API. The only thing you need to do is edit the Assembly line in the .addin file, and copy the file to your Revit Addins folder. You are welcome to download the code, and let me know if there are any questions and problems. Enjoy!


As mentioned at the beginning of this post, the content in this post is as concise as possible. If you want more information, here is a tutorial from Autodesk Revit API documentation that contains more details. You may also find the other parts of the documentation to be useful when you try to dig dipper.

“Customization could not be loaded” error with VSTO

ERROR: “Customization could not be loaded because the application domain could not be created.”

If you are working on an Excel (or other Office, I guess) VSTO AddIn / PlugIn project using C# and encountered this error, you may want to check that you are using the right platform (x86 vs x64) in your Configuration Manager.

I have x64 Office 2010 installed and saw this error when I am trying to run the x86 plugin today.

3-28-2014 4-17-19 PM

How to Debug/Run Navisworks 2013 API Programs

Of course you can just type in code, click build, go to Windows Explorer, copy your dll files to Plugins folder, start Navisworks and click the buttons on the Add-in tab. But what’s fun with that? Besides, that will not give you useful real-time info for debugging. Here in this blog I present you some simple configurations that can make your API life a little bit easier.

Use Post-build Event

Using post-build event to copy the generated dll file (and pdb file for debugging) to the destination folder in Plugins.

1. Right-click the project, choose Properties.

2. Click the Build Events tab on the left, copy the following to the “Post-build event command line”:

xcopy /Y “$(TargetDir)*.*” “C:\Program Files\Autodesk\Navisworks Manage 2013\Plugins\$(TargetName)\”

The “xcopy” is a Windows command for copying stuff. The “/Y” is a switch that tells the system to overwrite anything in the destination folder without letting the user confirm.

The $(Xyz) are macros that will be replaced by actually values. For example, on my machine it will be replaced as “xcopy /Y “D:\zhangle\BIM\Navisworks\API\myVsProjects\LeZhangPlugin\LeZhangPlugin\bin\Debug\*.*” “C:\Program Files\Autodesk\Navisworks Manage 2013\Plugins\LeZhangPlugin\””. Using the macros are good for code portability and ease to read and understand. If your Navisworks is not installed at the default folder, you need to adjust the destination folder accordingly. If you want to know all the macros, you may want to click the “Edit Post-build …” button, and click the “Macros” button.

Configure Start Action

If you just click the “Start” button in VS just like you are doing for any stand-alone application, VS will show you the following error message. Indeed, what we have created is a “Class Library” project, which generates a dll file. We need somehow trigger Navisworks first and run our program in it.


1. Right-click your project name in the Solution Explorer, and click Properties, which is at the bottom of the pop-up menu.

2. Click Debug tab on the left; in Start Action group, choose “Start external program”. Click the browse button to go to Navisworks installation folder, and choose “Roamer.exe”. (I have no idea about the name, but trust me.) Save and close the project properties page.

3. In the code you want to run and debug, find an interesting statement and click the left side border of the code window as shown below. A red dot should be added as a “break point”, which means when running, the program will stop BEFORE running this statement to give you a chance to look into the status of the program.


4. Click the Start button in VS again. Navisworks should start automatically.

5. Do something that will trigger your break point code. You will notice that your program will stop at the statement, while you can check the program status and use the different debug tools.


Keep these in mind if a strange bug appears

Check which .NET framework you are using. Right-click your project – Properties. Make sure the “Target framework” is selected as .NET Framework 4. Since VS2012 uses .NET 4.5 as the default framework, some strange bugs may be as a result of the compatibility between different .NET framework versions. In one of my VS2012 project using Autodesk.Navisworks.Automation.dll, the assembly can be added into the reference without errors, but when running it throws “FileNotFoundException” on the Automation file. Changing the framework back to 4 then works fines.

NOTE: This is a re-post due to broken links on website transfer. The original link to this page is:​sworks-2013-api-programs/