Archive for the ‘.NET’ 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

Unable to determine the URL to the Xap file from web

leave a comment »

I encountered today this undocumented error: “Unable to determine the URL to the Xap file from web XXX”, after renaming one of my silverlight 4 projects, which was set to run Out of Browser.
Supposing you renamed your project from XXX to YYY.
To fix the error above, go to your YYY.csproj.user on the disk, and open it for editing with notepad.
Then change


Now save it, and you’re good to go.

Usually *.csproj.user files should not be under source control.
So if you rename a silverlight project which a colleague of yours has set it to be ran out of browser on his computer, then he will get this error unless you share the same *.csproj.user on the source control (which is not a good idea).

I think a visual studio 2010 improvement is needed here, that describes the situation better. Something like: “Unable to determine the URL to the Xap file from web XXX. If the project was renamed, please rename it inside your *.csproj.user” file settings as well”

Written by Liviu Trifoi

June 1, 2011 at 8:48 am

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 , , ,

Debugging .Net Framework source stepping problems using dia2dump

with 2 comments

So you are trying to step into .net framework source code.
You configured debugging options, triple-checked everything, consulted this comprehensive FAQ here: http://blogs.msdn.com/b/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx.
Still nothing seems to work.

You are not alone :(
The most likely cause is that Microsoft has released some patches to the .net framework, and didn’t publish pdbs that you could use for stepping into .net framework source code.
So supposing that you are trying to debug DateTime.Now.
First look on where the pdb-file downloaded from Microsoft Symbol Server is located on your computer, by right clicking in the Call-Stack -> Symbol Load Information:

Now we’ll look inside this pdb file using dia2dump which is a tool that comes with visual studio at: c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\

It’s not compiled by default, so you’ll have to open it’s sln and compile it. Fortunately it’s just a small C++ proj, and compiles easily. (Just press compile).
After compiling you should see it at c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\Debug\.
To look inside a pdb file you just open a command prompt and write: dia2dump.exe xxx.pdb >xxx.txt . This will write the contents of the pdb inside a xxx.txt.

So if we run dia2dump on the mscorlib.pdb file which was downloaded we can see that it doesn’t contain any source files information or start /end addresses like the mscorlib.pdb you get from: http://referencesource.microsoft.com/netframework.aspx and it’s ~300 KB vs 10 MB.
Now I’m no pdb file contents expert, since the format of the pdb keeps changing and there’s no public specification according to microsoft (you can read more about it here: http://msdn.microsoft.com/en-us/library/x93ctkx8%28v=VS.80%29.aspx)
But the fact that there’s no addresses and source code mappings does ring a bell to me that the pdb is not one that we could use for .net framework source code stepping purposes.

So how do we make it work?
My first idea was of using that “Browse to source” option in visual studio 2010, and browse to .net framework source code you have downloaded manually from referencesource.microsoft.com. But after looking at the information inside the pdb, or rather the lack of it, it’s now clear why it’s always grayed out.

So one solution is to use Reflector Pro, which is commercial, and use it’s runtime debugging feature to step into microsoft .net framework source code.
This is not an ideal solution (especially because of the optimized code), and I just don’t understand why Microsoft doesn’t publish automatically source code according to their latest hotfixes/patches of the .net framework :(
I mean, I need to step into .net framework because I’m having problems today, not in two months when they decide to actually publish it. What am I supposed to do? Disable windows update so I don’t install any hotfixes? :(
This might be a second solution: Just have a virtual machine with visual studio installed, and a .net framework version which has .net framework source stepping support. Then disable any updates on that virtual machine and do your debugging there. (Really painfull, I know)

Written by Liviu Trifoi

May 17, 2011 at 8:06 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