Debugging code called while NUnit testing.

This post is intended mostly as a reminder for myself.
This is probably a beaten path so I expect a lot of people already know how to do this if they need.

Let’s say you are executing your NUnit tests using the NUnit console.
Something is happening and you want to be able to stop at a breakpoint somewhere in the code that you are testing.
So, while you have your NUnit console executable up and running, in Visual Studio go to Tools\Attach to Process ….
This will open a dialog where you can select the process you want to attach to. This is : “nunit-agent.exe” and after you selected it click Attach. (Leave all the other setting settings to their default values: Transport=Default, Qualifier=<your_computer_name>, Attach to=Automatic)

We will assume the dialog was dismissed cleanly with no errors and you are back to the main VS window. At this point, if you have a breakpoint in your to be tested code, switch to NUnit console and just Run (or Run All) tests. Of course the portion of the tested code with a breakpoint in it has to be called eventually (directly or indirectly) from the testing code. If that is the case, the execution will now stop at that breakpoint allowing you to go step by step, investigate the values of variables and so on.

Hopping this helps,

Unable to access computer B on a windows network from computer A.

\\myNAS is not accessible. You might not have permission to use this network resource. Contact the administrator of this server to find out if you have access permissions.Access is denied

I ran into this nightmare of a problem last Friday. Honestly I don’t know what happened, but all of the sudden, from the workstation I cannot access anymore my Synology NAS. I mean by name or fully qualified domain name. By IP address it was working fine but you don’t want that approach to resolve the problem unless you are desperate.

It took Saturday, Sunday and Monday on and off grinding at the issue to finally realize what the problem was. I have to mention that from other machines I could access the NAS just fine. More, I could access other hosts on my network from my workstation with no problems. This was strictly an issue between my station and my NAS.

After numerous Wireshark traces and struggling to discern which ones are the records in those traces that represent the failed attempts to access my NAS, I started to believe more and more that this record was the one pointing out the problem: tree connect andx response error status_access_denied

A simple search on google takes me to this blog entry. Now even though it is only similar to my problem it made me remember a Kerberos error that I have seen in the Wireshark traces, in a reply from my DC: KRB5 KRB Error: KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN . I should mention that my NAS is part of a domain and it uses AD authentication.

Naturally as the next step, I went automatically searching for the request related to the above reply: KRB5 AS-REQ. So, I’m expanding the NetBIOS section of the request in Wireshark, to see what is the name of the principal that my DC does not recognize. I was happy to see this:  Client Name (Principal): admin Realm: MYDOMAIN. I know for sure I don’t have such a user in MYDOMAIN so where is this coming from and why is it trying to authenticate using this specific account?

Thanks to my memory who’s still in decent shape I remember while analysing the Event Logs, seeing a very isolated warning in the System event log that was happening during the Windows booting procedure. This is the warning’s message:

The password stored in Credential Manager is invalid. This might be caused by the user changing the password from this computer or a different computer. To resolve this error, open Credential Manager in Control Panel, and reenter the password for the credential MYDOMAIN\admin.

This particular error was a confirmation that somewhere, Windows is storing the wrong credentials that are to be used automatically when accesing my NAS.

What do you want more clearer than that? Kudos to whomever was thoghtfull enough to put that error message. I immediatelly opened Control Panel, typed Credential Manager in the Search box. Clicked on Credential Manager link that showed up. It took me two seconds to spot the entry for my NAS’ name in the Windows Credentials section. I deleted the entry, went back to Windows explorer and tried both  \\myNAS and \\myNAS.mydomain.local and they worked!


