Archive for the ‘C#’ Category

Unknown build error, ‘An item with the same key has already been added.’

with 2 comments

I got this error today when trying to build a C# solution today.
In my case the error was caused by the fact that I had the same file added twice in the .csproj file. (This happened after a merge in the *.csproj file).
To find the exact file would have been very hard since there can be thousands of files in a *.csproj.
The first thing to do is to find the exact project which is causing the issue. Visual studio is using msbuild in the background to do the builds, but it’s outputing very little information from msbuild.
This can be controlled by going to Tools->Options->Projects and Solutions->Build and Run. Set MSBuild project output verbosity to Diagnostic.
Now do a build again. After it fails display the output window. Debug->Windows->Output. Select “Show output from” -> Build. Find the first “Build failed” text.
Now you know which project’s build failed.
Go to Build->Clean, then right click on the specific project that is causing the problem and select build. (Do not build entire solution).
If you do that instead of building the entire solution, you will sometimes get an error with the exact file which is causing the build problem.
If it still doesn’t show you the file, you will have to go through the csproj and find out which line is duplicate.
Now unload that project (right click -> unload). Right click the unloaded project -> edit. Now search for that file which is causing the build problem and ensure that it is only added once in the csproj.
E.g. if the file is called EquipmentDetailsTitleBarPopup.xaml, look for duplicates of

    <Page Include="Features\Equipments\EquipmentDetailsTitleBarPopup.xaml">

and remove one of them.
Now reload the project (right click -> reload) and build again.

This fixed it for me.


Written by Liviu Trifoi

January 18, 2013 at 1:35 pm

Posted in .NET, C#, Visual Studio 2010

Visual Studio 2010 – No Source available: Browse To Find Source

leave a comment »

Seems there’s a lot of confusion about this “Browse to find source” feature of visual studio 2010.

I had a colleague which was convinced he didn’t need pdbs to debug an .net app, because of the “Browse to find source” :) Anyone else want to take a bet with me on that?
In this post I want to clarify when to use this “Browse to find source” feature since the mdsn doc is missing, and sometimes is grayed out for no (apparent) reason.

If you experiment a little with process monitor, you’ll see that every time visual studio displays the “Browse to find source” page, it looks inside the pdb file corresponding to the top stacktrace instruction your are trying to step into.

For example in the image below, it looks into the pdb of the DateTime.Now (which is mscorlib.pdb), since I’m trying to step into DateTime.Now:
pdb load info

But what is it looking for there?
Well, if the pdb file is for a private build, then it includes information on where to find the source file from which it was built.
And that’s what visual studio is searching for inside the pdb.

For example I built the following simple library consisting of just one class:

    public class Class1
        public static DateTime GetCurrentDateTime()
            return DateTime.Now;

Class1.cs was located at: d:\ClassLibrary\Class1.cs

If you build this program and then look inside the pdb file that was generated for the build, using dia2dump, you’ll see, among other things, the Files section:


Compiland = ClassLibrary.Class1


Suppose I reference the ClassLibrary1.dll which I built, in another simple console project called Console1 located at D:\Console1:

    class Program
        static void Main(string[] args)
            var currentDateTime = ClassLibrary1.Class1.GetCurrentDateTime();

If I build Console1I will have in D:\Console1\bin\debug a Console1.exe, Console1.pdb, and a ClassLibrary1.dll,
If I try to step into the ClassLibrary1.Class1.GetCurrentDateTime() above, the first thing visual studio will do, is look for the ClassLibrary1.pdb at: D:\Console1\bin\debug\ClassLibrary1.pdb, and then in a couple of other locations.

If it doesn’t find the pdb in any of those locations, then you’ll get a disabled (grayed out) “Browse to source”. Why? Because it’s impossible for the visual studio debugger to know which function goes into what class and at what line number having just an binary and the source code files from which it was built. You need to understand that if you just have an .net executable(or any other assembly) and its source code visual studio doesn’t magically know how to match the instructions from the exe with their corresponding functions/line numbers from source files. That’s what pdbs help do. They store this “mapping”.
You’ll would never be able to do do source code level stepping inside the debugger if you have the source files but don’t have the pdb.
So the first step you do if you get “No source code available” is right click on the top call-stack instruction, choose “Symbol Load Information” and be sure you have a “Symbols loaded” message somewhere there. If you don’t see any “Symbols loaded”, then you should try placing the pdb at one of the locations which is listed there.

