<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Blog</title>
	<atom:link href="http://liviutrifoi.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://liviutrifoi.wordpress.com</link>
	<description>In .NET</description>
	<lastBuildDate>Thu, 23 Feb 2012 08:05:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='liviutrifoi.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My Blog</title>
		<link>http://liviutrifoi.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://liviutrifoi.wordpress.com/osd.xml" title="My Blog" />
	<atom:link rel='hub' href='http://liviutrifoi.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Unable to determine the URL to the Xap file from web</title>
		<link>http://liviutrifoi.wordpress.com/2011/06/01/unable-to-determine-the-url-to-the-xap-file-from-web/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/06/01/unable-to-determine-the-url-to-the-xap-file-from-web/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 08:48:04 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[OutOfBrowser]]></category>
		<category><![CDATA[visual studio 2010]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=279</guid>
		<description><![CDATA[I encountered today this undocumented error: &#8220;Unable to determine the URL to the Xap file from web XXX&#8221;, 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, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=279&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
I encountered today this undocumented error: &#8220;Unable to determine the URL to the Xap file from web XXX&#8221;, after renaming one of my silverlight 4 projects, which was set to run Out of Browser.<br />
Supposing you renamed your project from XXX to YYY.<br />
To fix the error above, go to your <em>YYY.csproj.user</em> on the disk, and open it for editing with notepad.<br />
Then change<br />
<pre class="brush: xml;">
&lt;OutOfBrowserProjectToDebug&gt;XXX&lt;/OutOfBrowserProjectToDebug&gt;
to 
&lt;OutOfBrowserProjectToDebug&gt;YYY&lt;/OutOfBrowserProjectToDebug&gt;
</pre></p>
<p>Now save it, and you&#8217;re good to go.</p>
<p>Usually *.csproj.user files should not be under source control.<br />
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).</p>
<p>I think a visual studio 2010 improvement is needed here, that describes the situation better. Something like: &#8220;Unable to determine the URL to the Xap file from web XXX. If the project was renamed, please rename it inside your *.csproj.user&#8221; file settings as well&#8221;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/279/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/279/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/279/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=279&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/06/01/unable-to-determine-the-url-to-the-xap-file-from-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>Visual Studio 2010 &#8211; No Source available: Browse To Find Source</title>
		<link>http://liviutrifoi.wordpress.com/2011/05/19/visual-studio-2010-no-source-available-browse-to-find-source/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/05/19/visual-studio-2010-no-source-available-browse-to-find-source/#comments</comments>
		<pubDate>Thu, 19 May 2011 20:14:43 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[browse to find source]]></category>
		<category><![CDATA[visual studio 2010]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=254</guid>
		<description><![CDATA[Seems there&#8217;s a lot of confusion about this &#8220;Browse to find source&#8221; feature of visual studio 2010. I had a colleague which was convinced he didn&#8217;t need pdbs to debug an .net app, because of the &#8220;Browse to find source&#8221; :) Anyone else want to take a bet with me on that? In this post [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=254&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
Seems there&#8217;s a lot of confusion about this &#8220;Browse to find source&#8221;  feature of visual studio 2010.<br />
<img src="http://liviutrifoi.files.wordpress.com/2011/05/nosourcecodeavailable.png?w=700" alt="no_source_available" />
</p>
<p align="justify">
I had a colleague which was convinced he didn&#8217;t need pdbs to debug an .net app, because of the &#8220;Browse to find source&#8221; :) Anyone else want to take a bet with me on that?<br />
In this post I want to clarify when to use this &#8220;Browse to find source&#8221; feature since the mdsn doc is missing, and sometimes is grayed out for no (apparent) reason.
</p>
<p align="justify">
If you experiment a little with process monitor, you&#8217;ll see that every time visual studio displays the &#8220;Browse to find source&#8221; page, it looks inside the pdb file corresponding to the top stacktrace instruction your are trying to step into.
</p>
<p align="justify">
For example in the image below, it looks into the pdb of the DateTime.Now (which is mscorlib.pdb), since I&#8217;m trying to step into DateTime.Now:<br />
<img src="http://liviutrifoi.files.wordpress.com/2011/05/pdbloadinfo.png?w=700" alt="pdb load info" /></p>
<p>But what is it looking for there?<br />
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.<br />
And that&#8217;s what visual studio is searching for inside the pdb.</p>
<p>For example I built the following simple library consisting of just one class:</p>
<p><pre class="brush: csharp;">
    public class Class1
    {
        public static DateTime GetCurrentDateTime()
        {
            return DateTime.Now;
        }
    }
</pre></p>
<p>Class1.cs was located at: d:\ClassLibrary\Class1.cs</p>
<p>If you build this program and then look inside the pdb file that was generated for the build, using <a href="http://liviutrifoi.wordpress.com/2011/05/17/net-framework-stepping-problems/">dia2dump</a>, you&#8217;ll see, among other things, the Files section:</p>
<p><em>*** FILES</p>
<p>Compiland = ClassLibrary.Class1</p>
<p>	 D:\ClassLibrary\Class1.cs</em></p>
<p>Suppose I reference the <em>ClassLibrary1.dll</em> which I built, in another simple console project called <em>Console1</em> located at <em>D:\Console1</em>:</p>
<p><pre class="brush: csharp;">
    class Program
    {
        static void Main(string[] args)
        {
            var currentDateTime = ClassLibrary1.Class1.GetCurrentDateTime();
        }
    }
</pre></p>
<p>If I build <em>Console1</em>I will have in <em>D:\Console1\bin\debug</em> a <em>Console1.exe, Console1.pdb, and a ClassLibrary1.dll,  </em><br />
If I try to step into the <em> ClassLibrary1.Class1.GetCurrentDateTime()</em>  above, the first thing visual studio will do, is look for the <em>ClassLibrary1.pdb</em> at: <em>D:\Console1\bin\debug\ClassLibrary1.pdb</em>, and then in a couple of <a href="http://www.wintellect.com/CS/blogs/jrobbins/archive/2009/05/11/pdb-files-what-every-developer-must-know.aspx">other locations</a>.
</p>
<p align="justify">
If it doesn&#8217;t find the pdb in any of those locations, then you&#8217;ll get a disabled (grayed out) &#8220;Browse to source&#8221;. Why? Because it&#8217;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.<br />
You&#8217;ll would never be able to do do source code level stepping inside the debugger if you have the source files but don&#8217;t have the pdb.<br />
So the first step you do if you get &#8220;No source code available&#8221; is right click on the top call-stack instruction, choose &#8220;Symbol Load Information&#8221; and be sure you have a &#8220;Symbols loaded&#8221; message somewhere there. If you don&#8217;t see any &#8220;Symbols loaded&#8221;, then you should try placing the pdb at one of the locations which is listed there.
</p>
<p align="justify">
Now if visual studio finds the pdb file it might still be possible, using our above example, that the file <em>&#8221; D:\ClassLibrary\Class1.cs&#8221;</em>  which it finds in the pdb, is no longer there.<br />
You either moved it at a another location, say <em>C:\ClassLibrary</em>, or it was built by someone else on another machine at <em>D:\ClassLibrary\Class1.cs</em> and you don&#8217;t even have that directory on your computer.<br />
In those cases you get a nice browse to source file dialog:<br />
<img src="http://liviutrifoi.files.wordpress.com/2011/05/browse1.png?w=700" alt="browsetosource" /></p>
<p>If you cancel this accidentally (or intentionally) you will get a &#8220;Browse to source&#8221; enabled:</p>
<p><img src="http://liviutrifoi.files.wordpress.com/2011/05/browseenabled.png?w=700" alt="" /></p>
<p align="justify">
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 &#8220;browse to source file&#8221;, visual studio has no idea where to look for the source files.<br />
One more thing to look out for is that not all pdbs have source line number information: see for example <a href="http://liviutrifoi.wordpress.com/2011/05/17/net-framework-stepping-problems/">this</a>. Those are pdb&#8217;s which you cannot use for stepping into source code, since they don&#8217;t contain the necessary information.<br />
In those cases, you&#8217;ll also get a grayed out &#8220;Browse to source&#8221;.<br />
In that case, we&#8217;re out of luck my friend  and the only available solution is to use Reflector Pro&#8217;s ability to debug and step into those assemblies from visual studio at runtime :(</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/254/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=254&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/05/19/visual-studio-2010-no-source-available-browse-to-find-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2011/05/nosourcecodeavailable.png" medium="image">
			<media:title type="html">no_source_available</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2011/05/pdbloadinfo.png" medium="image">
			<media:title type="html">pdb load info</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2011/05/browse1.png" medium="image">
			<media:title type="html">browsetosource</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2011/05/browseenabled.png" medium="image" />
	</item>
		<item>
		<title>Silverlight IsolatedStorageFile.IncreaseQuotaTo</title>
		<link>http://liviutrifoi.wordpress.com/2011/05/18/silverlight-isolatedstoragefile-increasequotato/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/05/18/silverlight-isolatedstoragefile-increasequotato/#comments</comments>
		<pubDate>Wed, 18 May 2011 18:33:55 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[IsolatedStorage]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=247</guid>
		<description><![CDATA[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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=247&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
I had a lot of troubles using IsolatedStorageFile.IncreaseQuotaTo</p>
<p>Msdn documentation for <a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.increasequotato%28v=vs.95%29.aspx">IsolatedStorageFile.IncreaseQuotaTo</a> states that:<br />
<em>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.</em>
</p>
<p align="justify">
Now that seems like half-baked documentation to me since it doesn&#8217;t accurately describe what a user-initiated event is, it only gives one example.<br />
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 &#8220;<a href="http://msdn.microsoft.com/en-us/library/cc189018%28VS.95%29.aspx#fullscreen_mode_and_events">events overview</a>&#8220;, but there&#8217;s no link between documentation of IsolatedStorageFile.IncreaseQuotaTo and Events Overview.<br />
So here goes the definition:</p>
<p><em>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.</p>
<p>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.</p>
<p>User-initiated event restrictions also apply to usages of JavaScript API for Silverlight.</p>
<p>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.</em>
</p>
<p>At first these limitations seemed draconian to me.<br />
Thinking it more about it, I guess Microsoft didn&#8217;t want a user to have many tabs open in a browser and then poof: I call automatically IncreaseQuotaTo.<br />
Since the IncreaseQuotaTo is a modal dialog, meaning you can&#8217;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.
</p>
<p align="justify">
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 &#8220;Silverlight needs to be updated&#8221; image over the IncreaseQuotaTo text, hence tricking the user in pressing Yes . Or maybe some other evil scenario.
</p>
<p align="justify">
 I was curios though how exactly does silverlight know what a user initiated event is, but after digging through .net framework source code I&#8217;ve got to a dead end:</p>
<p><pre class="brush: csharp;">
if ((browserService == null) || !browserService.InPrivateMode())
{
    //..
}
return false; //means that IncreaseQuota will fail
where browser.IsInPrivateMode is:

[SecuritySafeCritical]
public bool InPrivateMode()
{
    bool privateMode = false;
    return (NativeMethods.SUCCEEDED(UnsafeNativeMethods.DOM_InPrivateMode(this._browserServiceHandle, out privateMode)) &amp;&amp; privateMode);
}

</pre></p>
<p>where DOM_InPrivateMode is in a DllImport["agcore"], which according to microsoft is confidential :(<br />
So it looks like I won&#8217;t find out soon how they&#8217;re detecting user initiated events, although I&#8217;m guessing they have some centralized private method that detects clicks for example, and then probably sets a flag that this was indeed &#8220;a user initiated event&#8221;, and since you can&#8217;t forge clicks or keypresses using javascript and since you can&#8217;t call those private methods using reflection, it&#8217;s &#8220;safe&#8221;.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/247/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=247&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/05/18/silverlight-isolatedstoragefile-increasequotato/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging .Net Framework source stepping problems using dia2dump</title>
		<link>http://liviutrifoi.wordpress.com/2011/05/17/net-framework-stepping-problems/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/05/17/net-framework-stepping-problems/#comments</comments>
		<pubDate>Tue, 17 May 2011 20:06:07 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[.net framework source stepping]]></category>
		<category><![CDATA[dia2dump]]></category>
		<category><![CDATA[issues]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=238</guid>
		<description><![CDATA[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&#8217;t publish pdbs that you could [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=238&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
So you are trying to step into .net framework source code.<br />
You configured debugging options, triple-checked everything, consulted this comprehensive FAQ here: <a href="http://blogs.msdn.com/b/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx">http://blogs.msdn.com/b/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx</a>.<br />
Still nothing seems to work.
</p>
<p align="justify">
You are not alone :(<br />
The most likely cause is that Microsoft has released some patches to the .net framework, and didn&#8217;t publish pdbs that you could use for stepping into .net framework source code.<br />
So supposing that you are trying to debug <em>DateTime.Now</em>.<br />
First look on where the pdb-file downloaded from Microsoft Symbol Server is located on your computer, by right clicking in the <em>Call-Stack -&gt; Symbol Load Information</em>:<br />
<img src="http://liviutrifoi.files.wordpress.com/2011/05/pdbloadinfo.png?w=700" alt="pdbloadinfo" />
</p>
<p align="justify">
Now we&#8217;ll look inside this pdb file using <em>dia2dump</em> which is a tool that comes with visual studio at: <em>c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\</em></p>
<p>It&#8217;s not compiled by default, so you&#8217;ll have to open it&#8217;s sln and compile it. Fortunately it&#8217;s just a small C++ proj, and compiles easily. (Just press compile).<br />
After compiling you should see it at <em>c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\Debug\</em>.<br />
To look inside a pdb file you just open a command prompt and write: <em>dia2dump.exe   xxx.pdb &gt;xxx.txt </em> . This will write the contents of the pdb inside a xxx.txt.
</p>
<p align="justify">
So if we run <em>dia2dump</em> on the mscorlib.pdb file which was downloaded we can see that it doesn&#8217;t contain any source files information or start /end addresses like the mscorlib.pdb you get from: <a href="http://referencesource.microsoft.com/netframework.aspx">http://referencesource.microsoft.com/netframework.aspx</a> and it&#8217;s ~300 KB vs 10 MB.<br />
Now I&#8217;m no pdb file contents expert, since the format of the pdb keeps changing and there&#8217;s no public specification according to microsoft (you can read more about it here: <a href="http://msdn.microsoft.com/en-us/library/x93ctkx8%28v=VS.80%29.aspx">http://msdn.microsoft.com/en-us/library/x93ctkx8%28v=VS.80%29.aspx</a>)<br />
But the fact that there&#8217;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.
</p>
<p align="justify">
So how do we make it work?<br />
My first idea was of using that &#8220;Browse to source&#8221; 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&#8217;s now clear why it&#8217;s always grayed out.
</p>
<p align="justify">
So one solution is to use Reflector Pro, which is commercial, and use it&#8217;s runtime debugging feature to step into microsoft .net framework source code.<br />
This is not an ideal solution (especially because of the optimized code), and I just don&#8217;t understand why Microsoft doesn&#8217;t publish automatically source code according to their latest hotfixes/patches of the .net framework :(<br />
I mean, I need to step into .net framework because I&#8217;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&#8217;t install any hotfixes? :(<br />
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)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/238/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=238&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/05/17/net-framework-stepping-problems/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2011/05/pdbloadinfo.png" medium="image">
			<media:title type="html">pdbloadinfo</media:title>
		</media:content>
	</item>
		<item>
		<title>Comparing object properties in C#</title>
		<link>http://liviutrifoi.wordpress.com/2011/04/28/c-deep-copy-object-property-comparison/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/04/28/c-deep-copy-object-property-comparison/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 18:20:58 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[unit-testing]]></category>
		<category><![CDATA[deep-object-property-comparison]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=211</guid>
		<description><![CDATA[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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=211&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I needed today, in my unit tests, a way to compare two objects of the same type, property by property.<br />
In addition to this I needed the following:</p>
<ul>
-If the property is a reference type, then go inside it and compare that too, property by property. (Basically deep object property comparison).<br />
-If the property is IEnumerable, then compare properties of each element inside the IEnumerable.<br />
-If the elements inside the IEnumerable are themselves IEnumerable then this could go on many levels deep, but this was not on my requirements.<br />
-I also want the possibility to ignore certain properties in the comparison, such as property which is a delegate (like the <em>Filter</em> property of the <em>PagedCollectionView</em> framework class).<br />
And of course I would need to know which properties were not equal, so I could show into my assertions.
</ul>
<p>Obviously this involves some heavy reflection code which I&#8217;m sure it would be fun to write if you had the time.<br />
An alternative is to use a free library, with just one class, that does exactly this: <a href="http://comparenetobjects.codeplex.com/">http://comparenetobjects.codeplex.com/</a><br />
Usage is very simple, and supports all kind of useful features which you can see below:</p>
<p><pre class="brush: csharp;">
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&lt;string&gt;() { &quot;Filter&quot; }
            };
                                                                            
            bool areEqual = compareObjects.Compare(objectA, objectB);
            Assert.IsTrue(areEqual, compareObjects.DifferencesString); //DifferencesString contains exactly what property and what value(s) was different
</pre></p>
<p>For Silverlight you do have to remove a couple of line of codes from it, related to DataTable comparison, which don&#8217;t exist in silverlight, but it&#8217;s easy and only takes a couple of minutes.</p>
<p>Happy Deep Object Comparisons :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=211&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/04/28/c-deep-copy-object-property-comparison/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight stack trace line numbers</title>
		<link>http://liviutrifoi.wordpress.com/2011/04/21/silverlight-stack-trace-line-numbers/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/04/21/silverlight-stack-trace-line-numbers/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 18:15:05 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Line Numbers]]></category>
		<category><![CDATA[Stack Trace]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=175</guid>
		<description><![CDATA[As you probably know, to have stack traces line numbers in a .net app you need pdb files. In Silverlight though, pdb files don&#8217;t get included in the XAP. Even if it was possible to include them you probably wouldn&#8217;t want them there because they tend to be huge and you want to have a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=175&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
As you probably know, to have stack traces line numbers in a .net app you need pdb files.<br />
In Silverlight though, pdb files don&#8217;t get included in the XAP. Even if it was possible to include them you probably wouldn&#8217;t want them there because they tend to be huge and you want to have a fast loading web-site.</p>
<p>So what will you do when your client calls you and says he received an error message like the one below?<br />
<em>NullReferenceException<br />
at ChaptersView.ChaptersTree_OnMouseMove<br />
at ChaptersView.GetPreviousCommandButton  </em></p>
<p>After I had a look today at the answer to this <a href="http://stackoverflow.com/questions/1037925/recreate-stack-trace-with-line-numbers-from-user-bug-report-in-net">question</a>, it became obvious:</p>
<p><strong>Step 1)</strong><br />
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.<br />
You should write a stacktrace that includes IL offsets using the code below:</p>
<p><pre class="brush: csharp;">
...
catch (Exception exception)
{
            string errorDescription = string.Empty;
            try
            {
                StackTrace st = new System.Diagnostics.StackTrace(ex);
                string stackTrace = &quot;&quot;;

                if (st != null)
                {
                    foreach (StackFrame frame in st.GetFrames())
                    {
                        stackTrace = &quot;at &quot; + frame.GetMethod().Module.Name + &quot;.&quot; + frame.GetMethod().ReflectedType.Name +
                                     &quot;.&quot; + frame.GetMethod().Name + &quot;  (IL offset: 0x&quot; +
                                     frame.GetILOffset().ToString(&quot;x&quot;) + &quot;)\n&quot; + 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  
}
</pre> </p>
<p>If we pass the exception that generated the stacktrace in our first example, it will look like:</p>
<p><em>NullReferenceException<br />
at ChaptersView.ChaptersTree_OnMouseMove  (IL offset: 0&#215;44)<br />
at ChaptersView.GetPreviousCommandButton  (IL offset: 0&#215;28)<br />
</em></p>
<p><strong>Step 2: Client sends you an exception/log that includes IL offsets</strong><br />
You can use Reflector or IL Disassembler (which comes with visual studio) to find out the piece of code that generated the exception.<br />
I prefer IL Disassembler because Reflector will only show you IL code from those IL offsets, and there&#8217;s a large chance you don&#8217;t read IL as well as you read C#.</p>
<p>So you have to perform the following steps:<br />
1) Go to start menu and start IL Disassembler<br />
2) Inside IL Disassembler go to File-&gt;Open and load the dll (not the XAP) where the exception was thrown. (e.g. &#8230;\Release\Client.dll)<br />
3) Go to View-&gt;Show source lines<br />
4) Now navigate to the last method that appears in the stacktrace and we look after the IL offset from the stacktrace.<br />
In our example above this is <em>ChaptersView.GetPreviousCommandButton</em> and has offset 0&#215;28</p>
<p>Inside IL Disassembler we will see a mix of C# code and corresponding IL code:</p>
<p><pre class="brush: csharp;">
  IL_001c:  brtrue.s   IL_0029
//000193:             {
//000194:                 throw new NullReferenceException(“Test Exception”);
  IL_001e:  ldstr      &quot;Test Exception&quot;
  IL_0023:  newobj     instance void [mscorlib]System.Exception::.ctor(string)
  IL_0028:  throw
//000195:             }
//000196:
//000197:             var previousCommandButton =
 
</pre></p>
<p>As you can see, at IL_0028 there&#8217;s the instruction that caused the exception.<br />
This is IL code, but because we&#8217;ve checked &#8220;View-&gt;Show source lines&#8221; we also get the corresponding C# code just a few lines above.</p>
<p>//000193:             {<br />
//000194:                 throw new NullReferenceException();</p>
<p>So, in this case the exception was thrown because I&#8217;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.</p>
<p>Also please don&#8217;t just put that piece of code in every catch block. Create a method like &#8220;string GetStackTraceFromException(Exception ex)&#8221;<br />
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.<br />
So by the time the client calls you, you have fixed the problem and he&#8217;s impressed ;)</p>
<p>Happy Debugging.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=175&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/04/21/silverlight-stack-trace-line-numbers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a zip file with a folder in Silverlight</title>
		<link>http://liviutrifoi.wordpress.com/2011/03/22/silverlight-create-zip-file-with-folder/</link>
		<comments>http://liviutrifoi.wordpress.com/2011/03/22/silverlight-create-zip-file-with-folder/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 12:09:16 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Folder]]></category>
		<category><![CDATA[SharpZipLib]]></category>
		<category><![CDATA[Zip]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=195</guid>
		<description><![CDATA[If you happen to have a silverlight application that sends/receives files from the server side it&#8217;s a good idea to have those files compressed Unless the files are already in some compressed format like (png, jpg, etc), this will reduce the amount of data sent through the network minimizing transfer times considerably. So for textual [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=195&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
If you happen to have a silverlight application that sends/receives files from the server side it&#8217;s a good idea to have those files compressed<br />
Unless the files are already in some compressed format like (png, jpg, etc), this will reduce the amount of data sent through the network minimizing transfer times considerably.<br />
So for textual file formats like xml, txt, etc I definitely recommend zipping them.<br />
For zipping in silverlight I found two options at the time of this writing:<br />
 1) Use the System.IO.Compression.GZipStream<br />
 2) Use the <a href="http://slsharpziplib.codeplex.com/">SharpZipLib</a> ported to Silverlight library.</p>
<p>I wouldn&#8217;t recommend the first option but for the simplest scenarios like: zip one file to one one compressed file.<br />
If you need to create a zip archive that can contain multiple files &amp; folders which are taken from the file-system or from in-memory content then I recommend the second option.<br />
The only problem I had using option 2 is that documentation for sharpziplib is scarce, but persistent google searching does help.</p>
<p>So let&#8217;s take an example scenario and see how it&#8217;s done using SharpZipLib in Silverlight.<br />
Suppose you have an in-memory xml, stored as a string, and the end result you want to achieve is an archive called <em>test.zip</em> which contains a single folder called <em>Test</em>.<br />
Inside the folder you want to have the in-memory xml stored into an xml file called <em>test.xml</em>.<br />
To see the end result easily we&#8217;ll write the archive to isolated storage.</p>
<p>So here goes the source code for the method that will do the creation of the archive:<br />
<pre class="brush: csharp;">
        public byte[] GetCompressedByteArray(string content)
        {
            byte[] compressedResult;
            using (MemoryStream zippedMemoryStream = new MemoryStream())
            {
                using (ZipOutputStream zipOutputStream = new ZipOutputStream(zippedMemoryStream))
                {
                    // Highest compression rating
                    zipOutputStream.SetLevel(9);

                    byte[] buffer;

                    // The string to use as file content
                    using (MemoryStream file = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        buffer = new byte[file.Length];
                        file.Read(buffer, 0, buffer.Length);
                    }

                    // Write the data to the ZIP file            
                    ZipEntry entry = new ZipEntry(&quot;test/test.xml&quot;);
                    zipOutputStream.PutNextEntry(entry);
                    zipOutputStream.Write(buffer, 0, buffer.Length);

                    zipOutputStream.Finish();
                }

                compressedResult = zippedMemoryStream.ToArray();
            }
</pre></p>
<p>In the code above you can see how easily it is to create a virtual folder with a file in in a zip archive: <em>ZipEntry entry = new ZipEntry(&#8220;test/test.xml&#8221;);</em>.<br />
Don&#8217;t be mislead though, although winrar and winzip will show you folders inside your zip archive, if you copy the archive to a mac/linux/android device you&#8217;ll still see only a file called &#8220;test/test.xml&#8221; since there the &#8220;/&#8221; doesn&#8217;t mean anything.<br />
I haven&#8217;t had time to investigate this further but there&#8217;s some class <em>ZipEntryFactory zef = new ZipEntryFactory().MakeDirectoryEntry();</em> which I think should be used for making directory entries.</p>
<p>So this <em>GetCompressedByteArray</em> method will return us a compressed byte array.<br />
All we need to do now is to write it to isolated storage:</p>
<p><pre class="brush: csharp;">
        public void WriteToIsolatedStorage(byte[] compressedBytes)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream zipTemplateStream = new IsolatedStorageFileStream(&quot;test.zip&quot;, FileMode.OpenOrCreate, isoStore))
            using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream))
            {
                streamWriter.Write(compressedBytes);
            }

        }
</pre></p>
<p>And write some test code:</p>
<p><pre class="brush: csharp;">
          var xmlString =
@&quot;&lt;?xml version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;utf-8&quot;&quot; standalone=&quot;&quot;no&quot;&quot;?&gt;
&lt;book&gt;
	&lt;version&gt;1&lt;/version&gt;
	&lt;language&gt;english&lt;/language&gt; 	
	&lt;chapters&gt;
       &lt;chapter name=&quot;&quot;chapter1&quot;&quot; /&gt;
       &lt;chapter name=&quot;&quot;chapter2&quot;&quot; &gt;
    &lt;/chapters&gt;
&lt;/book&gt;&quot;;
            byte[] compressedXmlStringBytes = GetCompressedByteArray(xmlString);
            WriteToIsolatedStorage(compressedXmlStringBytes);
</pre></p>
<p>After running it, you should get a &#8220;test.zip&#8221; inside the isolated storage.<br />
On Windows 7 the isolated storage is located at: <em>c:\Users\username\AppData\LocalLow\Microsoft\Silverlight\. </em><br />
Just search for a file named &#8220;test.zip&#8221;, inside the folders and subfolders at that location.</p>
<p>If you want to see the full actual, without searching for it, the only way is to  use the visual studio debugger:<br />
Put a breakpoint on <em>using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream))</em>, then move the mouse over <em>zipTemplateStream</em>, non-public members, and look at <em>m_FullPath</em>, like in the image below:<br />
<a href="http://liviutrifoi.files.wordpress.com/2011/05/isopath.png" title="isopath" target="_blank">See Image</a></p>
<p>Also see <a href="https://rapidshare.com/files/458636961/SilverlightApplicationZipTest.zip">here </a>the attached VS2010 solution.</p>
<p>For the unzipping part you can take a look at the<a href="http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx#Unpack_a_Zip_with_full_control_over_the_operation_3"> Unpack a zip with full control over the operation</a></p>
<p>Happy zipping :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=195&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2011/03/22/silverlight-create-zip-file-with-folder/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Process Monitor to debug applications</title>
		<link>http://liviutrifoi.wordpress.com/2010/06/21/using-process-monitor-to-debug-applications/</link>
		<comments>http://liviutrifoi.wordpress.com/2010/06/21/using-process-monitor-to-debug-applications/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 17:42:20 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Capicom]]></category>
		<category><![CDATA[File Not Found]]></category>
		<category><![CDATA[process monitor]]></category>
		<category><![CDATA[registry]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=136</guid>
		<description><![CDATA[Every once in a while there comes a bug that requires the use of Process Monitor Process monitor is great but sometimes it is difficult to correlate its event stream with the source code you are debugging. Using it for quite some time, I don&#8217;t really read it&#8217;s help anymore, but accidentally I&#8217;ve looked into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=136&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">Every once in a while there comes a bug that requires the use of <a>Process Monitor</a><br />
Process monitor is great but sometimes it is difficult to correlate its event stream with the source code you are debugging.</p>
<p>Using it for quite some time, I don&#8217;t really read it&#8217;s help anymore, but accidentally I&#8217;ve looked into it recently and was pleasantly surprised to see  that it now supports injecting debug messages into it&#8217;s event stream using an API and there&#8217;s even a managed wrapper for this (read <a href="http://www.wintellect.com/CS/blogs/jrobbins/archive/2010/04/13/see-the-i-o-you-caused-by-getting-your-diagnostic-tracing-into-process-monitor.aspx">diagnostic tracing process monitor</a>)<br />
A few of my colleagues asked me for scenarios in which to use it, so I&#8217;ll describe a few from my personal experience.</p>
<p>I mainly used it for debugging bugs that involve the file system or registry in some way.</p>
<p><strong>Example 1: </strong></p>
<p>I was maintaining a real-time legacy system that was doing a series of processing on xml files. Each processing involved creating  a temporary file in a certain directory. The problem was that sometimes certain files were lost.
</p>
<p>Code looked similar to this:<br />
<pre class="brush: csharp;">
string filename = string.Format(“{0}-{1}.xml”,
DateTime.Now.ToString(“yyyy-MM-dd-HH-mm”), Environment.TickCount);
filename = Path.Combine(dir, filename);
// synchronize access to file
lock (m_cs.MainAppDomain)
{
	using (StreamWriter sw = File.CreateText(filename))
	{
		sw.Write(content);
		sw.Close();
	}
}
</pre></p>
<p align="justify">
Can you spot the problem? I stepped through the code many times, and I wasn’t able to reproduce the bug.</p>
<p>After using Process Monitor I was able to spot it: the unique filename made from the number of miliseconds since the system has started is not really unique. In some cases more than 1 file/milisecond can be created, in which case the second file overwrites the first one.
</p>
<p class="MsoNormal"><strong>Example 2: </strong></p>
<p align="justify">
I had to do production debugging on a system accessible via Remote Desktop (via a special Remote Desktop infrastructure), but located behind an inaccessible subnet. So remote debugging was out of the question. It wouldn’t have helped anyway for this particular bug.</p>
<p>I had a piece of code that was installing an X.509 Certificate which was working under one Administrator user, but wasn’t working under another Administrator user.</p>
<p>The error I was receiving was “File not found exception” (the filename not being found was an empty string). Exception stacktrace was ending in an internal CAPICOM library (windows library).</p>
<p>At first I thought it was a security issue, so I enabled Windows security auditing but no security events were being logged, so this wasn’t a security issue.</p>
<p>So what I did was: take the output of process monitor when code was running fine and take the output of process monitor when the code was not working.</p>
<p>I compared these two and it revealed that, when it was not working, code didn’t found an AppData registry values in<br />
HKEY_USERS\S-1-5-21-218976661-61539242-33973980-1010\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\</p>
<p>AppData normally points to the Application Data environment folder and should have been there. Seems somebody corrupted the registry, because lots of other shell folder values were missing too and is used by CAPICOM to locate the physical certificate store when installing a new certificate.</p>
<p>So I manually re-created the missing values by looking at a working system, and everything worked.</p>
<p>So registry corruption is another good scenario where you can use it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=136&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2010/06/21/using-process-monitor-to-debug-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>AppDomain.Unload Garbage Collection</title>
		<link>http://liviutrifoi.wordpress.com/2010/05/21/appdomain_unload-garbage-collection/</link>
		<comments>http://liviutrifoi.wordpress.com/2010/05/21/appdomain_unload-garbage-collection/#comments</comments>
		<pubDate>Fri, 21 May 2010 17:24:11 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[AppDomain]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[C# AppDomain Unloading]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=119</guid>
		<description><![CDATA[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&#8217;t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=119&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
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.</p>
<p>The components performed their jobs in the isolated environment an AppDomain provides and, when their job finished, they were unloaded using AppDomain.Unload.</p>
<p>The problem was that memory didn&#8217;t seem to be recollected when I was doing AppDomain.Unload so I turned to read again sections from Jeffrey Richter&#8217;s, <a href="http://www.amazon.com/CLR-via-Dev-Pro-Jeffrey-Richter/dp/0735627045/" target="_blank">CLR via C# 3rd edition</a>, since that was the book from which I&#8217;ve learned about Application Domains and Garbage Collection. It stated clearly<em> &#8220;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.&#8221;</em></p>
<p>After some mail exchange with Jeffrey, which you can read, along with an example <a href="http://stackoverflow.com/questions/2723579/why-does-calling-appdomain-unload-doesnt-result-in-a-garbage-collection" target="_blank">here</a>, he suggested:</p>
<p><em>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.<br />
</em></p>
<p>To keep it short I had a small program that did this:</p>
<p>1) Create and application domain,</p>
<p>2) Create some object instances inside so It has some allocated memory.</p>
<p>2) Call AppDomain.Unload</p>
<p>3) Perform an explicit GC.Collect() call.</p>
<p>I took Visual Studio and loaded <a href="http://msdn.microsoft.com/en-us/library/bb190764.aspx" target="_blank">Son of Strike</a> into it and it revealed this:</p>
<p>Before AppDomain.Unload:</p>
<p><em>!EEHeap -gc<br />
Number of GC Heaps: 1<br />
generation 0 starts at 0x0180b1f0<br />
generation 1 starts at 0x017d100c<br />
generation 2 starts at 0x017d1000<br />
ephemeral segment allocation context: none<br />
segment    begin allocated     size<br />
017d0000 017d1000  01811ff4 0x00040ff4(266228)<br />
Large object heap starts at 0x027d1000<br />
segment    begin allocated     size<br />
027d0000 027d1000  02f75470 0x007a4470(8012912)<br />
Total Size  0x7e5464(8279140)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
GC Heap Size  0x7e5464(8279140)</em></p>
<p>After AppDomain.Unload (same addresses, no heap compaction was done)</p>
<p><em>!EEHeap -gc<br />
Number of GC Heaps: 1<br />
generation 0 starts at 0x0180b1f0<br />
generation 1 starts at 0x017d100c<br />
generation 2 starts at 0x017d1000<br />
ephemeral segment allocation context: none<br />
segment    begin allocated     size<br />
017d0000 017d1000  01811ff4 0x00040ff4(266228)<br />
Large object heap starts at 0x027d1000<br />
segment    begin allocated     size<br />
027d0000 027d1000  02f75470 0x007a4470(8012912)<br />
Total Size  0x7e5464(8279140)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
GC Heap Size  0x7e5464(8279140)</em></p>
<p>After GC.Collect(), addresses differ indicating heap compaction was done.</p>
<p><em>!EEHeap -gc<br />
Number of GC Heaps: 1<br />
generation 0 starts at 0&#215;01811234<br />
generation 1 starts at 0x0180b1f0<br />
generation 2 starts at 0x017d1000<br />
ephemeral segment allocation context: none<br />
segment    begin allocated     size<br />
017d0000 017d1000  01811ff4 0x00040ff4(266228)<br />
Large object heap starts at 0x027d1000<br />
segment    begin allocated     size<br />
027d0000 027d1000  027d3240 0&#215;00002240(8768)<br />
Total Size   0&#215;43234(274996)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
GC Heap Size   0&#215;43234(274996)</em></p>
<p>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.</p>
<p>Memory for those objects will be reclaimed whenever the next garbage collection happens, so, in this case it&#8217;s not a good idea to call for GC.Collect() yourself.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=119&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2010/05/21/appdomain_unload-garbage-collection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>
	</item>
		<item>
		<title>What is an Application Domain and what is it used for?</title>
		<link>http://liviutrifoi.wordpress.com/2010/04/21/what-is-an-application-domain-and-what-is-it-used-for/</link>
		<comments>http://liviutrifoi.wordpress.com/2010/04/21/what-is-an-application-domain-and-what-is-it-used-for/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 17:26:46 +0000</pubDate>
		<dc:creator>Liviu Trifoi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C# AppDomain tutorial]]></category>
		<category><![CDATA[Marshalling MarshalByRef]]></category>

		<guid isPermaLink="false">http://liviutrifoi.wordpress.com/?p=126</guid>
		<description><![CDATA[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&#8217;t familiar with other basic topics like communication between AppDomains and memory recollection. So, in this post I&#8217;ll share what I&#8217;ve learned [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=126&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
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&#8217;t familiar with other basic topics like communication between AppDomains and memory recollection. So, in this post I&#8217;ll share what I&#8217;ve learned on the topic.</p>
<p>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).</p>
<p><img src="/Users/LIVIU%7E1.TRI/AppData/Local/Temp/moz-screenshot.png" alt="" /></p>
<p><a href="http://liviutrifoi.files.wordpress.com/2010/10/applicationdomains.png"><img class="alignnone size-full wp-image-127" title="Application Domains Abstraction" src="http://liviutrifoi.files.wordpress.com/2010/10/applicationdomains.png?w=700" alt="ApplicationDomains Image"   /></a></p>
<p>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).</p>
<p>Next, let&#8217;s say you want your SampleConsoleApplication to use classes from another library, <em>ClassLibrary1</em>. Normally you would just reference <em>ClassLibrary1.</em> But there are scenarios where you don&#8217;t want to do that. A few scenarios I can think is:</p>
<ol>
<li>You have an application that supports plug-ins (e.g. MMC, SAP, etc). Presume  <em>ClassLibrary1</em> is a plug-in written by a 3rd Party. You have to isolate <em>ClassLibrary1</em> from a security and reliability point of view. From a security point of view you don&#8217;t want <em>ClassLibrary1</em> to access your directly code (MMC/SAP code in this case) because it might be malicious. From reliability point of view, even if <em>ClassLibrary1 </em>is not malicious it still might have bugs in implementation that you couldn&#8217;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&#8217;t use <em>ClassLibrary1</em> classes anymore.</li>
<li>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.</li>
<li>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.</li>
</ol>
<p>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.</p>
<p>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. (<a href="http://stackoverflow.com/questions/1294494/is-net-remoting-really-deprecated">See http://stackoverflow.com/questions/1294494/is-net-remoting-really-deprecated</a>)</p>
<p>If you&#8217;re stuck with maintaining a legacy system that uses remoting keep on reading.</p>
<p>If you&#8217;re using remoting, there are two important ways by which you can send one object from one AppDomain to another.</p>
<ol>
<li>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.</li>
<li>Marshaling by Reference: In some cases it doesn&#8217;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 <em>TransparentProxy</em> is created for it. For example you have a <em>DataAccess</em> class that contains a method <em>InsertPerson(string name) </em>that opens a db connection and inserts a person in a database<em>.</em> 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 <em>TransparentProxy</em>. When you call <em>InsertPerson </em>on it, it will make a remote call to an instance of type <em>DataAccess</em> living in the first <em>AppDomain, </em>which will perform the operation and send the result back. (it&#8217;s enough if you imagine the remote call as web-service call).</li>
</ol>
<p>I hope this abstraction suffices your needs.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/liviutrifoi.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/liviutrifoi.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/liviutrifoi.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=liviutrifoi.wordpress.com&amp;blog=6373551&amp;post=126&amp;subd=liviutrifoi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://liviutrifoi.wordpress.com/2010/04/21/what-is-an-application-domain-and-what-is-it-used-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd071c02d4b0aa858873cc2afe698fa?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">liviutrifoi</media:title>
		</media:content>

		<media:content url="http://liviutrifoi.files.wordpress.com/2010/10/applicationdomains.png" medium="image">
			<media:title type="html">Application Domains Abstraction</media:title>
		</media:content>
	</item>
	</channel>
</rss>
