<?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>Juan Wajnerman</title>
	<atom:link href="http://weblogs.manas.com.ar/waj/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblogs.manas.com.ar/waj</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 19:38:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PDC2008 at Buenos Aires</title>
		<link>http://weblogs.manas.com.ar/waj/2008/11/21/pdc2008-at-buenos-aires/</link>
		<comments>http://weblogs.manas.com.ar/waj/2008/11/21/pdc2008-at-buenos-aires/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 01:56:10 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/?p=27</guid>
		<description><![CDATA[Last Tuesday we had a resume of the PDC2008 here in Buenos Aires. I had the opportunity to present, together with my friend and coworker Brian Cardiff, the roadmap for ASP.NET 4.0. All the downloadable stuff is already available (mostly in Spanish) here: http://blogs.msdn.com/gardel/archive/2008/11/20/pdc-buenos-aires-materiales-y-links.aspx. My friends Kzu and Vga were also presenting a nice preview [...]]]></description>
			<content:encoded><![CDATA[<p>Last Tuesday we had a resume of the PDC2008 here in Buenos Aires. I had the opportunity to present, together with my friend and coworker <a href="http://weblogs.manas.com.ar/bcardiff/">Brian Cardiff</a>, the roadmap for ASP.NET 4.0.</p>
<p>All the downloadable stuff is already available (mostly in Spanish) here: <a href="http://blogs.msdn.com/gardel/archive/2008/11/20/pdc-buenos-aires-materiales-y-links.aspx">http://blogs.msdn.com/gardel/archive/2008/11/20/pdc-buenos-aires-materiales-y-links.aspx</a>.</p>
<p>My friends <a href="http://www.clariusconsulting.net/blogs/kzu">Kzu</a> and <a href="http://www.clariusconsulting.net/blogs/vga/">Vga</a> were also presenting a nice preview of VS2010.</p>
<p>It was a great experience, thanks <a href="http://blogs.msdn.com/masaez">Miguel</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2008/11/21/pdc2008-at-buenos-aires/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Location Extraction</title>
		<link>http://weblogs.manas.com.ar/waj/2008/10/07/location-extraction/</link>
		<comments>http://weblogs.manas.com.ar/waj/2008/10/07/location-extraction/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 22:03:17 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/?p=25</guid>
		<description><![CDATA[In this post I'll describe I have been doing for Instedd during the last couple of weeks. In one of the projects we have we need to classify a series of articles depending on the geographical location they are talking about. This process is known as geotagging, and is really important on the biosurveillance areas. [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I'll describe I have been doing for <a href="http://www.instedd.org/">Instedd</a> during the last couple of weeks. In one of the projects we have we need to classify a series of articles depending on the geographical location they are talking about. This process is known as geotagging, and is really important on the biosurveillance areas.</p>
<p><a href="http://en.wikipedia.org/wiki/Geotagging">Geotagging</a> items is not a new thing, and many web sites already supports adding geographic information to the objects their handle. For example, <a href="http://www.flickr.com/map/">Flickr</a> allows you to set the coordinates where the picture was taken. <a href="http://en.wikipedia.org/wiki/Wikipedia:WikiProject_Geographical_coordinates">Wikipedia</a> also has structured information that contains the latitude and longitude for articles about a place in the world. On the other side, specs like <a href="http://georss.org/">GeoRSS</a> can be used to augment the information given by a feed. However, even though all these new geo-related features are being widely adopted, there are still much information out there, that would need a human reading the text to understand which places is it mentioning.</p>
<p>So, we decided to make this process automatically as most as possible, extracting the information from the text itself. This is know as "location extraction", and is actually a branch of a more general thing named "entity extraction".</p>
<p><span id="more-25"></span></p>
<p>The location extraction process can be splitted in two stages:</p>
<h2>Infer the possible location mentions in the text</h2>
<p>The first step in location extraction is to obtain the possible terms in the text that could be talking about a location in the world.</p>
<p>This is being accomplished using a <a href="http://en.wikipedia.org/wiki/Gazetteer">gazetteer</a> plus some heuristics. A <a href="http://en.wikipedia.org/wiki/Gazetteer">gazetteer</a> it's like a world location database, and in this case I used the information that can be downloaded from the site of <a href="http://www.geonames.org/">GeoNames</a>. This database contains dozens of million of places all around the world, each one with alternative names, kind of place and geographical coordinates between some other things.</p>
<p>The matching cannot be done simply by looking the words in a dictionary because most place names are composed by more than just one term (i.e.: Buenos Aires). This is why I used a variant of the <a href="http://en.wikipedia.org/wiki/Aho-Corasick_algorithm">Aho-Corasick</a> algorithm in which the tokens are whole words instead of characters. A good description of this algorithm can be read in these <a href="http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf">slides</a>. This means that I had to pre-process the GeoNames database (consisting in a bunch of zipped text files) and construct a directed graph. I decided to use <a href="http://www.sqlite.org/">SQLite</a> as the store for this huge graph as it is really fast and lightweight.</p>
<p>The construction of this database graph is a slow and memory hungry process. My first approach was to implement it with a Python script and it was enough to process test data from one or two countries. But for the final database that implies millions of nodes, the lightweight of C++ made a good job and I end up with a process that takes around 20 minutes in my Core 2 Duo notebook at 2.2 GHz. I think it's important to mention that I used a <a href="http://wiki.debian.org/ramfs">ramfs</a> file system to construct the SQLite database. This really makes a big difference. As I taken only the most important places for my database, the final version sizes around 500Mb.</p>
<p>Once the database is ready, the graph can be traversed as a kind of state machine with the words being read from the text. Every time a match is found, it's saved in a list for later disambiguation. The Aho-Corasick algorithm finds all the occurrences of substrings, so those that overlaps must be discarded. For instance if we read "San Fernando" we have a positive match that must be later discarded if we read "San Fernando del Valle de Catamarca". This is very straightforward if we save the starting word index and length of each location mention, so for each match we look up the list of previous matches from the bottom, removing all those entries that would overlap with the new one.</p>
<p>To improve the accuracy of the matching algorithm I added these heuristics to the process:</p>
<ol>
<li>Every place must start with a capital letter: If the name is composed by more than one word, the following words are not enforced to start with upper case. This doesn't work for all languages, but at this stage I only need it does with English and Spanish.</li>
<li>Uppercase chains must not be broken: this means that consecutive uppercase words must be taken together or not taken at all.</li>
</ol>
<h2>Resolve ambiguities</h2>
<p>Before an specific location name extracted from the text can be placed on a map we have to disambiguate it from different meanings. For example, there are dozens of places named 'Springfield' and not only in US. So the meaning of a particular name must be inferred from the context. For this part of the process I used mainly this <a href="http://portal.acm.org/citation.cfm?id=1119394.1119400">paper</a>.</p>
<p>The central idea is calculate a weight for each of the senses of every location. This weights are calculated taking into account the other senses of other locations coocurring in the same text, and the distance of these terms in the text.</p>
<p>To calculate the weight of a sense we use the following formula:</p>
<p><img class="alignnone size-full wp-image-26" title="senseweightformula" src="http://weblogs.manas.com.ar/waj/wp-content/uploads/2008/10/senseweightformula.png" alt="" width="460" height="78" /></p>
<p>Where "num" is the number of mentions of a place, "dist" represents the distance in words between two locations, and "weight" represents the influence of a location sense to other sense of a different location, and is calculated with the following table:</p>
<table border="1" width="385">
<tbody>
<tr>
<td>Sense 1</td>
<td>Sense 2</td>
<td>Condition</td>
<td>Weight</td>
</tr>
<tr>
<td>City</td>
<td>City</td>
<td>in same state</td>
<td>2</td>
</tr>
<tr>
<td></td>
<td>City</td>
<td>in same country</td>
<td>1</td>
</tr>
<tr>
<td></td>
<td>State</td>
<td>in same state</td>
<td>3</td>
</tr>
<tr>
<td></td>
<td>Country</td>
<td>in country without state</td>
<td>4</td>
</tr>
</tbody>
</table>
<p>Some heuristics are also applied in this stage of the process. For example, if a location name can be interpreted as a country, other meanings are discarded. Also, if the same location name appears many times in the same text, the sense with maximum weight is taken and propagated to the other mentions, based on the "one sense per discourse" principle.</p>
<p>To improve the accuracy and based on my own tests, I introduced these modifications to the original formula:</p>
<p>1) Increase weight based on the location inside the text: the weight of the locations found at the beginning of the text, or even at the title, are multiplied by a factor that decreases exponentially as we advance in the document.</p>
<p>2) Reduce the weight based on a blacklist: Many locations has names of common words. The weight of these locations are reduced if are found in a blacklist to avoid potential mismatches. This blacklist can also be populated based on feedback of the users of the final system.</p>
<p>Once the algorithm has finished, only those places with weights above the 5% of the maximum weight are considered for the final result.</p>
<h2>Next steps</h2>
<p>The algorithm described here is far from being perfect. There is some much work done out there about this subject and I need to introduce some interesting stuff. I found that the biggest effort should be placed on the first part of the process that currently is introducing some noise because of false positive matches.</p>
<p>These are some of the points in my wishlist:</p>
<p>1) Use pattern matching before the disambiguation algorithm for patterns like "&lt;city&gt;, &lt;state&gt;" or "&lt;city&gt;, &lt;country&gt;", or even "city of &lt;city&gt;".</p>
<p>2) Prepare a better blacklist based on statistics. These can be obtained looking for common words appearing in hundead of texts.</p>
<p>3) Improve the mention lookup with previous processing. For example using sentence splitting and <a href="http://en.wikipedia.org/wiki/Part-of-speech_tagging">POS tagging</a>.</p>
<p>I hope you found this article interesting, as it was to me to implement such process. The results are truly amazing!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2008/10/07/location-extraction/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Double Click in Silverlight (by Ary)</title>
		<link>http://weblogs.manas.com.ar/waj/2008/06/25/double-click-in-silverlight-by-ary/</link>
		<comments>http://weblogs.manas.com.ar/waj/2008/06/25/double-click-in-silverlight-by-ary/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 01:13:51 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/?p=24</guid>
		<description><![CDATA[Ary has just wrote a very useful post about supporting double click events in Silverlight 2.0. The approach he presents is really interesting and takes advantage of attached properties in order to provide clean usage. In fact the implementation supports not only double clicks, but multiple clicks being able to specify the max number of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://weblogs.manas.com.ar/ary">Ary</a> has just wrote a very useful <a href="http://weblogs.manas.com.ar/ary/2008/06/24/adding-double-click-support-in-silverlight/">post</a> about supporting double click events in Silverlight 2.0. The approach he presents is really interesting and takes advantage of attached properties in order to provide clean usage.</p>
<p>In fact the implementation supports not only double clicks, but multiple clicks being able to specify the max number of clicks we want to receive, and the number of actual clicks are received in the event handlers.</p>
<p>There were previous <a href="http://hackingsilverlight.blogspot.com/2008/02/silverlight-20-double-click-support.html">approaches</a> but the Ary's one feels much more natural to use.</p>
<p>I wish Microsoft adds support for this kind of basic events in the final release. However in this way we don't have to miss these useful gestures.</p>
<p>Good job Ary!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2008/06/25/double-click-in-silverlight-by-ary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox text selection tips</title>
		<link>http://weblogs.manas.com.ar/waj/2008/03/29/firefox-text-selection-tips/</link>
		<comments>http://weblogs.manas.com.ar/waj/2008/03/29/firefox-text-selection-tips/#comments</comments>
		<pubDate>Sat, 29 Mar 2008 23:40:02 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2008/03/29/firefox-text-selection-tips/</guid>
		<description><![CDATA[Firefox has many settings that are not available through the standard "Preferences" dialog. However these can be configured navigating to the "about:config" address. In that list we can find two useful options related to text selection, that has different default values depending on the platform (Windows, Linux, Mac). The first option "layout.word_select.stop_at_punctuation" controls whether the [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox has many settings that are not available through the standard "Preferences"<br />
dialog. However these can be configured navigating to the "<code>about:config</code>" address.</p>
<p>In that list we can find two useful options related to text selection, that has different<br />
default values depending on the platform (Windows, Linux, Mac).</p>
<p>The first option "<code>layout.word_select.stop_at_punctuation</code>" controls whether the selection<br />
when you do a double-click or use ctrl+arrows to select text, stops at dots, slashes and<br />
other symbols. Enabling this option is specially useful when trying to change some part of<br />
the URL in the address bar. Otherwise the entire URL is selected, thing that can still be<br />
done triple-clicking on the address.</p>
<p>This option is selected by default in Windows version of Firefox, but is not in the Linux<br />
version. There is a long <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=190615">discussion</a> about how it would be in future versions.</p>
<p>The second option "<code>layout.word_select.eat_space_to_next_word</code>" is related to the selection<br />
of surrounding spaces when double-clicking on a word on a page text. I think this option<br />
should be always set to <code>false</code>, because sometimes is really annoying specially when copying<br />
and pasting usernames and passwords. This time the value I consider more useful is the<br />
default in Linux and the opposite happens in Windows. No exact idea of what happens on the<br />
Mac's world <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2008/03/29/firefox-text-selection-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking with LINQ: Moq is here!</title>
		<link>http://weblogs.manas.com.ar/waj/2007/12/19/mocking-with-linq-moq-is-here/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/12/19/mocking-with-linq-moq-is-here/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 16:16:28 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2007/12/19/mocking-with-linq-moq-is-here/</guid>
		<description><![CDATA[In these days of agile and TDD, most probably you had to deal with mocks. In such case, you have two options: create hundreds of mock classes by hand, one for each expected behavior for every unit test, or use a mock framework. There are many mock frameworks available for .NET but all of them [...]]]></description>
			<content:encoded><![CDATA[<p>In these days of agile and TDD, most probably you had to deal with <a href="http://en.wikipedia.org/wiki/Mock_object">mocks</a>. In such case, you have two options: create hundreds of mock classes by hand, one for each expected behavior for every unit test, or use a mock framework.</p>
<p>There are many mock frameworks available for .NET but all of them have one or both of the following problems:</p>
<ol>
<li>The framework rely on the member names as strings, making really difficult the refactoring sessions.</li>
<li>The mock objects are created based on the interaction and sequences of calls.</li>
</ol>
<p>The last problem is probably subjective, but I believe the unit tests should not rely on the interaction of the tested entity with the outside. Otherwise, these tests would be tightly coupled with the implementation making possible refactoring and optimizations almost impossible without rewriting the entire test suite.</p>
<p>So, how to solve these issues and what LINQ has to do with all this? <a href="http://code.google.com/p/moq/">Moq</a> is the answer! And you can read the <a href="http://www.clariusconsulting.net/blogs/kzu/archive/2007/12/18/LinqtoMockMoqisborn.aspx">"official" announcement</a> in the kzu's weblog. I've been working with him in this project as was completely amazing to have the first prototype working in just an hour of pairing and having written just a bunch of dozens of code lines! LINQ rocks! <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Many examples are given in the <a href="http://code.google.com/p/moq/wiki/QuickStart">project site</a>, but I'll copy some of them so you can get excited:</p>
<pre>
var mock = new Mock&lt;ICloneable>();
var clone = new object();

mock.Expect(x => x.Clone()).Returns(clone);

Assert.AreEqual(clone, mock.Instance.Clone());
</pre>
<p>I think the framework interface is self explanatory. Even for more complicated examples like this one with callbacks:</p>
<pre>
var mock = new Mock&lt;IFoo>();
bool called = false;
mock.Expect(x => x.Do1()).Callback(() => called = true).Returns(1);

Assert.AreEqual(1, mock.Instance.Do1());
Assert.IsTrue(called);
</pre>
<p>Or even very flexible filters (powered by LINQ):</p>
<pre>
var mock = new Mock&lt;IFoo>();

mock.Expect(x => x.Duplicate(It.Is&lt;int>(value => value &lt; 5 &#038;& value > 0))).Returns(() => 1);

Assert.AreEqual(1, mock.Instance.Duplicate(3));
</pre>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/12/19/mocking-with-linq-moq-is-here/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>REST/POX client with WCF 3.5</title>
		<link>http://weblogs.manas.com.ar/waj/2007/12/01/rest-pox-client-with-wcf-35/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/12/01/rest-pox-client-with-wcf-35/#comments</comments>
		<pubDate>Sat, 01 Dec 2007 21:56:06 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2007/12/01/rest-pox-client-with-wcf-35/</guid>
		<description><![CDATA[Some time ago I wrote a post about how to create REST clients using WCF and taking advantage of its extensibility to obtain a type safe endpoint. My approach implied create a bunch of WCF behaviors and other extensions that intercepts the messages and customize the representation over the wire. Now with .NET 3.5 the [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I wrote a <a href="http://weblogs.manas.com.ar/waj/2007/05/13/rest-pox-client-with-wcf/">post</a> about how to create REST clients using WCF and taking advantage of its extensibility to obtain a type safe endpoint. My approach implied create a bunch of WCF behaviors and other extensions that intercepts the messages and customize the representation over the wire.</p>
<p>Now with .NET 3.5 the WCF framework is extended to support natively these kind of scenarios and is really easy and clean to use. There are some good tutorials describing these extensions, like <a href="http://www.developer.com/net/article.php/10916_3695436_1">this one</a>.<br />
So, I've put hands on these exciting tools, and migrated my previous example to use the new framework. Ideally I would just remove my customizations, configure the endpoint with a <code>webHttpBinding</code> and a <code>webHttp</code> behavior. But this wont work because in my service contract I have some operations receiving unsupported argument types like <code>int?</code>, <code>DateTime?</code> and <code>string[]</code>. Fortunately, one more time the WCF team did and excellent job providing us the extensibility points we need <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>Instead of configuring directly the <code>webHttp</code> behavior, I've created a custom <code>DeliciousWebHttpBehavior</code> (extending <code>WebHttpBehavior</code>) and overriding only the <code>GetQueryStringConverter</code> method. The <code>QueryStringConverter</code> is responsible of converting the arguments to a string representation and back again. Creating a <code>DeliciousQueryStringConverter</code> that supports our special types is fair enough.</p>
<p>After that I only have to change the attributes in the operations, so for example where in the previous example says:</p>
<pre>
[OperationContract(Action = "/v1/posts/get")]
PostsResponse GetPosts(string tag, DateTime? dt, string url);
</pre>
<p>in the new version is changed by:</p>
<pre>
[OperationContract]
[WebGet(UriTemplate = "/v1/posts/get?tag={tag}&#038;dt={dt}&#038;url={url}")]
PostsResponse GetPosts(string tag, DateTime? dt, string url);
</pre>
<p>Happy RESTing! <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>Download: <a href='http://weblogs.manas.com.ar/waj/wp-content/uploads/2007/12/wcfrestpox35.zip' title='wcfrestpox35.zip'>wcfrestpox35.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/12/01/rest-pox-client-with-wcf-35/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automatic &#039;Organize Usings&#039; when saving files on VS2008</title>
		<link>http://weblogs.manas.com.ar/waj/2007/12/01/automatic-organize-usings-when-saving-files-on-vs2008/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/12/01/automatic-organize-usings-when-saving-files-on-vs2008/#comments</comments>
		<pubDate>Sat, 01 Dec 2007 06:44:52 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2007/12/01/automatic-organize-usings-when-saving-files-on-vs2008/</guid>
		<description><![CDATA[I really love this new feature of VS2008 that allows you to cleanup the 'usings' section of C# files (and I've been always missing this from Eclipse) but I was a bit disappointed when I found there is no choice to execute automatically before every save. Somebody please tell me if I'm wrong! As I'm [...]]]></description>
			<content:encoded><![CDATA[<p>I really love this new feature of VS2008 that allows you to cleanup the 'usings' section of C# files (and I've been always missing this from Eclipse) but I was a bit disappointed when I found there is no choice to execute automatically before every save. Somebody please tell me if I'm wrong!<br />
As I'm little paranoid I wrote this simple macro and reassigned the 'CTRL+S' shortcut to it. The try/catch is needed because it will fail with no C# editors.</p>
<pre>
Public Sub RemoveSortAndSaveSelectedItems()
    Try
        DTE.ExecuteCommand("Edit.RemoveAndSort")
    Catch
    End Try
    DTE.ExecuteCommand("File.SaveSelectedItems")
End Sub
</pre>
<p>Now this will be on my list of the very first things to do when reinstalling a development machine <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/12/01/automatic-organize-usings-when-saving-files-on-vs2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prime generator with LINQ</title>
		<link>http://weblogs.manas.com.ar/waj/2007/10/13/prime-generator-with-linq/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/10/13/prime-generator-with-linq/#comments</comments>
		<pubDate>Sat, 13 Oct 2007 13:01:25 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2007/10/13/prime-generator-with-linq/</guid>
		<description><![CDATA[Maybe this example is not impressive as an entire ray tracer in a single line, but it gives an idea of the expressiveness of LINQ. I remember from my high school times, the internal spontaneous competitions looking for the "fastest" prime generators. We were really far from the greatest and well known prime generators, but [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe this example is not impressive as an <a href="http://blogs.msdn.com/lukeh/archive/2007/10/01/taking-linq-to-objects-to-extremes-a-fully-linqified-raytracer.aspx">entire ray tracer in a single line</a>, but it gives an idea of the expressiveness of LINQ.</p>
<p>I remember from my high school times, the internal spontaneous competitions looking for the "fastest" prime generators. We were really far from the greatest and well known prime generators, but things like that one gave us funny ways to learn interesting stuff. The algorithm used in this post is a naïve version, that has nothing to do with "fast".</p>
<p>Since C# 2.0, we can write custom enumerators very easily and create "generators" like this one:</p>
<pre>
IEnumerable<int> PositiveIntegers
{
  get {
    for (int i = 1; ; i++)
      yield return i;
  }
}
</pre>
<p>Now with LINQ, is very easy to write lazy evaluated sequences like this one:</p>
<pre>
var primes = from n in PositiveIntegers.Skip(1)
             where
                 PositiveIntegers.Skip(1)
                     .TakeWhile(x => x <= Math.Sqrt(n))
                     .All(x => (n % x) > 0)
             select n;
</pre>
<p>or even using subqueries:</p>
<pre>
var primes = from n in PositiveIntegers.Skip(1)
             where
                 (from m in PositiveIntegers.TakeWhile(x => x <= Math.Sqrt(n))
                 where m >= 2 &#038;& (n % m) == 0
                 select m).FirstOrDefault() == 0
             select n;
</pre>
<p>Now you can assume that <code>primes</code> contains all the infinite series of primes! In fact it's a lazy evaluated enumerator, that searches for the next prime every time you call the <code>MoveNext</code> method.</p>
<p>So, you can search the primes less that 100:</p>
<pre>
foreach (var x in primes.TakeWhile(n => n < 100))
    Console.WriteLine(x);
</pre>
<p>the first 100 primes:</p>
<pre>
foreach (var x in primes.Take(100))
    Console.WriteLine(x);
</pre>
<p>or look for the 200th prime:</p>
<pre>
var prime200 = primes.ElementAt(200);
</pre>
<p>Of course, don't try to iterate the entire sequence <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/10/13/prime-generator-with-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASMX to WCF migration</title>
		<link>http://weblogs.manas.com.ar/waj/2007/05/31/asmx-to-wcf-migration/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/05/31/asmx-to-wcf-migration/#comments</comments>
		<pubDate>Thu, 31 May 2007 15:49:55 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/2007/05/31/asmx-to-wcf-migration/</guid>
		<description><![CDATA[In the last weeks I faced a real world example of an ASMX to WCF migration. The existing services has been relying in many of the features available in ASMX, and a complete rewrite would not be an option. Kenny Wolf describes in a post how to reuse an implementation of an ASMX Web Service, [...]]]></description>
			<content:encoded><![CDATA[<p>In the last weeks I faced a real world example of an ASMX to WCF migration. The existing services has been relying in many of the features available in ASMX, and a complete rewrite would not be an option.</p>
<p><a href="http://kennyw.com/">Kenny Wolf</a> describes in a <a href="http://kennyw.com/indigo/86">post</a> how to reuse an implementation of an ASMX Web Service, "double-decorating" the classes to support both ASMX and WCF with the same sources.</p>
<p><a href="http://blogs.msdn.com/kaevans/default.aspx">Kirk Allen Evans</a> <a href="http://blogs.msdn.com/kaevans/archive/2006/10/05/Phased-Migration-From-ASMX-to-WCF.aspx">extends</a> that idea allowing the clients remain unchanged in a first phase. That means the web services' URL remain with the same .asmx extension replacing the ASP.NET build provider:</p>
<pre>
&lt;buildProviders>
  &lt;remove extension=".asmx"/>
  &lt;add extension=".asmx" type="System.ServiceModel.Activation.ServiceBuildProvider, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
&lt;/buildProviders>
</pre>
<p>Based on my experience, I can add some comments:</p>
<h3>1. Action names</h3>
<p>The default action names that WCF assigns for each operation is different than ASMX ones. ASMX uses the namespace appended by the message name, while WCF uses the namespace, the service name and the operation name. This means that if you want to be fully compatible with old clients, you have to specify the <code>Action</code> name:</p>
<pre>
[WebService(Namespace = "http://mynamespace/")]
<b>[ServiceContract(Namespace = "http://mynamespace/")]</b>
public class MyService
{
   [WebMethod]
   <b>[OperationContract(Action = "http://mynamespace/TheWebMethod")]</b>
   public void TheWebMethod()
   {
      // . . .
   }
}
</pre>
<h3>2. Message names</h3>
<p>If you used the <code>MessageName</code> argument for a web method, then you have to specify the corresponding <code>Name</code> argument in the operation contract. Also, the action name must be modified:</p>
<pre>
[WebService(Namespace = "http://mynamespace/")]
<b>[ServiceContract(Namespace = "http://mynamespace/")]</b>
public class MyService
{
   [WebMethod(MessageName = "MyWebMethod")]
   <b>[OperationContract(Action = "http://mynamespace/MyWebMethod", Name = "MyWebMethod")]</b>
   public void TheWebMethod()
   {
      // . . .
   }
}
</pre>
<h3>3. Xml serialization</h3>
<p>In the most common cases you also created your own "data contracts" for ASMX, creating Xml serializable clases. In order to support the same serialization you have to add the <code>XmlSerializerFormat</code> attribute:</p>
<pre>
[WebService(Namespace = "http://mynamespace/")]
[ServiceContract(Namespace = "http://mynamespace/")]
<b>[XmlSerializerAttribute]</b>
public class MyService
{
   [WebMethod]
   [OperationContract(Action = "http://mynamespace/TheWebMethod")]
   public MyDataType TheWebMethod()
   {
      // . . .
   }
}
</pre>
<h3>4. ASP.NET compatibility</h3>
<p>In those cases where you used ASP.NET specific features, or just for example want to have access to the current <code>HttpContext</code> and don't want to change all the source code to make it in the WCF way, you can take advantage of the ASP.NET compatibility mode of WCF.</p>
<pre>
&lt;system.serviceModel>
  &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
&lt;system.serviceModel>
</pre>
<p>The problem with this mode, is that if you replaced the .asmx build provider as I mention before and pointed by <a href="http://blogs.msdn.com/kaevans/archive/2006/10/05/Phased-Migration-From-ASMX-to-WCF.aspx">Kirk</a> your services will fail miserably with an error like this:</p>
<pre>
Unable to cast object of type 'System.Web.Compilation.BuildResultCustomString'
to type 'System.Web.Compilation.BuildResultCompiledType'.
</pre>
<p>Fortunately, there is a very simple solution for this. Just replace the .asmx HTTP handler with the WCF one:</p>
<pre>
&lt;httpHandlers>
  &lt;remove path=".asmx" verb="*" />
  &lt;add path="*.asmx" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false" />
&lt;/httpHandlers>
</pre>
<p>There are also some other important points to take into account in order to be fully compatible with old clients. For example, ASMX allows thin clients to invoke services using HTTP GET and/or PUT request with arguments encoded in the query string and the request body respectively. There are some improvements for this in Orcas that would support this out of the box, but in order to be fully compatible, all this should be supported by the same endpoint at the same time without forgetting the SOAP protocol. I had to deal with this also, but that gives enough material for a future post <img src='http://weblogs.manas.com.ar/waj/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/05/31/asmx-to-wcf-migration/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>REST/POX client with WCF</title>
		<link>http://weblogs.manas.com.ar/waj/2007/05/13/rest-pox-client-with-wcf/</link>
		<comments>http://weblogs.manas.com.ar/waj/2007/05/13/rest-pox-client-with-wcf/#comments</comments>
		<pubDate>Sun, 13 May 2007 15:13:27 +0000</pubDate>
		<dc:creator>Juan Wajnerman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://weblogs.manas.com.ar/waj/?p=14</guid>
		<description><![CDATA[The most remarkable thing in WCF is its extensibility. The Indigo team did a really great work providing hooks almost everywhere, that allows to customize all behaviors in every step of the communication framework. In this example I created a client behavior that communicates with a REST/POX service. Clemens Vasters have an excelent series of [...]]]></description>
			<content:encoded><![CDATA[<p>The most remarkable thing in WCF is its extensibility. The Indigo team did a really great work providing hooks almost everywhere, that allows to customize all behaviors in every step of the communication framework.</p>
<p>In this example I created a client behavior that communicates with a REST/POX service. Clemens Vasters have an excelent series of <a href="http://staff.newtelligence.net/clemensv/PermaLink,guid,2d61b97b-3a6e-46bd-89db-b1b20499ba18.aspx">articles</a> on how to create REST/POX services with WCF. There is also two articles <a href="http://msdn2.microsoft.com/en-us/library/aa738456.aspx">here</a> and <a href="http://msdn2.microsoft.com/en-us/library/aa395208.aspx">there</a> in MSDN but what I don't like about it is that you loose the type safe contracts, having to deal with <code>Message</code> class in order to send a request or decode the response.</p>
<p>What I did to avoid this problem is use a series of extension points in order to customize the request and response messages. In the example I used the <a href="http://del.icio.us/">del.icio.us</a> API.<br />
These are the WCF hooks I've implemented:</p>
<ul>
<li><strong>IEnpointBehavior</strong>: attaches the message inspector and also configures itself as an operation behavior in every contract operation.</li>
<li><strong>IOperationBehavior</strong>: configures the message formatter.</li>
<li><strong>IClientMessageInspector</strong>: customizes the request message suppressing the body, changing the HTTP method and headers, and setting the query string previously created by the message formatter. This is done setting an instance of <code>HttpRequestMessageProperty</code> in the message properties. This is later used by the HTTP channel.</li>
<li><strong>IClientMessageFormatter</strong>: This implements both the request and response customizations. In the <code>SerializeRequest</code> method, it creates the query string and attach it as a property in an empty message. This is later used by the message inspector to create the final request. It also uses the <code>Action</code> value of the <code>OperationContract</code> to use as the relative URI. In the <code>DeserializeReply</code> method, the received message is deserialized as a plain XML using the <code>XmlSerializer</code>.</li>
</ul>
<p>Once the behavior is attached to the client endpoint, the contract methods can be defined as:</p>
<pre>
[OperationContract(Action = "/v1/posts/recent")]
PostsResponse GetRecentPosts(string tag, int? count);
</pre>
<p>That makes a request to the address defined in the <code>Action</code>, with two arguments (tag and count) in the query string.</p>
<p><strong>IMPORTANT NOTE: </strong>Take special care in the usage of this example. It doesn't take into account any of the <a href="http://del.icio.us/help/api/]">important notes</a> about the del.icio.us api usage, and you can be throttled. Use under your own risk. Again, this is just an example.</p>
<p>In order to use this example, first you have to provide your del.icio.us username and password to WCF, setting it in the <code>Program.cs</code>.</p>
<p>There is a number of things to improve. For example, right now it only supports GET requests. It should also handle POST method serializing the arguments as a POX request. Also, the query string arguments formatting should be customizable.</p>
<p>Download the sources: <a href='http://weblogs.manas.com.ar/waj/wp-content/uploads/2007/05/wcfrestpox.zip' title='WcfRestPox.zip'>WcfRestPox.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://weblogs.manas.com.ar/waj/2007/05/13/rest-pox-client-with-wcf/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