Now if visual studio finds the pdb file it might still be possible, using our above example, that the file ” D:\ClassLibrary\Class1.cs” which it finds in the pdb, is no longer there.
You either moved it at a another location, say C:\ClassLibrary, or it was built by someone else on another machine at D:\ClassLibrary\Class1.cs and you don’t even have that directory on your computer.
In those cases you get a nice browse to source file dialog:

If you cancel this accidentally (or intentionally) you will get a “Browse to source” enabled:

Why? Well because the pdb is OK, and contains source line information, just that the source files path inside pdb, are not at the right location and since you canceled “browse to source file”, visual studio has no idea where to look for the source files.
One more thing to look out for is that not all pdbs have source line number information: see for example this. Those are pdb’s which you cannot use for stepping into source code, since they don’t contain the necessary information.
In those cases, you’ll also get a grayed out “Browse to source”.
In that case, we’re out of luck my friend and the only available solution is to use Reflector Pro’s ability to debug and step into those assemblies from visual studio at runtime :(

Written by Liviu Trifoi

May 19, 2011 at 8:14 pm

Silverlight IsolatedStorageFile.IncreaseQuotaTo

leave a comment »

I had a lot of troubles using IsolatedStorageFile.IncreaseQuotaTo

Msdn documentation for IsolatedStorageFile.IncreaseQuotaTo states that:
To increase the quota, you must call this method from a user-initiated event, such as in an event handler for a button-click event. When you call the IncreaseQuotaTo method, the common language runtime in Silverlight presents a dialog box for the user to approve the request. If the user declines the request, this method returns false and the quota remains the same size.

Now that seems like half-baked documentation to me since it doesn’t accurately describe what a user-initiated event is, it only gives one example.
After much digging, I did find out what a user initiated event is. Seems that the msdn documentation specifies what a user initiated event in the section related to “events overview“, but there’s no link between documentation of IsolatedStorageFile.IncreaseQuotaTo and Events Overview.
So here goes the definition:

Silverlight user-initiated events include the mouse events (such as MouseLeftButtonDown), and the keyboard events (such as KeyDown). Events of controls that are based on such events (such as Click) are also considered user-initiated.

API calls that require user initiation should be called as soon as possible in an event handler. This is because the Silverlight user initiation concept also requires that the calls occur within a certain time window after the event occurrence. In Silverlight 4, this time window is approximately one second.

User-initiated event restrictions also apply to usages of JavaScript API for Silverlight.

When Silverlight is in full-screen mode, some input events are deliberately limited for security reasons, although this can be mitigated for out-of-browser applications using elevated trust. For more information, see Full-Screen Support.

At first these limitations seemed draconian to me.
Thinking it more about it, I guess Microsoft didn’t want a user to have many tabs open in a browser and then poof: I call automatically IncreaseQuotaTo.
Since the IncreaseQuotaTo is a modal dialog, meaning you can’t navigate to other tabs from it, if the user is now on page google.com, and I show automatically IncreaseQuotaTo, the user might think that google.com is asking for more storage :)). This would be a security breach indeed.

Had they implemented this with a page level dialog, then that would have probably been more easily hacked (worked around). I can already start imagining drawing some evil forged “Silverlight needs to be updated” image over the IncreaseQuotaTo text, hence tricking the user in pressing Yes . Or maybe some other evil scenario.

I was curios though how exactly does silverlight know what a user initiated event is, but after digging through .net framework source code I’ve got to a dead end:

if ((browserService == null) || !browserService.InPrivateMode())
return false; //means that IncreaseQuota will fail
where browser.IsInPrivateMode is:

public bool InPrivateMode()
    bool privateMode = false;
    return (NativeMethods.SUCCEEDED(UnsafeNativeMethods.DOM_InPrivateMode(this._browserServiceHandle, out privateMode)) && privateMode);

