<?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/"
	>

<channel>
	<title>Florent Clairambault &#187; C# .Net</title>
	<atom:link href="http://florent.clairambault.fr/tag/c-net/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Wed, 28 Jul 2010 20:31:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Technology brotherhoods</title>
		<link>http://florent.clairambault.fr/technology-brotherhoods?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=technology-brotherhoods</link>
		<comments>http://florent.clairambault.fr/technology-brotherhoods#comments</comments>
		<pubDate>Tue, 06 Jul 2010 21:00:02 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[productivity]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2870</guid>
		<description><![CDATA[I often hear people talking about which technology is better between C / C++ / C# .Net / java. Most of the time, it&#8217;s more a political/brotherhood/community thing (like football, even thought frenchies like me aren&#8217;t so proud of their team now) than a technical talk. I find it absurd. Computer science is about efficiency [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://i.mivi.fr/08d1a596d5f04e8a326679be679c2c76934ed624.x.300.jpg" style="position: relative; float: right; margin-left: 10px"/><br />
I often hear people talking about which technology is better between C / C++ / C# .Net / java. Most of the time, it&#8217;s more a political/brotherhood/community thing (like football, even thought frenchies like me aren&#8217;t so proud of their team now) than a technical talk.<br />
I find it absurd. Computer science is about efficiency and making more money. You can take into account the pleasure you have to work on a technology (as you will be more likely to be more efficient with it), but it should still remain about efficiency.</p>
<p>The following video is about java &#038; C# .Net. I&#8217;ve done quite a lot of C# .Net, Java and C, and some ObjectiveC (while developing iPhone applications) and a little bit of C++. The only thing I <strong>feel</strong> is that Sun with java had the good ideas, Microsoft with .Net took them and finished the work. Still, I often code in java, mostly for the TC65 chip, it&#8217;s quite comfortable. But for instance, it was longer to implement the <a href="http://florent.clairambault.fr/tag/m2mp">M2MP protocol</a> on the <a href="http://florent.clairambault.fr/tag/tc65">TC65</a> chip than the C# .net server. Mostly because of the unsigned types I had to &#8220;emulate&#8221; and the lack of generics (which are not available on the TC65&#8242;s JVM).</p>
<p>By the way, I&#8217;m running .Net apps on Linux everyday using <a href="http://www.mono-project.com/">Mono</a>. </p>
<p>Here is the best example of what I&#8217;m talking about :</p>
<div style="text-align: center">
<video src="http://florent.clairambault.fr/downloads/video/java4ever.mp4" controls="controls" />
</div>
<p>For all these lame people who don&#8217;t have an HTML5 compatible browser, you can go to <a href="http://www.youtube.com/watch?v=9g1y7f1xjJ0">the original video</a>. The reason I downloaded it here in my blog is that you are forced to register to YouTube to watch it. </p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/technology-brotherhoods/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
<enclosure url="http://florent.clairambault.fr/downloads/video/java4ever.mp4" length="12992699" type="video/mp4" />
		</item>
		<item>
		<title>Recycling .net objects to improve performances</title>
		<link>http://florent.clairambault.fr/recycling-net-objects-to-improve-performances?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=recycling-net-objects-to-improve-performances</link>
		<comments>http://florent.clairambault.fr/recycling-net-objects-to-improve-performances#comments</comments>
		<pubDate>Tue, 15 Jun 2010 20:00:02 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2792</guid>
		<description><![CDATA[C# .Net allocation and freeing system is quite efficient but when you need to create a huge number of objects, it&#8217;s just not fast enough. So what you can do is try to recycle each object to avoid to recreate them. You will then just need to set their property. In my tests, this class [...]]]></description>
			<content:encoded><![CDATA[<p>C# .Net allocation and freeing system is quite efficient but when you need to create a huge number of objects, it&#8217;s just not fast enough. So what you can do is try to recycle each object to avoid to recreate them. You will then just need to set their property. </p>
<p>In my tests, this class reduced the allocation time from 12 to 15 times:</p>

<div class="wp_codebox"><table><tr id="p27922"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p2792code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Heap<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T<span style="color: #008000;">:</span><span style="color: #6666cc; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Stack<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> _stack <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Stack<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _count<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> T Allocate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		var obj <span style="color: #008000;">=</span> _count <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">?</span> _stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> obj<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Free<span style="color: #008000;">&#40;</span> T obj <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #008000;">++</span>_count<span style="color: #008000;">;</span>
		_stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span> obj <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>You might think that this process is really painful as you have to manually free every single objects you use. But in fact you just have to recycle most of the objects. If you forget some, that&#8217;s not really important (they will just get garbage collected).</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/recycling-net-objects-to-improve-performances/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google PowerMeter and sample C# .Net API usage</title>
		<link>http://florent.clairambault.fr/google-powermeter-and-sample-c-net-api-usage?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=google-powermeter-and-sample-c-net-api-usage</link>
		<comments>http://florent.clairambault.fr/google-powermeter-and-sample-c-net-api-usage#comments</comments>
		<pubDate>Sat, 29 May 2010 17:00:18 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google Power]]></category>
		<category><![CDATA[green]]></category>
		<category><![CDATA[Z-wave]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2723</guid>
		<description><![CDATA[Google PowerMeter offers you to collect your home&#8217;s / enterprise&#8217;s / specific equipment&#8217;s power usage and have some basic analysis on it. There&#8217;s also a Google PowerMeter API so that everyone can build devices able to report their consumption and applications. I would say one cheap and evolutive way to implement this API would be [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://i.mivi.fr/acdeb2ba6ad51309f5d9f6eafbe3ad7566f30afa.x.300.jpg" style="position: relative; float: right; margin-left: 10px" /></p>
<p><a href="http://www.google.com/powermeter/about/about.html">Google PowerMeter</a> offers you to collect your home&#8217;s / enterprise&#8217;s / specific equipment&#8217;s power usage and have some basic analysis on it.</p>
<p>There&#8217;s also a <a href="http://code.google.com/intl/ro-RO/apis/powermeter/">Google PowerMeter API</a> so that everyone can build devices able to report their consumption and applications.</p>
<p>I would say one cheap and evolutive way to implement this API would be to use a Z-Wave power meter and have a Z-Wave central controller sent the data to Google servers. It could quickly get interesting because this could make you know the exact consumption of all your lights by calculating the result of the light-on/light-off order on individual lights. And this could even lead to define some max energy consumption scenarios so that you don&#8217;t allow more than X watts for all the lights of your house (this is just a idea, I would never put this in my home).</p>
<p>I know this isn&#8217;t the next BIG thing. Because everyone knows it&#8217;s all about the social right-now. But still, I think this could be the most green project of all time. Because it clearly shows people (or enterprises) how they consume energy and gives the useful analysis to find solutions. And for once it doesn&#8217;t try to make you feel guilty about not being green enough, it just brings facts.</p>
<p>By the way, you can have a negative energy consumption (power meter counting backward) in Google PowerMeter. I don&#8217;t think this is a bug, it must be for people selling their energy for their solar or wind energy producer equipments. But unfortunately you set the estimated cost of energy but you can&#8217;t set its estimated revenue price.</p>
<h3>Sample API call</h3>
<p>Here is a sample Google PowerMeter API call in C# .Net :<br />
<em>I just changed a little bit of the authentication parameters so that you can&#8217;t actually upload data on my account.</em></p>

<div class="wp_codebox"><table><tr id="p27234"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
</pre></td><td class="code" id="p2723code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Globalization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/* Equipment was activated with this URL :
 * https://www.google.com/powermeter/device/activate?mfg=WebIngenia&amp;model=TestEquipment1&amp;did=100102030405&amp;cvars=1&amp;dvars=0&amp;rurl=http://test.webingenia.com/powerMeter&amp;snonce=123
 *
 * The resulting request was (available here : http://test.webingenia.com/read/powerMeter#id2473 ) :
 * $_POST = array(
 *		[snonce] =&gt; 123
 *		[hash] =&gt; _____6c2562aeb7bab4e24afc7beeb18c701c04ba,____8c9f62fa8a137eba7fa35b5f58802fca2e56,____f4db13304c49d36fb81b20cabbe147f8ce34
 *		[token] =&gt; ________FhCtzebg_P____8BGMWjtUs
 *		[path] =&gt; /user/____4578579014915368/____4578579014915368/variable/WebIngenia.TestEquipment1.100102030405
 * )
 *
 */</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> test <span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">class</span> Program <span style="color: #008000;">&#123;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Random _random <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Random<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> DeviceHash <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// Useless</span>
			<span style="color: #666666;">&quot;____bf8262aeb7bab4e24afc7beeb18c701c04ba,____8c9f62fa8a137eba7fa35b5f58802fca2e56,____f4db13304c49d36fb81b20cabbe147f8ce34&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> DeviceToken <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// Needed for authentication</span>
			<span style="color: #666666;">&quot;________FhCtzebg_P____8BGMWjtUs&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> DevicePath <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// URL of the device</span>
			<span style="color: #666666;">&quot;/user/____4578579014915368/_____4578579014915368/variable/WebIngenia.TestEquipment1.100102030405&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> DeviceVar <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;c1&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> FeedUrl <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// URL of the Google PowerMeter API</span>
			<span style="color: #666666;">&quot;https://www.google.com/powermeter/feeds&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">String</span> EventUrl <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// URL where to send the data</span>
			FeedUrl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/event&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span> Period <span style="color: #008000;">=</span> <span style="color: #FF0000;">5000</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// 5 to 10 seconds</span>
&nbsp;
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> MeterValue <span style="color: #008000;">&#123;</span>
			get <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">return</span> Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MeterValue</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			set <span style="color: #008000;">&#123;</span>
				Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MeterValue</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
				Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Send<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// We increment the counter (by 0 to 1 kWh)</span>
			MeterValue <span style="color: #008000;">+=</span> _random<span style="color: #008000;">.</span><span style="color: #0000FF;">NextDouble</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;MeterValue : {0}&quot;</span>, MeterValue <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// This is what we are going to send</span>
			var strContent <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>
			<span style="color: #666666;">@&quot;
&lt;feed xmlns=&quot;</span><span style="color: #666666;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #666666;">&quot; xmlns:meter=&quot;</span><span style="color: #666666;">&quot;http://schemas.google.com/meter/2008&quot;</span><span style="color: #666666;">&quot;&gt;
	&lt;entry&gt;
      &lt;category scheme=&quot;</span><span style="color: #666666;">&quot;http://schemas.google.com/g/2005#kind&quot;</span><span style="color: #666666;">&quot; term=&quot;</span><span style="color: #666666;">&quot;http://schemas.google.com/meter/2008#instMeasurement&quot;</span><span style="color: #666666;">&quot; /&gt;
      &lt;meter:subject&gt;{0}&lt;/meter:subject&gt;
      &lt;meter:occurTime meter:uncertainty=&quot;</span><span style="color: #666666;">&quot;1.0&quot;</span><span style="color: #666666;">&quot;&gt;{1}&lt;/meter:occurTime&gt;
      &lt;meter:quantity meter:uncertainty=&quot;</span><span style="color: #666666;">&quot;0.001&quot;</span><span style="color: #666666;">&quot; meter:unit=&quot;</span><span style="color: #666666;">&quot;kW h&quot;</span><span style="color: #666666;">&quot;&gt;{2}&lt;/meter:quantity&gt;
    &lt;/entry&gt;
&lt;/feed&gt;&quot;</span>,
				FeedUrl <span style="color: #008000;">+</span> DevicePath <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.&quot;</span> <span style="color: #008000;">+</span> DeviceVar,						<span style="color: #008080; font-style: italic;">// Device URL</span>
				DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">UtcNow</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;yyyy-MM-dd'T'HH:mm:ss.fffK&quot;</span> <span style="color: #008000;">&#41;</span>,	<span style="color: #008080; font-style: italic;">// Current UTC date</span>
				MeterValue<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span> CultureInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">InvariantCulture</span> <span style="color: #008000;">&#41;</span>			<span style="color: #008080; font-style: italic;">// Meter value</span>
			<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// We convert it into raw content</span>
			var content <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span> strContent <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// We create the POST request</span>
			var req <span style="color: #008000;">=</span> WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span> EventUrl <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			req<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Authorization&quot;</span>, <span style="color: #666666;">&quot;AuthSub token=<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> DeviceToken <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			req<span style="color: #008000;">.</span><span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span>
			req<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> content<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
			req<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/atom+xml&quot;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">// Opening the request stream means starting the request</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Requesting...&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> var requestStream <span style="color: #008000;">=</span> req<span style="color: #008000;">.</span><span style="color: #0000FF;">GetRequestStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
					<span style="color: #008080; font-style: italic;">// We send the content</span>
					requestStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span> content, <span style="color: #FF0000;">0</span>, content<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
					<span style="color: #008080; font-style: italic;">// And we end the request by getting the response</span>
					<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> var streamReader <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span> req<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
						Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Result : &quot;</span> <span style="color: #008000;">+</span> streamReader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008000;">&#125;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> WebException e <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// If a webException occurs (should be 400)</span>
				<span style="color: #008080; font-style: italic;">// We display the error</span>
				var response <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span> <span style="color: #0600FF; font-weight: bold;">as</span> HttpWebResponse<span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> response <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080; font-style: italic;">// This is not the exception that we want</span>
					<span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> response <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
						Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>
							<span style="color: #666666;">&quot;Error {0} : {1} &quot;</span>,
							response<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span>,
							<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span> responseStream <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
						<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008000;">&#125;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
					Send<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception ex <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Exception : &quot;</span> <span style="color: #008000;">+</span> ex <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span> Period <span style="color: #008000;">+</span> _random<span style="color: #008000;">.</span><span style="color: #0000FF;">Next</span><span style="color: #008000;">&#40;</span> Period <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// This code was generated by Visual Studio (but I prefer to put it here rather than having to publish the VS project)</span>
		<span style="color: #008080;">#region Auto-generated</span>
		<span style="color: #008000;">&#91;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Runtime</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompilerServices</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompilerGeneratedAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
		<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">CodeDom</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Compiler</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">GeneratedCodeAttribute</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator&quot;</span>, <span style="color: #666666;">&quot;9.0.0.0&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
		<span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Settings<span style="color: #008000;">:</span><span style="color: #000000;">System.<span style="color: #0000FF;">Configuration</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationSettingsBase</span> <span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Settings defaultInstance <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>Settings<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span> Synchronized<span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Settings<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Settings <span style="color: #0600FF; font-weight: bold;">Default</span> <span style="color: #008000;">&#123;</span>
				get <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">return</span> defaultInstance<span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Configuration</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserScopedSettingAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
			<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">DebuggerNonUserCodeAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
			<span style="color: #008000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Configuration</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultSettingValueAttribute</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;3000&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> MeterValue <span style="color: #008000;">&#123;</span>
				get <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#91;</span> <span style="color: #666666;">&quot;MeterValue&quot;</span> <span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				set <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#91;</span> <span style="color: #666666;">&quot;MeterValue&quot;</span> <span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #008080;">#endregion</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I didn&#8217;t let it running too much time but still you can see that it works :<br />
<a href="http://i.mivi.fr/dd6f0441bdec29e66f68ff07a5530c52237ff66e.png"><img src="http://i.mivi.fr/dd6f0441bdec29e66f68ff07a5530c52237ff66e.x.800.png" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/google-powermeter-and-sample-c-net-api-usage/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stupid C++ vs C# performance comparison</title>
		<link>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=stupid-cpp-vs-charp-performance-comparison</link>
		<comments>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison#comments</comments>
		<pubDate>Fri, 26 Feb 2010 20:00:35 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[g++]]></category>
		<category><![CDATA[gmcs]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2409</guid>
		<description><![CDATA[I found out that there is a much better test than the one i did here. In fact the main interest of the post is this spreadsheet : I recently did a simple and stupid comparison test between C++ (g++) and C# .Net (mono). My point here is that C# can actually be considered as [...]]]></description>
			<content:encoded><![CDATA[<p>I found out that there is <a href="http://reverseblade.blogspot.com/2009/02/c-versus-c-versus-java-performance.html">a much better test</a> than the one i did here. In fact the main interest of the post is this spreadsheet :</p>
<div style="text-align: center">
<a href="http://i.mivi.fr/013092c429a64dd39a55850d8467bad0869e5b35.gif"><img src="http://i.mivi.fr/013092c429a64dd39a55850d8467bad0869e5b35.x.700.gif" /></a>
</div>
<p>I recently did a simple and stupid comparison test between C++ (g++) and C# .Net (mono).</p>
<p>My point here is that C# can actually be considered as a very fast language. It allows automatic hardware-specific optimization.<br />
Lot of people think it&#8217;s like a java, it has a virtual machine. But in fact it just compiles everything into native language when it accesses methods. This is the whole purpose of the JIT (Just Int Time compiler).</p>
<p>If you feel the JIT compiler is a possible reason of slowness (it slows down the program startup), you can pre-compile your assemblys (mono &#8211;aot on linux). If you feel the mono files are a drawback, you can bundle everything in one file using mkbundle. You can read more about this <a href="http://www.mono-project.com/Guide:Running_Mono_Applications">here</a>.</p>
<p>The only real drawback you have in the .Net framework is the garbage collector. It prevents the C# .Net it from being a realtime language/framework. But in real life usage it doesn&#8217;t slow much.</p>
<p>The purpose of the following test is to show that C++ isn&#8217;t faster than C# &#8220;as is&#8221;. I KNOW C# can not be faster than C++ because C# can do few hardware specific optimizations when you can do all of them in C++.</p>
<p>But if you&#8217;re searching for arguments to choose C# .Net over native C++, you should also consider these :</p>
<ul>
<li>C# is much more simple. No headers, the languages specifications are simple and clear</li>
<li>You don&#8217;t have to manage the memory in .Net (no memory leak, optimal memory consumption, etc.)</li>
<li>.Net don&#8217;t just crash, they always throw an exception and they only crash if nobody catches it</li>
<li>Mono (and presumely the .Net framework) does some really interesting optimizations :

<div class="wp_codebox"><table><tr id="p240914"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code14"><pre class="sh" style="font-family:monospace;"># mono --list-opt</pre></td></tr></table></div>

<p>I put some link so that you can learn about these really interesting optimization. This is like design patterns, you should really know these things.</em></p>
<table>
<tr>
<td>peephole</td>
<td><a href="http://en.wikipedia.org/wiki/Peephole_optimization">Peephole postpass</a></td>
</tr>
<tr>
<td>branch</td>
<td><a href="http://www.compileroptimizations.com/category/branch_optimization.htm">Branch optimizations</a></td>
</tr>
<tr>
<td>inline</td>
<td><a href="http://dotnetperls.com/inline-optimization">Inline method calls</a></td>
</tr>
<tr>
<td>cfold</td>
<td><a href="http://www.nullstone.com/htmls/category/consfold.htm">Constant folding</a></td>
</tr>
<tr>
<td>consprop</td>
<td><a href="http://en.wikipedia.org/wiki/Constant_folding#Constant_propagation">Constant propagation</a></td>
</tr>
<tr>
<td>copyprop</td>
<td><a href="http://en.wikipedia.org/wiki/Copy_propagation">Copy propagation</a></td>
</tr>
<tr>
<td>deadce</td>
<td><a href="http://en.wikipedia.org/wiki/Dead_code_elimination">Dead code elimination</a></td>
</tr>
<tr>
<td>linears</td>
<td><a href="http://en.wikipedia.org/wiki/Register_allocation">Linear scan global reg allocation</a></td>
</tr>
<tr>
<td>cmov</td>
<td><a href="http://assemblyrequired.crashworks.org/2009/01/04/fcmp-conditional-moves-for-branchless-math/">Conditional moves</a></td>
</tr>
<tr>
<td>shared</td>
<td>Emit per-domain code</td>
</tr>
<tr>
<td>sched</td>
<td><a href="http://en.wikipedia.org/wiki/Instruction_scheduling">Instruction scheduling</a></td>
</tr>
<tr>
<td>intrins</td>
<td>Intrinsic method implementations</td>
</tr>
<tr>
<td>tailc</td>
<td><a href="http://en.wikipedia.org/wiki/Tail_recursion">Tail recursion and tail calls</a></td>
</tr>
<tr>
<td>loop</td>
<td><a href="http://en.wikipedia.org/wiki/Loop_optimization">Loop related optimizations</a></td>
</tr>
<tr>
<td>fcmov</td>
<td><a href="http://en.wikipedia.org/wiki/FCMOV">Fast x86 FP compares</a></td>
</tr>
<tr>
<td>leaf</td>
<td><a href="http://moby.cs.uchicago.edu/documentation/HTML/ImplNotes/report011.html#htoc82">Leaf procedures optimizations</a></td>
</tr>
<tr>
<td>aot</td>
<td><a href="http://www.mono-project.com/AOT">Usage of Ahead Of Time compiled code</a></td>
</tr>
<tr>
<td>precomp</td>
<td>Precompile all methods before executing Main</td>
</tr>
<tr>
<td>abcrem</td>
<td><a href="http://blogs.msdn.com/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx">Array bound checks removal</a></td>
</tr>
<tr>
<td>ssapre</td>
<td><a href="http://en.wikipedia.org/wiki/Partial_redundancy_elimination">SSA based Partial Redundancy Elimination</a></td>
</tr>
<tr>
<td>exception</td>
<td><a href="http://stackoverflow.com/questions/1108050/does-the-net-jit-optimize-nested-try-catch-statements">Optimize exception catch blocks</a></td>
</tr>
<tr>
<td>ssa</td>
<td><a href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Use plain SSA form</a></td>
</tr>
<tr>
<td>treeprop</td>
<td>Tree propagation</td>
</tr>
<tr>
<td>sse2</td>
<td><a href="http://en.wikipedia.org/wiki/SSE2">SSE2 instructions on x86</a></td>
</tr>
<tr>
<td>gshared</td>
<td>Share generics</td>
</tr>
<tr>
<td>simd</td>
<td><a href="http://en.wikipedia.org/wiki/SIMD">Simd intrinsics</a></td>
</tr>
</table>
</li>
<li>Multithreading in .net is much easier than any other language. You even have the parrallel loop operations, like <a href="http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html">Parallel.For</a>.</li>
</ul>
<p><strong>SO ! Here is the test&#8230;</strong></p>
<p>I wrote these two sample programs :</p>
<p>One in C++ :</p>

<div class="wp_codebox"><table><tr id="p240915"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p2409code15"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">long</span> stupidThing<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">long</span> nb <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">long</span> out <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> nb <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
		out <span style="color: #000040;">*</span><span style="color: #000080;">=</span> nb<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
	<span style="color: #0000ff;">return</span> out<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #0000ff;">long</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">long</span> l <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> l <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>l <span style="color: #008000;">&#41;</span>
			total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> stupidThing<span style="color: #008000;">&#40;</span> l <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total : &quot;</span> <span style="color: #000080;">&lt;&lt;</span> total <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>One in C# :</p>

<div class="wp_codebox"><table><tr id="p240916"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p2409code16"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> test <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">class</span> Program <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">long</span> stupidThing<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">long</span> nb <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        <span style="color: #6666cc; font-weight: bold;">long</span> ret <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span> nb <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span>
                                ret <span style="color: #008000;">*=</span> nb<span style="color: #008000;">--;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> ret<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
                        <span style="color: #6666cc; font-weight: bold;">long</span> total <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
                        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000000</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#41;</span>
                                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">long</span> l <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> l <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>l <span style="color: #008000;">&#41;</span>
                                        total <span style="color: #008000;">+=</span> stupidThing<span style="color: #008000;">&#40;</span> l <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Total : {0}&quot;</span>, total <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>First of all, I open a shell in real-time priority, because I don&#8217;t want my other processses to mess with my tests :</p>

<div class="wp_codebox"><table><tr id="p240917"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code17"><pre class="sh" style="font-family:monospace;"># rtprio 99 bash</pre></td></tr></table></div>

<p>Then I compile the two programs :</p>

<div class="wp_codebox"><table><tr id="p240918"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2409code18"><pre class="sh" style="font-family:monospace;"># gmcs test.cs
# g++ -O4 test.cpp -o test</pre></td></tr></table></div>

<p>And then I launch my test :</p>
<p>On a 64 bits host :</p>

<div class="wp_codebox"><table><tr id="p240919"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code19"><pre class="txt" style="font-family:monospace;">Kernel : 2.6.9-023stab051.3-smp #1 SMP Wed Nov 4 18:36:34 MSK 2009 x86_64 x86_64 x86_64 GNU/Linux</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p240920"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2409code20"><pre class="sh" style="font-family:monospace;"># time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : -6192109806162068864
&nbsp;
real    0m12.433s
user    0m12.394s
sys     0m0.049s
Total : -6192109806162068864
&nbsp;
real    0m12.415s
user    0m12.411s
sys     0m0.014s
Total : -6192109806162068864
&nbsp;
real    0m12.430s
user    0m12.411s
sys     0m0.026s
Total : -6192109806162068864
&nbsp;
real    0m10.311s
user    0m10.287s
sys     0m0.029s
Total : -6192109806162068864
&nbsp;
real    0m10.254s
user    0m10.247s
sys     0m0.011s
Total : -6192109806162068864
&nbsp;
real    0m10.250s
user    0m10.255s
sys     0m0.012s</pre></td></tr></table></div>

<p>C# clearly beats C++ here. Well</p>
<p>On a 32 bits host :</p>

<div class="wp_codebox"><table><tr id="p240921"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code21"><pre class="txt" style="font-family:monospace;">Kernel : 2.6.30-2-686 #1 SMP Fri Dec 4 00:53:20 UTC 2009 i686 GNU/Linux</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p240922"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2409code22"><pre class="sh" style="font-family:monospace;"># time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : 100461056
&nbsp;
real    1m10.927s
user    1m7.376s
sys     0m0.056s
Total : 100461056
&nbsp;
real    1m12.590s
user    1m8.976s
sys     0m0.020s
Total : 100461056
&nbsp;
real    1m13.279s
user    1m9.532s
sys     0m0.056s
Total : -6192109806162068864
&nbsp;
real    2m22.492s
user    2m15.260s
sys     0m0.136s
Total : -6192109806162068864
&nbsp;
real    2m23.002s
user    2m15.760s
sys     0m0.104s
Total : -6192109806162068864
&nbsp;
real    2m25.102s
user    2m17.709s
sys     0m0.144s</pre></td></tr></table></div>

<p>C++ beats C# here, but in 32 bits C++ use other types whereas C# use the same. In C# long is always 64 bits, in C++ it can be 64 bits or 32 bits (depending on the current architecture).</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>D language</title>
		<link>http://florent.clairambault.fr/d-language?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=d-language</link>
		<comments>http://florent.clairambault.fr/d-language#comments</comments>
		<pubDate>Thu, 25 Feb 2010 21:57:40 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[D language]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2406</guid>
		<description><![CDATA[This is just a very short post about this language I recently discovered : the D language. As described on its little website : &#8220;D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a very short post about this language I recently discovered : the D language. As described on <a href="http://www.d-programming-language.org/">its little website</a> : &#8220;D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.&#8221;.</p>
<p>It has most of the features of the java and the C# (best language ever), but it is fully compiled (no JVM or JIT stuff).</p>
<p>You should have a quick loot of its features on the <a href="http://www.digitalmars.com/d/">Digital mars website</a> (they&#8217;re the who made it).</p>
<p>I wonder if this language really has a future. C# (with the .Net framework) compiles and runs on every platform (thanks to <a href="http://www.mono-project.com/">mono</a>), even <a href="http://monotouch.net/">on the iPhone</a>, so it only remove the burden of the libraries required to run .net apps.<br />
So, will it really beats its performance ? Because in C# is very fast (my next post will bring a stupid C++/C# performance comparison), and in C# avoiding time or memory consuming mistakes or multi-threading our code is very easy.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/d-language/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPSecurity.RunWithElevatedPrivileges</title>
		<link>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=spsecurity-runwithelevatedprivileges</link>
		<comments>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges#comments</comments>
		<pubDate>Mon, 22 Feb 2010 20:00:56 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[SPSite]]></category>
		<category><![CDATA[SPWeb]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2303</guid>
		<description><![CDATA[If like me you&#8217;re wondering what exactly does SPSecurity.RunWithElevatedPrivileges, this is what I understand of it : 1 2 3 4 5 SPSecurity.RunWithElevatedPrivileges&#40; &#40;&#41; =&#62; &#123; using&#40; var newSite = new SPSite&#40; site.Url &#41; &#41; &#123; // your stuff &#125; &#125;&#41;; Is equivalent to : 1 2 3 using&#40; var newSite = new SPSite&#40; site.Url, [...]]]></description>
			<content:encoded><![CDATA[<p>If like me you&#8217;re wondering what exactly does SPSecurity.RunWithElevatedPrivileges, this is what I understand of it :</p>

<div class="wp_codebox"><table><tr id="p230325"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2303code25"><pre class="csharp" style="font-family:monospace;">SPSecurity<span style="color: #008000;">.</span><span style="color: #0000FF;">RunWithElevatedPrivileges</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span> var newSite <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// your stuff</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Is equivalent to :</p>

<div class="wp_codebox"><table><tr id="p230326"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2303code26"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span> var newSite <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span>, site<span style="color: #008000;">.</span><span style="color: #0000FF;">SystemAccount</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Token</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// your stuff</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So if this is just to open an elevated privilege SPSite, it will be equivalent. But if you do some more complex things, as soon as you load a new SPSite it will open it with the original UserToken. And you lose&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# .Net : How works automatic assembly versioning</title>
		<link>http://florent.clairambault.fr/c-net-how-works-automatic-assembly-versioning?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=c-net-how-works-automatic-assembly-versioning</link>
		<comments>http://florent.clairambault.fr/c-net-how-works-automatic-assembly-versioning#comments</comments>
		<pubDate>Sun, 21 Feb 2010 12:02:01 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[assembly versions]]></category>
		<category><![CDATA[C# .Net]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2388</guid>
		<description><![CDATA[When you choose to automatically generate a new version of your assembly at compilation, you have to set in the AssemblyInfo.cs file, this line : 1 2 3 4 5 6 7 8 9 10 // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // [...]]]></description>
			<content:encoded><![CDATA[<p>When you choose to automatically generate a new version of your assembly at compilation, you have to set in the AssemblyInfo.cs file, this line :</p>

<div class="wp_codebox"><table><tr id="p238829"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p2388code29"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Version information for an assembly consists of the following four values:</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">//      Major Version</span>
<span style="color: #008080; font-style: italic;">//      Minor Version </span>
<span style="color: #008080; font-style: italic;">//      Build Number</span>
<span style="color: #008080; font-style: italic;">//      Revision</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">// You can specify all the values or you can default the Build and Revision Numbers </span>
<span style="color: #008080; font-style: italic;">// by using the '*' as shown below:</span>
<span style="color: #008000;">&#91;</span>assembly<span style="color: #008000;">:</span> AssemblyVersion<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;1.0.*&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span></pre></td></tr></table></div>

<p>But what do these automatically generated numbers mean ? Well, chances are you don&#8217;t care. But one day after using this features for years I decided that I would.</p>
<ul>
<li>The build number is the number of days since 01/01/2000</li>
<li>The revision number if the number of 2 seconds periods since 00:00 of this day</li>
</ul>
<p>So&#8230; To get the compilation date of an assembly from its version, you would have to do :</p>

<div class="wp_codebox"><table><tr id="p238830"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2388code30"><pre class="csharp" style="font-family:monospace;">var version <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Version</span><span style="color: #008000;">;</span>
var date <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DateTime<span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">2000</span>, 01, 01 <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddDays</span><span style="color: #008000;">&#40;</span> version<span style="color: #008000;">.</span><span style="color: #0000FF;">Build</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddSeconds</span><span style="color: #008000;">&#40;</span> version<span style="color: #008000;">.</span><span style="color: #0000FF;">Revision</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>This feature is great if your need some atomic versionning of a particular library for instance. Each program using your library has its own version and you don&#8217;t risk to break working feature in future release of your library. And if you want to force the use of a newer version of a library within a particular application, you can use some <a href="http://msdn.microsoft.com/en-us/library/7wd6ex19(VS.71).aspx">assembly redirection</a>.<br />
But you should also note that if you deploy your assembly in the <a href="http://florent.clairambault.fr/tag/GAC">GAC</a> (like it&#8217;s required to do with Sharepoint), you will have tons of &#8220;garbage&#8221; versions of your assembly. And there&#8217;s not &#8220;easy fix&#8221; for that.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/c-net-how-works-automatic-assembly-versioning/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Loading plugins assemblies in .Net</title>
		<link>http://florent.clairambault.fr/loading-plugins-assemblies-in-net?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=loading-plugins-assemblies-in-net</link>
		<comments>http://florent.clairambault.fr/loading-plugins-assemblies-in-net#comments</comments>
		<pubDate>Tue, 01 Dec 2009 18:00:37 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net Add-In]]></category>
		<category><![CDATA[.Net plugins]]></category>
		<category><![CDATA[C# .Net]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2267</guid>
		<description><![CDATA[(first post of the year) This might seem like a quite complex thing to do, but it&#8217;s in fact very simple. Thank you .Net for being so well built. Note : With .Net 3.5, there is a much more advanced method called Add-In. But it&#8217;s also much more complex. You should use it on long-term [...]]]></description>
			<content:encoded><![CDATA[<p><em>(first post of the year)</em></p>
<p>This might seem like a quite complex thing to do, but it&#8217;s in fact very simple. Thank you .Net for being so well built.</p>
<p>Note : With .Net 3.5, there is a much more advanced method called <a href="http://msdn.microsoft.com/en-us/library/bb384200.aspx">Add-In</a>. But it&#8217;s also much more complex. You should use it on long-term projects with some evolutions of the plugins API (and no possibility to change the plugins). I&#8217;ve used it for a project and that really made us lose a lot of time.</p>
<p>So here is the code for a simple plugins class loading system :</p>

<div class="wp_codebox"><table><tr id="p226735"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p2267code35"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span> GetPlugins<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> folder <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	var files <span style="color: #008000;">=</span> Directory<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFiles</span><span style="color: #008000;">&#40;</span> folder, <span style="color: #666666;">&quot;*.dll&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var tList <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> file <span style="color: #0600FF; font-weight: bold;">in</span> files <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
			var assembly <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadFile</span><span style="color: #008000;">&#40;</span> file <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> Type type <span style="color: #0600FF; font-weight: bold;">in</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTypes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>type<span style="color: #008000;">.</span><span style="color: #0000FF;">IsClass</span> <span style="color: #008000;">||</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNotPublic</span> <span style="color: #008000;">&#41;</span>
					<span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
				var interfaces <span style="color: #008000;">=</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInterfaces</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>IList<span style="color: #008000;">&#41;</span> interfaces <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
					tList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> type <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception ex <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogException</span><span style="color: #008000;">&#40;</span> ex <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> tList<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> InstanciatePlugins<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span> types, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args <span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T<span style="color: #008000;">:</span><span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #008000;">&#123;</span>
	var list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> var t <span style="color: #0600FF; font-weight: bold;">in</span> types <span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInterfaces</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IList <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
			list<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> Activator<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span> t, args <span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> T <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> list<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Our project can be organized like this :<br />
Project.Project : The application that will load plugins<br />
Project.Common.Plugins : The common types used by the core and the plugins<br />
Project.Plugin.Test1 : One stupid test plugin</p>
<p>In Project.Common.Plugins, we will declare an interface :</p>

<div class="wp_codebox"><table><tr id="p226736"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2267code36"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Project<span style="color: #008000;">.</span><span style="color: #0000FF;">Common</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Plugins</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IPlugin <span style="color: #008000;">&#123;</span>
		<span style="color: #6666cc; font-weight: bold;">String</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">void</span> DoSomeStuff<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In Project.Plugin.Test1, we will declare a class :</p>

<div class="wp_codebox"><table><tr id="p226737"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2267code37"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Project<span style="color: #008000;">.</span><span style="color: #0000FF;">Plugin</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Test1</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PluginTest1 <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">String</span> Name <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;PluginTest1&quot;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoSomeStuff<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #008000;">&#41;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I only count ({0})&quot;</span>, i <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This assembly has to be generated in a &#8220;plugins&#8221; directory.</p>
<p>Then, in your project, you will just have to use the methods given in the beginning and do something like that :</p>

<div class="wp_codebox"><table><tr id="p226738"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2267code38"><pre class="csharp" style="font-family:monospace;">var types <span style="color: #008000;">=</span> GetPlugins<span style="color: #008000;">&lt;</span>IPlugin<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;plugins&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var pluginInstances <span style="color: #008000;">=</span> InstanciatePlugins<span style="color: #008000;">&#40;</span> types, <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Plugins are :&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var pi <span style="color: #0600FF; font-weight: bold;">in</span> pluginInstances <span style="color: #008000;">&#41;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;* {0}&quot;</span>, pi<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you&#8217;re worried that you&#8217;re stuck with these created objects, you should take a look on the AppDomain (I think I will talk about them pretty soon). This allows to load .Net assemblies and types and then unload them when ever you want. But as it can be easily adapted to some existing code, you should start without it and then add it when you feel your application could benefit from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/loading-plugins-assemblies-in-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NDepend</title>
		<link>http://florent.clairambault.fr/ndepend?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=ndepend</link>
		<comments>http://florent.clairambault.fr/ndepend#comments</comments>
		<pubDate>Sun, 08 Nov 2009 19:57:35 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[NDepend]]></category>
		<category><![CDATA[Resharper]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1918</guid>
		<description><![CDATA[Patrick Smacchia gave me a professional license of NDepend v2.12 so that I could write some stuff about it if I liked it. As it was a gift (yes it is), I decided to force myself to look into this software. And after having looked much of its functionalities, I kind of like it. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codebetter.com/blogs/patricksmacchia/">Patrick Smacchia</a> gave me a professional license of <a href="http://www.ndepend.com/">NDepend</a> v2.12 so that I could write some stuff about it if I liked it. As it was a gift (yes it is), I decided to force myself to look into this software. And after having looked much of its functionalities, I kind of like it. It&#8217;s not THE ultimate tool you have to use, but it&#8217;s a little bit like the other (resharper, reflector, etc.), it gives you a better insight of what you have in hand.</p>
<p>It&#8217;s a little bit like Resharper, it helps you see what you might have done wrong. Except Resharper tells it when you&#8217;re editing the code and NDepend helps you make a full review of the code.</p>
<p>Everything in this software goes around the Code Query Language (CQL). I thought this was some sort of commercial concept to sell more but it turns out that we can do a lot of things with it. It seems like a sensible idea, it&#8217;s SQL for the code. And the CQL textbox supports for auto-completion makes the CQL writing process pretty easy.</p>
<p><strong>Don&#8217;t make it your judgment tool</strong><br />
I guess the most risky think to do would be to give NDepend to dumb people (like code quality reviewers that don&#8217;t understand much of what they do). They would end up getting on everybody&#8217;s nevers. Because I don&#8217;t think that all the code creating query warnings could be changed. I&#8217;ve taken the simple stripped down method example of the StreamRipper app :</p>

<div class="wp_codebox"><table><tr id="p191840"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p1918code40"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Boolean ParseArgs<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args, Parameters parameters <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> args<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span> args<span style="color: #008000;">&#91;</span> i <span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;--url&quot;</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;-u&quot;</span><span style="color: #008000;">:</span>
					parameters<span style="color: #008000;">.</span><span style="color: #0000FF;">Urls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> args<span style="color: #008000;">&#91;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
&nbsp;
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;--user-agent&quot;</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;-a&quot;</span><span style="color: #008000;">:</span>
					parameters<span style="color: #008000;">.</span><span style="color: #0000FF;">UserAgent</span> <span style="color: #008000;">=</span> args<span style="color: #008000;">&#91;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
&nbsp;
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;--reconnect-attempts&quot;</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;-r&quot;</span><span style="color: #008000;">:</span>
					parameters<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxReconnectionTries</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span> args<span style="color: #008000;">&#91;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Arguments error ! &quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This code will create a cyclomatic complexity warning (as it would in Resharper 5.0 [I should talk about this someday] with the cyclomatic complexity addin). So, for me, it&#8217;s clear you have to accept to have a lot of warnings. It&#8217;s not like resharper where you can solve all the warnings by accepting every little things he tells you to do (sometimes I&#8217;m resharper&#8217;s slave but I like it).</p>
<p>BUT, if you really want to make it your judgment call tool, you should really stick to this diagram :</p>
<div style="text-align: center"><img src="http://i.mivi.fr/ee5c87844c14b061e7688781307b3376273d6d73.png" /></div>
<p>You can see that in this project some of the projets are in the zone of pain and the zone of uselessness. Don&#8217;t worry. In the zone of uselessness we have the WCF contracts. It&#8217;s quite normal they are totally abstract. And close to the zone of pain, we have low level libraries. So, nothing to worry about.</p>
<p>If you WANT a quick and dirty rule : If your business logic core code is in one of these two red zones, you have a problem.</p>
<p><strong>What I think it does well</strong><br />
I think it gives you a quick view of the software you&#8217;re working on : What are the main projects, classes and methods. What quality of work you should expect from it. The graphical view isn&#8217;t pretty but it gives you a good simplified view :</p>
<div style="text-align: center">
<img src="http://i.mivi.fr/380b2b6f72d30c1c6364d474364d20201923ad54.x.800.png" />
</div>
<p>And anytime you have a doubt, you just double-click on the method and it opens it in Visual Studio.</p>
<p>You have the dependency graphical view :</p>
<div style="text-align: center">
<img src="http://i.mivi.fr/420da71ae9cb3e162609ff3f302194399ff349d1.x.800.png" /></p>
<p>It doesn&#8217;t look very useful like this. But within Visual NDepend, it displays the parent and child assemblies when you move your mouse over a project :<br />
<img src="http://i.mivi.fr/3139ed7c3d9bc525e527b6e950f06c17a1a13657.x.800.png" />
</div>
<p><strong>Evolution of your project</strong><br />
NDepend also saves the results of all previous analysis and allows you to show the evolution of your product. You can see easily what methods have been modified / added / deleted from one analysis to an other. Each analysis you do is saved and can be compared to any other later. This can be done on simple .Net assembly. This will allow you to see what has been changed between two version of an assembly. And with the help of reflector, you can see precisely what has been fixed/improved.</p>
<p>You can see a pretty good example by Patrick Smacchia : <a href="http://codebetter.com/blogs/patricksmacchia/archive/2009/10/21/interesting-findings-in-the-diff-between-net-fx-v4-beta1-and-beta2.aspx">Comparison of .Net Bêta 1 and 2 with NDepend</a>.</p>
<p><strong>My thoughts</strong><br />
I think it gives you a quick and simplified view of the organization and size of a project. It&#8217;s great .Net tool, I would recommend to any company having big .Net projects. But you shouldn&#8217;t spend too much time on trying to comply with all its default CQL query checks as they are a little bit constrictive. If you do, you might want to increase the threshold values. And please take extra care before making judgment calls (it might be tempting).</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/ndepend/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.Net, Flex and WebORB.Net</title>
		<link>http://florent.clairambault.fr/net-flex-and-weborb-net?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=net-flex-and-weborb-net</link>
		<comments>http://florent.clairambault.fr/net-flex-and-weborb-net#comments</comments>
		<pubDate>Sat, 24 Oct 2009 07:47:41 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[TC65]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WebORB]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1825</guid>
		<description><![CDATA[I&#8217;ve been working on a project where we had to use Flex on a .Net environment and it had to be realtime. I was a little worried at first that it could be tricky to set up. But with WebORB.Net it&#8217;s pretty easy. We used the integrated RTMP (Real Time Messaging Protocol) messaging server. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a project where we had to use Flex on a .Net environment and it had to be realtime. I was a little worried at first that it could be tricky to set up. But with <a href="http://www.themidnightcoders.com/products/weborb-for-net/overview.html">WebORB.Net</a> it&#8217;s pretty easy. We used the integrated RTMP (Real Time Messaging Protocol) messaging server. It&#8217;s almost like using WCF. The most important differences are that, by default, objects are transmitted as Hashtable and calls can&#8217;t be synchronous. We can bind the object to right .Net object within the WebOrb management console but we decided to do it ourself using reflection (because we don&#8217;t like to depend too much on the management console).</p>
<p><strong>RTMP</strong><br />
The result is pretty impressive as it makes a really powerful real-time management system. And it&#8217;s freaking fast. Like WCF, it can handle concurrent calls. It&#8217;s in fact easier because everything is asynchronous but still, it does manage that. These calls are received on parallel threads.</p>
<p>The only problem with WebORB.Net is that you don&#8217;t have too much examples in the documentation. Our biggest problem was that we didn&#8217;t even knew how to detect what was the current calling connection on a method. This is pretty easy but you can&#8217;t exactly guess it. This is a dummy sample to show you how it&#8217;s done :</p>

<div class="wp_codebox"><table><tr id="p182542"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code" id="p1825code42"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FlexConnectionsServer<span style="color: #008000;">:</span>ApplicationAdapter <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _connectionIdCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> Dictionary<span style="color: #008000;">&lt;</span>IConnection,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> _connectionsToId <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span>IConnection,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Returns the CURRENT connection (must be used from a method called by the flex client)</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> IConnection CurrentConnection <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> ConnectionHub<span style="color: #008000;">.</span><span style="color: #0000FF;">getConnectionLocal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> CurrentID <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _connectionsToId<span style="color: #008000;">&#91;</span> CurrentConnection <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// When the RTMP service is loaded, we start the time dispatcher thread</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> appStart<span style="color: #008000;">&#40;</span> IScope app <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #008000;">&#40;</span> Thread_TimeDispatcher <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This method is called when a client connects</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> appConnect<span style="color: #008000;">&#40;</span> IConnection conn, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> parms <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_connectionsToId<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> conn, _connectionIdCounter<span style="color: #008000;">++</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This method is called when a client disconnects</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> appDisconnect<span style="color: #008000;">&#40;</span> IConnection conn <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_connectionsToId<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span> conn <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This is a sample method call</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">String</span> GetMyName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Client{0}&quot;</span>, CurrentId <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This thread dispatches the current time to every client</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Thread_TimeDispatcher<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			var t <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">UtcNow</span><span style="color: #008000;">;</span>
			SendCommand<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;CurrentTime&quot;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> t <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Call a remote method on each client connection</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SendCommand<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span> command, <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var kvp <span style="color: #0600FF; font-weight: bold;">in</span> _connectionsToId <span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#40;</span> kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span> <span style="color: #0600FF; font-weight: bold;">as</span> IServiceCapableConnection <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span> command, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s a little bit like using WCF in Single instance mode.</p>
<p><strong>IIS 7.0 Setup</strong><br />
There was something really weird that happened with my IIS 7.0 on my Windows 7 x64. Using the RTMP connection, I frequently had null return values on .Net method calls. We firstly find a quick way to solve it : Recall each time we get a null value. It&#8217;s really dirty, but It worked as a very temporary solution.<br />
Then, when debugging on the IIS process, I noticed a lot of NullReferenceException exceptions displayed on the debugger console. They were about the WebORB &#8220;LicenseManager&#8221; class, and I had as much exceptions as I had null values on the Flex client. I tried a lot of things that didn&#8217;t do anything. And then, I changed the &#8220;Managemd pipeline mode&#8221; of the Application Pool from &#8220;Integrated&#8221; to &#8220;Classic&#8221;, and the problem was solved. The really weird stuff here is that <a href="http://www.themidnightcoders.com/products/weborb-for-net/developer-den/technical-articles/installing-weborb-on-vista-iis-7-install.html">WebORB recommends to use the &#8220;Integrated&#8221; mode</a>.</p>
<p><strong>MidnightCoders support</strong><br />
We asked MidnightCoders to send us a community edition license, and they instantly answered. We got our license within the 48 hours after our first mail (I was the one slowing it down).</p>
<p><strong>Quick note on the project</strong><br />
To give you a better view of the project I&#8217;m talking about :<br />
GPRS Equipment <--(<a href="http://florent.clairambault.fr/the-m2mp-protocol">M2MP protocol</a>)&#8211;> [Generic M2M Server] <--(WCF)--> [Business logic software] <--(WCF)--> [Flex connections server] <--(RTMP)--> Flex<br />
We&#8217;ve got a little equipment (<a href="http://florent.clairambault.fr/tag/tc65">TC65</a> based as you can guess) that connects to a generic server using our own (real-time and bandwidth optimized) protocol. A business logic software connects to this server using a WCF service. And then we have the WebORB.Net / IIS that connects to this business logic software. </p>
<p>WCF helps make very interesting .Net applications because it simplifies all the communication but can also create deadlocks you might not have seen. Take this example : You have a lock on the WCF Server on the clients list object, you make a call to the WCF client and this client makes a call to the WCF Server that also requires a lock. If you were doing all this client/server calls on the same process this wouldn&#8217;t be a problem. But as you use WCF client-to-server and server-to-client calls are made on differents threads. You create a deadlock (unless you set the OperationContract attribute to IsOneWay and/or you fix the deadlock source).</p>
<p>If you&#8217;re doing real-time applications, I would recommend to define all the frequently used methods with the &#8220;IsOneWay=true&#8221; option. </p>
<p><em>06/11/2009 Update : I added a sample server-to-client code because some people were interested by it.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/net-flex-and-weborb-net/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.497 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-29 16:44:28 -->