Using foreach to loop through the elements of an enumeration in .Net (C#)

If you find yourself in need of looping through the elements of an enumeration
and do something for each of them then you may want to take a look at the following code:

enum Elements {
        Manganese,         Chrome,         Gold,         Argon
// we cannot loop through the enumeration but we can through an array of strings
// representing the name of the elements of that enumeration,
// which is what Enum.GetNames() method will gladly give us if we pass the type of the enumeration

foreach (string elementName in Enum.GetNames(typeof(Elements))) {
    // then, if we need to create an instance of the enumeration type based on the
    // current elementName we use Enum.Parse() method which takes:
    //  - the type of the enumeration and
    //  - the elementName (a string);
    //  Enum.Parse() returns <object> so we have to explicitly cast it to our enumeration type

    Elements element = (Elements)Enum.Parse(typeof(Elements), elementName);

so, the above foreach loops four times and in each iteration you have access to an element
of the enumeration as its native type and as a string representation – its name.

‘Application’ is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’

I’m working on a project that involves manipulating xls files. I worked before with the Microsoft.Office.Interop.Excel assembly and after you learn a few tricks it is pretty straightforward. Now this project I’m discussing about was not started by me and I was assigned the task of adding some functionality to it. Long story short I load the project in VS2008. I had to remove and add back some references who couldn’t find their underlying files anymore. I rebuilt the project and voila, the app is up and running. I added it to source control  just in case. I thought that if I get some ideas I’ll work on the project on my home computer having the comfort of my two monitors setup and a normal size keyboard. And that, actually happened.

I’m home and I am loading the same project un-modified in any way, make a couple of changes and compile it. The surprise came immediately as the project didn’t compile, but instead failing with the message that gave this article its title: ‘Application is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’. I new very well why this may happen in a project and because of that I immediately started to search any other references to Application that were not fully qualified and might confuse the compiler. There were a few references to an Application identifier but all fully qualified.

No luck on the net either. Every article out there told me to do the same thing.

Then, I changed "Application” to “_Application” as anyway that is what you should use when programming with PIA (the interface not the class) . The error message at compilation became now: ‘_Application is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’. However, at this point the only reference to the Excel PIA’s _Application was mentioned in one place only in the code and I couldn’t understand why the complier kept telling me that I still have an ambiguous reference in my code. It was clear though that _Application from Microsoft.Office.Interop.Excel was conflicting with _Application from Microsoft.Office.Interop.Excel. I got the 100% confirmation for this when I glanced at the Class view window, where the Microsoft.Office.Interop.Excel namespace was listed twice. How is that possible I asked myself?

I immediately removed the reference to Microsoft.Office.Interop,Excel assembly in Solution Explorer and one of the entries of the same name namespace from the Class View disappeared. One was still there. Where is it reading it from? There’s no reference whatsoever. Could it be from the GAC? It has to be. Anyway, I run a compilation which this time works perfectly. Wait, now I will have to add the reference back every time I’m on my work laptop and remove it when I open the project at home. I don’t want that so I decided to remove the Microsoft.Office.Interop.Excel assembly from the GAC. Just as a test I try to re-compile the project and surely it fails. I manually added the reference to the assembly and successfully compile.

It works and I’m happy because I spent some long time on this and there was a point I really didn’t know what to do. The confusing thing is that all the PIA’s assemblies were added a long time ago to the GAC on my machine. At the same time I worked on at least two other projects where I added a reference to the PIA’s Excel assembly in the project itself and I have never had a conflict.

I’m now asking myself, could it be that this project that I didn’t start myself was configured differently? More precisely, is there a setting saying that you can use the Import statement to import anything from an assembly which is already in the GAC without having to manually add a reference to the project? If anybody knows anything like this, I would appreciate a reply.


Regular expressions – Backreferences

I’m going through the SPTK for the 70-536 and stopped a bit at the regular expressions sub chapter. I always avoided allocating some serious time to catching up (yeah baby I used to know this stuff better while at university) with them and I was always satisfied with only a quick fix. Blame me all you want, now this is where I want to redeem myself.

There’s an example in the book and the explanation is “superficial” enough to have let me completely in the dark. I categorized it as superficial then when I didn’t know as much as I do know.

So, how about matching the (?<letter>a)(?<letter>\k<letter>b)* regular expression which is the same thing as (?<1>a)(?<1>\1b)* against the "aababb" string.

The above regular expression "(?<letter>a)(?<letter>\k<letter>b)*" will first try to match a core of three characters: the ones highlighted in red, orange and blue. Where <letter> is in green it means a string will be assigned to it and when in orange we refer to its value.

Note: The * at the end refers to only the group between the last set of parentheses (in pink).

Now, even if we only match three characters we can consider as having a complete match (from the regular expression’s perspective) as the "*" makes it optional to have anything else matching after the first partial three-char match.

However, if there is something after and we want to be a match it better be whatever was lastly stored in <letter> and followed by a "b".

Be aware that while still trying to match the first three characters we assigned the <letter> reference two times: first with a string of one character and then a two characters string.

So, the “*” at the end of the regular expression will match something that first of all has to be three characters long (we will see how this actually changes for the next time we try to match “*”).

You can observe now how the second set of parentheses needs to match only two characters at first, to find that next time (when “*” gets involved) it will be match for a three character string.

Let us see how the actual regex engine works.

Step 1

First it will try to see if we have a match against the first group of three characters: "aab".

Grabbing the "aab" substring we can see that the first letter matches with a. Additionally, at this moment the string "a" is stored to the <letter> reference.

The next two characters substring in "aab", and that is "ab" is compared against \k<letter>b which is in fact "ab". Besides the fact it is a match, this string is stored to <letter> overwriting the previous content: "a". So, just to be clear, at this moment <letter> = "ab"

First three letters are a match.

Step 2

Now that we are done with matching the core let’s see what "*" means at this point. It is a repetition of whatever we have in “\k<letter>b which is "ab" + "b" = "abb" and more it is a match for what’s next in the "aababb" string.

So, our original string is a complete match.

What should the original string be suffixed with to have one more match for the "*" and so a complete match of the whole string. Now this depends very much on the answer to the following question:

While the previous matching against "*" was the <letter> reassigned again (obviously with "abb")?

The answer I found to be: YES, while matching a "*", references assignment is still on. So, the match for the "*" will be at this point "abbb" meaning a complete matching string is: "aababbabbb".


XmlNodeType.EndElement or XmlReader.IsEmptyElement

I was going through some learning material for the 505 and I came across an example which turned out a bit wrong as it doesn’t account for all the flavors a valid xml file could be written. After some research I realized that when reading and xml file there is a difference in how the default implementation of XmlReader (and maybe the other ones like XmlTextReader) interprets nodes.

Let’s say we have the following fragments from an xml file

First fragment:


Second fragment:

The first fragment needs two XmlReader.Read calls to read it whereas the second needs only one. Right after a first XmlReader.Read call it reads the <element> node and its type is XmlNodeType.Element. A second XmlReader.Read reads the </element> node which has a type of XmlNodeType.EndElement. This is all good as we know when we are positioned on an element’s beginning node and when we are at its end node.

What about the second fragment? Can we tell when we are positioned on the end node of an element? Not really because the whole element is really only one node. The beginning is end too.

The problem may not seem too obvious as described here but the example I was talking about in the beginning of the post was trying to read the xml file into a TreeView. The trick to know when a set of child nodes ends and it’s time to go back, up one level for the next node to be added, was to test the current xml node’s type against XmlNodeType.EndElement and if it matches, to set the parent one level up.

Case XmlNodeType.EndElement
    parentNode = parentNode.Parent

Guess what. For xml files using the short closing form of a tag, the above Case branch never got executed as we never encountered and EndElement, so the parentNode continued to nest deeper and deeper.

So, apparently, in this situation you will not get an EndElement type of node but an Element type node whose IsEmptyElement property though will be set to True. This way you can tell that when your done with this element you shouldn’t expect another EndElement node to make a decision.

In case your short terminated element contains attributes right after you read the node, save the IsEmptyElement property to a Boolean, as you may read those attributes and you will lose the value as the node changes upon a new Read call.

Even though the xml element of the first fragment is empty too, the IsEmptyElement will stay False so, not very intuitive. Plus this leads to some code that breaks the symmetry of a Select Case statement. However I can perfectly understand the decision MS made, as it makes sense to interpret the second fragment’s element as only one node.

Hope this helps someone even though the MSDN documentation describes (not in so many words) the behaviour.



Cannot connect to an Access mdb file located on the network through OleDb

I have this web application that pulls its info from an Access database. This mdb file is located somewhere on the network. Now, the only access that I need to that file (from a user perspective) is read-only as I only extract and present information. I’m not going into details here but I even specified a read-only mode in the connection string.

Any attempt to create a connection to my database failed. You can see below the web application error and the Sysinternals FileMon capture I took while reproducing the issue.




It became pretty obvious that the account aspnet_wp.exe runs as MIRCEA\IUSR_MIRCEA  and it is denied access to the ctel.mdb file.

I decided then to impersonate the process aspnet_wp.exe under an account that I know has the required permissions. So, I added the following to the <system.web> section in web.config

<identity impersonate="true" userName="cteluser" password="cteluser"></identity>

Full of hope I fired up the application one more time expecting to finally see the page loading. Don’t hold your breath yet. The error I got is “better” than the previous one.


Didn’t I tell you. This is the kind of error that can make you leave everything and go to bed hoping that tomorrow it will go away by itself. What should I do? … FileMon, of course.


So, aspnet_wp.exe is again denied access, to a “Temp” folder this time. This is a folder in the folder of the Windows machine profile. Once I went ahead and gave write permissions to “cteluser” on that folder I was able finally to run my web application. Remember the cteluser is the user that aspnet_wp.exe impersonates so it can access ctel.mdb over the network.