where DOM_InPrivateMode is in a DllImport[“agcore”], which according to microsoft is confidential :(
So it looks like I won’t find out soon how they’re detecting user initiated events, although I’m guessing they have some centralized private method that detects clicks for example, and then probably sets a flag that this was indeed “a user initiated event”, and since you can’t forge clicks or keypresses using javascript and since you can’t call those private methods using reflection, it’s “safe”.

Written by Liviu Trifoi

May 18, 2011 at 6:33 pm

Posted in .NET, C#, Silverlight

Tagged with , , ,

Comparing object properties in C#

leave a comment »

I needed today, in my unit tests, a way to compare two objects of the same type, property by property.
In addition to this I needed the following:

    -If the property is a reference type, then go inside it and compare that too, property by property. (Basically deep object property comparison).
    -If the property is IEnumerable, then compare properties of each element inside the IEnumerable.
    -If the elements inside the IEnumerable are themselves IEnumerable then this could go on many levels deep, but this was not on my requirements.
    -I also want the possibility to ignore certain properties in the comparison, such as property which is a delegate (like the Filter property of the PagedCollectionView framework class).
    And of course I would need to know which properties were not equal, so I could show into my assertions.

Obviously this involves some heavy reflection code which I’m sure it would be fun to write if you had the time.
An alternative is to use a free library, with just one class, that does exactly this: http://comparenetobjects.codeplex.com/
Usage is very simple, and supports all kind of useful features which you can see below:

var compareObjects = new CompareObjects()
                CompareChildren = true, //this turns deep compare one, otherwise it's shallow
                CompareFields = false,
                CompareReadOnly = true,
                ComparePrivateFields = false,
                ComparePrivateProperties = false,
                CompareProperties = true,
                MaxDifferences = 1,
                ElementsToIgnore = new List<string>() { "Filter" }
            bool areEqual = compareObjects.Compare(objectA, objectB);
            Assert.IsTrue(areEqual, compareObjects.DifferencesString); //DifferencesString contains exactly what property and what value(s) was different

For Silverlight you do have to remove a couple of line of codes from it, related to DataTable comparison, which don’t exist in silverlight, but it’s easy and only takes a couple of minutes.

Happy Deep Object Comparisons :)

Written by Liviu Trifoi

April 28, 2011 at 6:20 pm

Silverlight stack trace line numbers

with 8 comments

As you probably know, to have stack traces line numbers in a .net app you need pdb files.
In Silverlight though, pdb files don’t get included in the XAP. Even if it was possible to include them you probably wouldn’t want them there because they tend to be huge and you want to have a fast loading web-site.

So what will you do when your client calls you and says he received an error message like the one below?
at ChaptersView.ChaptersTree_OnMouseMove
at ChaptersView.GetPreviousCommandButton

After I had a look today at the answer to this question, it became obvious:

Step 1)
In the place you now log or show exceptions to the user, you stop writing a normal stack trace using exception.ToString() or exception.StackTrace.
You should write a stacktrace that includes IL offsets using the code below:

catch (Exception exception)
            string errorDescription = string.Empty;
                StackTrace st = new System.Diagnostics.StackTrace(ex);
                string stackTrace = "";

                if (st != null)
                    foreach (StackFrame frame in st.GetFrames())
                        stackTrace = "at " + frame.GetMethod().Module.Name + "." + frame.GetMethod().ReflectedType.Name +
                                     "." + frame.GetMethod().Name + "  (IL offset: 0x" +
                                     frame.GetILOffset().ToString("x") + ")\n" + stackTrace;
                errorDescription = stackTrace;
            } catch //sometimes there are no stackframes, just use ex.ToString() in those cases
                errorDescription = ex.ToString();
            //do something with error description, like show it to the user or send it to your log server  

If we pass the exception that generated the stacktrace in our first example, it will look like:

at ChaptersView.ChaptersTree_OnMouseMove (IL offset: 0x44)
at ChaptersView.GetPreviousCommandButton (IL offset: 0x28)

Step 2: Client sends you an exception/log that includes IL offsets
You can use Reflector or IL Disassembler (which comes with visual studio) to find out the piece of code that generated the exception.
I prefer IL Disassembler because Reflector will only show you IL code from those IL offsets, and there’s a large chance you don’t read IL as well as you read C#.

So you have to perform the following steps:
1) Go to start menu and start IL Disassembler
2) Inside IL Disassembler go to File->Open and load the dll (not the XAP) where the exception was thrown. (e.g. …\Release\Client.dll)
3) Go to View->Show source lines
4) Now navigate to the last method that appears in the stacktrace and we look after the IL offset from the stacktrace.
In our example above this is ChaptersView.GetPreviousCommandButton and has offset 0x28

Inside IL Disassembler we will see a mix of C# code and corresponding IL code:

  IL_001c:  brtrue.s   IL_0029
//000193:             {
//000194:                 throw new NullReferenceException(“Test Exception”);
  IL_001e:  ldstr      "Test Exception"
  IL_0023:  newobj     instance void [mscorlib]System.Exception::.ctor(string)
  IL_0028:  throw
//000195:             }
//000197:             var previousCommandButton =

As you can see, at IL_0028 there’s the instruction that caused the exception.
This is IL code, but because we’ve checked “View->Show source lines” we also get the corresponding C# code just a few lines above.

//000193: {
//000194: throw new NullReferenceException();

So, in this case the exception was thrown because I’ve thrown it explicitly in order to show how IL offsets and IL Disassembler can be used to get an alternative to stack trace line numbers in silverlight.

Also please don’t just put that piece of code in every catch block. Create a method like “string GetStackTraceFromException(Exception ex)”
Another good idea would be to have some centralized error service, on the server side. You would then send those exception descriptions automatically along with method parameter values logged with PostSharp, whenever an error happens.
So by the time the client calls you, you have fixed the problem and he’s impressed ;)

Happy Debugging.

Written by Liviu Trifoi

April 21, 2011 at 6:15 pm

AppDomain.Unload Garbage Collection

with 2 comments

It all started while I was investigating memory usage in a .net 2.0 system. The system was organized into independent components that were loaded into Application Domains.

The components performed their jobs in the isolated environment an AppDomain provides and, when their job finished, they were unloaded using AppDomain.Unload.

The problem was that memory didn’t seem to be recollected when I was doing AppDomain.Unload so I turned to read again sections from Jeffrey Richter’s, CLR via C# 3rd edition, since that was the book from which I’ve learned about Application Domains and Garbage Collection. It stated clearly “The CLR forces a garbage collection to occur, reclaiming the memory used by any objects that were created by the now unloaded AppDomain. The Finalize methods for these objects are called, giving the objects a chance to clean themselves up properly.”

After some mail exchange with Jeffrey, which you can read, along with an example here, he suggested:

A better test would be to look at some object addresses in the debugger and see if compaction actually occurs. If it does (and I suspect it does), then the collection count is just not being updated correctly.

To keep it short I had a small program that did this:

1) Create and application domain,

2) Create some object instances inside so It has some allocated memory.

2) Call AppDomain.Unload

3) Perform an explicit GC.Collect() call.

I took Visual Studio and loaded Son of Strike into it and it revealed this:

Before AppDomain.Unload:

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x0180b1f0
generation 1 starts at 0x017d100c
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  02f75470 0x007a4470(8012912)
Total Size  0x7e5464(8279140)
GC Heap Size  0x7e5464(8279140)

After AppDomain.Unload (same addresses, no heap compaction was done)

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x0180b1f0
generation 1 starts at 0x017d100c
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  02f75470 0x007a4470(8012912)
Total Size  0x7e5464(8279140)
GC Heap Size  0x7e5464(8279140)

After GC.Collect(), addresses differ indicating heap compaction was done.

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x01811234
generation 1 starts at 0x0180b1f0
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  027d3240 0x00002240(8768)
Total Size   0x43234(274996)
GC Heap Size   0x43234(274996)

So to conclusion is that heap compaction is not done and the only thing you can really be sure during an AppDomain unload is that objects will get to be marked as unreachable.

Memory for those objects will be reclaimed whenever the next garbage collection happens, so, in this case it’s not a good idea to call for GC.Collect() yourself.

Written by Liviu Trifoi

May 21, 2010 at 5:24 pm

Posted in .NET, AppDomain, C#, debugging

Tagged with

What is an Application Domain and what is it used for?

leave a comment »

I recently had to work with some large legacy .net 2.0 system and it did made heavy use of Application Domains. I did have general knowledge about what an Application Domain is, but wasn’t familiar with other basic topics like communication between AppDomains and memory recollection. So, in this post I’ll share what I’ve learned on the topic.

You can imagine an Application Domain as a logical container in which you can load assemblies. An application domain lives inside a process and you can have many application domains living at the same time inside the same process (see figure below).

ApplicationDomains Image

In the above image we have C# SampleConsoleApplication executable. When you start the executable a SampleConsoleApplication process is created, then the CLR creates a default AppDomain for your code.  In this case, the default domain is the SampleConsoleApplication domain and, as you can see, the CLR also loaded other assemblies referenced by SampleConsoleApplication into this default domain (like System.dll).

Next, let’s say you want your SampleConsoleApplication to use classes from another library, ClassLibrary1. Normally you would just reference ClassLibrary1. But there are scenarios where you don’t want to do that. A few scenarios I can think is:

  1. You have an application that supports plug-ins (e.g. MMC, SAP, etc). Presume  ClassLibrary1 is a plug-in written by a 3rd Party. You have to isolate ClassLibrary1 from a security and reliability point of view. From a security point of view you don’t want ClassLibrary1 to access your directly code (MMC/SAP code in this case) because it might be malicious. From reliability point of view, even if ClassLibrary1 is not malicious it still might have bugs in implementation that you couldn’t just ignore using a try catch. For example it could have a memory leak and, if you were referencing it directly, it would cause your memory usage to keep growing and never be recollected even after you don’t use ClassLibrary1 classes anymore.
  2. Other examples: You are writing a web-server like IIS. Unintentionally somebody could have bugs in his ASP.NET web-site that could crash your web server.  You want complete isolation here, but without creating one process for each web-site. Why? Processes are very expensive to create form both performance and initial memory footprint point of view. Your web-server would be slow and would accommodate a lower number of web-sites as opposed to if you were using an ApplicationDomain for each web-site.
  3. You are writing a real-time system that processes data received from instruments (e.g. various blood analyzers). Some of the processing steps are done by various libraries written by various companies and could fail because a type of instrument is malfunctioning, a bug in the code, etc. You could use an application domain to isolate 3rd party processing failures, and simply unload problematic processings when they happen, leaving the core of the system to continue functioning and process data.

I guess, you see the pattern by now. Application Domains can help you isolate code from a security, resource usage, exceptions point of view without creating new processes. They can also be unloaded when you either finish using them or code inside them is causing problems.

To provide this level of isolation, you are not allowed to use types from an AppDomain in another AppDomain directly. Instead when you need to use a class from another AppDomain you can use either remoting or WCF. (See http://stackoverflow.com/questions/1294494/is-net-remoting-really-deprecated)

If you’re stuck with maintaining a legacy system that uses remoting keep on reading.

If you’re using remoting, there are two important ways by which you can send one object from one AppDomain to another.

  1. Marshaling by Value: You have a class that is marked as serializable. When you create an instance of that class and pass it to another domain, as a parameter in a function call for example, you are actually serializing/deserializing it. This is Marshaling by Value.
  2. Marshaling by Reference: In some cases it doesn’t make sense to serialize a class and send it to another AppDomain serialized, because the class contains members that are specific to a single application domain. For example: Suppose you have a class member that is a reference to socket, a filehandle, a db connection. Once serialized, those references would be invalid in the receiving AppDomain. In this case you have your class inheriting from MarshalByRefObject and when you instantiate it in another AppDomain a TransparentProxy is created for it. For example you have a DataAccess class that contains a method InsertPerson(string name) that opens a db connection and inserts a person in a database. When you instantiate it in another AppDomain a class identical to your class (having same name, same public methods, same public properties) is created instead. This is called a TransparentProxy. When you call InsertPerson on it, it will make a remote call to an instance of type DataAccess living in the first AppDomain, which will perform the operation and send the result back. (it’s enough if you imagine the remote call as web-service call).

I hope this abstraction suffices your needs.

Written by Liviu Trifoi

April 21, 2010 at 5:26 pm