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

<channel>
	<title>Malvin Ly</title>
	<atom:link href="http://malvinly.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://malvinly.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 07:07:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='malvinly.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Malvin Ly</title>
		<link>http://malvinly.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://malvinly.com/osd.xml" title="Malvin Ly" />
	<atom:link rel='hub' href='http://malvinly.com/?pushpress=hub'/>
		<item>
		<title>Castle Windsor 3 &#8211; Classes and Types</title>
		<link>http://malvinly.com/2012/02/07/castle-windsor-3-classes-and-types/</link>
		<comments>http://malvinly.com/2012/02/07/castle-windsor-3-classes-and-types/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 07:07:35 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=220</guid>
		<description><![CDATA[In my previous post on strongly typed sessions in ASP.NET, I had to manually look through the current assembly and retrieve all interfaces that end in &#8220;Session&#8221;. To register these interfaces with my container, I had to loop through each type found and register them one-by-one: Unfortunately, Castle Windsor 2.x did not have support for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=220&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my previous post on <a href="http://malvinly.com/2011/03/03/strongly-typed-asp-net-session/">strongly typed sessions in ASP.NET</a>, I had to manually look through the current assembly and retrieve all interfaces that end in &#8220;Session&#8221;.  To register these interfaces with my container, I had to loop through each type found and register them one-by-one:</p>
<p><pre class="brush: csharp;">
IEnumerable&lt;Type&gt; sessions =
    Assembly
        .GetExecutingAssembly()
        .GetTypes()
        .Where(x =&gt; x.IsInterface &amp;&amp; x.Name.EndsWith(&quot;Session&quot;));

foreach (Type session in sessions)
{
    Type sessionTemp = session;

    container.Register
    (
        Component
            .For(session)
            .UsingFactoryMethod(x =&gt;
            {
                var factory = x.Resolve&lt;DictionaryAdapterFactory&gt;();
                return factory.GetAdapter&lt;object&gt;(sessionTemp, new SessionDictionary(HttpContext.Current.Session));
            })
            .LifeStyle.PerWebRequest
    );
</pre></p>
<p>Unfortunately, Castle Windsor 2.x did not have support for finding and registering interfaces with no implementions for use with a <a href="http://stw.castleproject.org/Windsor.Registering-components-one-by-one.ashx#Using_a_delegate_as_component_factory_6">factory method</a> or <a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx">typed factories</a>.  I also couldn&#8217;t take advantage of the rich registration API of the <code>AllTypes</code> class since <code>AllTypes</code> only returned concrete classes.</p>
<p>Enter Castle Windsor 3.  In version 3, Castle Windsor introduced two new registration classes, <code>Classes</code> and <code>Types</code>.  The <code>Classes</code> class is identical to <code>AllTypes</code>.  The new <code>Types</code> class returns all types, including interfaces, abstract classes, and concrete classes.</p>
<p>We can now update the registration code above to take advance of the new <code>Types</code> class:</p>
<p><pre class="brush: csharp;">
container.Register
(
    Types
        .FromThisAssembly()
        .Where(t =&gt; t.IsInterface &amp;&amp; t.Name.EndsWith(&quot;Session&quot;))
        .Configure(c =&gt; c.UsingFactoryMethod(f =&gt;
        {
            var factory = f.Resolve&lt;DictionaryAdapterFactory&gt;();
            return factory.GetAdapter&lt;object&gt;(c.Implementation, new SessionDictionary(HttpContext.Current.Session));
        })) 
        .LifestylePerWebRequest()
);
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/220/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=220&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2012/02/07/castle-windsor-3-classes-and-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
		<item>
		<title>Forcing classes to implement ToString()</title>
		<link>http://malvinly.com/2012/02/06/forcing-classes-to-implement-tostring/</link>
		<comments>http://malvinly.com/2012/02/06/forcing-classes-to-implement-tostring/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 01:31:51 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=215</guid>
		<description><![CDATA[There are situations where it would be useful to force a class that implements an interface to override the ToString() method. In this case, I can execute a list of tasks and log any exceptions that occur. The ToString() method can provide a way to log information about the state or progress of a task [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=215&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are situations where it would be useful to force a class that implements an interface to override the <code>ToString()</code> method.  </p>
<p><pre class="brush: csharp;">
public interface ITask
{
	void Execute();
	string ToString();
}
</pre></p>
<p>In this case, I can execute a list of tasks and log any exceptions that occur.  The <code>ToString()</code> method can provide a way to log information about the state or progress of a task when an exception occurs. The exception and stack trace may not be able to provide enough information in all situations.</p>
<p><pre class="brush: csharp;">
foreach (ITask task in tasks)
{
	try
	{
		task.Execute();
	}
	catch (Exception e)
	{
		Log.Error(&quot;Error occurred: &quot; + task.ToString(), e);
	}
}
</pre></p>
<p>Unfortunately, there isn&#8217;t a way to force a class to override the <code>ToString()</code> method, even if the method signature is present on the interface.  Instead, we can use abstract classes to enforce this by using the <code>abstract</code> and <code>override</code> modifiers together:</p>
<p><pre class="brush: csharp;">
public abstract class Task
{
	public abstract void Execute();
	public abstract override string ToString();
}
</pre></p>
<p>Any classes that derive from <code>Task</code> will be required to override the <code>ToString()</code> method.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/215/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=215&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2012/02/06/forcing-classes-to-implement-tostring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
		<item>
		<title>Hash collision vulnerability</title>
		<link>http://malvinly.com/2012/02/06/hash-collision-vulnerability/</link>
		<comments>http://malvinly.com/2012/02/06/hash-collision-vulnerability/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 06:00:38 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=205</guid>
		<description><![CDATA[On December 28, 2011, two security researchers presented an effective way to perform a DoS attack on many websites at the 28C3 conference. Their presentation can be found on Youtube. Many web frameworks use hash tables to store data being sent from a client. Since hash tables are efficient at storing key value pairs, this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=205&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On December 28, 2011, two security researchers presented an effective way to perform a DoS attack on many websites at the 28C3 conference.  Their presentation can be found on <a href="http://www.youtube.com/watch?v=R2Cq3CLI6H8">Youtube</a>.</p>
<p>Many web frameworks use hash tables to store data being sent from a client.  Since hash tables are efficient at storing key value pairs, this makes them ideal for storing query strings or POST data.</p>
<p>At a basic level, hash tables are just an array of lists.  You&#8217;ll typically see hash tables used in this form for storing key value pairs in C#:</p>
<p><pre class="brush: csharp;">
hashtable[&quot;pepsi&quot;] = &quot;soda&quot;;
hashtable[&quot;pizza&quot;] = &quot;food&quot;;
</pre></p>
<p>To store this information, the keys &#8220;pepsi&#8221; and &#8220;pizza&#8221; will be hashed to an index using a deterministic hash function where the index is less than the size of the hash table. For example:</p>
<p><pre class="brush: csharp;">
Hash(&quot;pepsi&quot;) = 2
Hash(&quot;pizza&quot;) = 5
</pre></p>
<p>In a best case scenario, an operation will take constant time <i>O(1)</i> since it does not rely on the number of entries in a table. A large number of the web frameworks use the hash function &#8220;djb2&#8243; or a variation of it.  For example, this is the hash function (DJBX33A) used in PHP5:</p>
<p><pre class="brush: csharp;">
public ulong Hash(string input)
{
    ulong hash = 5381;
   
    foreach (char c in input)
        hash = ((hash &lt;&lt; 5) + hash) + c;

    return hash;
}
</pre></p>
<p>If we hash a list of strings, we can see the resulting hash is different for each string:</p>
<p><pre class="brush: csharp;">
string[] keys = new string[]
{
    &quot;hello world&quot;,
    &quot;Hello World&quot;,
    &quot;HELLO WORLD&quot;,
    &quot;hELlO wOrLd&quot;
};

foreach (string key in keys)
    Console.WriteLine (Hash(key));
</pre></p>
<p>And the output&#8230;</p>
<p><pre class="brush: plain;">
13876786532495509697
13827776004929097857
13826244427234115457
13875256318590737441
</pre></p>
<p>The hash function above is a fast and simple algorithm for generating string hashes.  But due to its simplicity, it&#8217;s susceptible to hash collisions.  It&#8217;s easy for an attacker to create many keys that generate the same hash.</p>
<p>In a worst case scenario, hash tables require <i>O(n)</i> time for look-up or delete operations since it has to iterate through the entire list under an index.  This can become very slow if there are a large number of items that are hashed to the same index.  To demonstrate an example hash collision, we can hash this list of strings using the Hash method we defined above:</p>
<p><pre class="brush: csharp;">
string[] keys = new string[]
{
    &quot;FYFYFYEzFYFYFYEzFYEzFYFYFYFYFY&quot;,
    &quot;EzFYEzEzEzFYFYFYFYEzEzFYEzEzFY&quot;,
    &quot;EzEzEzEzEzEzEzEzEzEzEzEzEzEzEz&quot;,
    &quot;FYFYFYFYFYFYFYFYFYFYFYFYFYFYFY&quot;,
    &quot;FYEzEzFYFYEzEzFYFYFYFYEzFYFYFY&quot;
};
</pre></p>
<p>If we run this through our Hash method and print the results, we&#8217;ll see that the resulting hashes are identical:</p>
<p><pre class="brush: plain;">
10499256222776351254
10499256222776351254
10499256222776351254
10499256222776351254
10499256222776351254
</pre></p>
<p>Using this information, we can use these keys that cause a hash collision to generate query strings or POST data for our request:</p>
<p><pre class="brush: plain;">
EzEzEzEzEzEzEzEzEzEzEzEzEzEzEz=&amp;EzEzEzEzEzEzEzEzEzEzEzEzEzEzFY=&amp;EzEzEzEzEzEzEzEzEzEzEzEzEzFYEz=
</pre></p>
<p>A 1MB file with pre-computed values will contain over 30,000 collisions.  A web framework will attempt to parse and insert all this data into a hash table.  The time required for any operations will increase over time as more entries are inserted into the table.  Each insert operation will need to iterate over the current list of entries. Using the formula <img src='http://s0.wp.com/latex.php?latex=%5Csum_%7Bi%3D1%7D%5En%5Cfrac%7Bn%28n%2B1%29%7D%7B2%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;sum_{i=1}^n&#92;frac{n(n+1)}{2}' title='&#92;sum_{i=1}^n&#92;frac{n(n+1)}{2}' class='latex' />, we can see that there will be around 450,000,000 string comparisions to check for duplicates.  This is only for a single 1MB request.  Proof-of-concept samples were made available by the presenters <a href="https://github.com/koto/blog-kotowicz-net-examples/tree/master/hashcollision">here</a>.</p>
<p>The numbers below were taken from the original presentation:</p>
<ul>
<li>PHP:
<ul>
<li>Single 500k POST: 30 seconds to process.</li>
<li>Single 8MB POST: 288 minutes of CPU time.</li>
<li>1 Gbit/s connection would allow you to keep 10,000 Core i7 processors busy.</li>
</ul>
</li>
<li>ASP.NET
<ul>
<li>Single 4MB POST: 650 minutes of CPU time (IIS typically limits to 90 seconds).</li>
<li>1 Gbit/s connection would allow you to keep 30,000 Core2 processors busy.</li>
</ul>
</li>
</ul>
<p>We have a website that runs on PHP and uses the Apache HTTP Server.   At the time of this exploit, PHP did not have a workaround available in their stable builds.  To address this issue, we set the <a href="http://httpd.apache.org/docs/2.0/mod/core.html#limitrequestbody">LimitRequestBody</a> size to 100k. We used this code to test our servers running PHP:</p>
<p><pre class="brush: csharp;">
for (int i = 0; i &lt; 10; i++)
{
    Task.Factory.StartNew(() =&gt;
    {
        string data = File.ReadAllText(@&quot;data.txt&quot;);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(&quot;http://example.com&quot;);
        request.Method = &quot;POST&quot;;
        request.ContentType = &quot;application/x-www-form-urlencoded&quot;;
        request.ContentLength = data.Length;

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            writer.Write(data);
    });
}
</pre></p>
<p>Before we applied the update, the CPU usage would remain at 100% until we restarted Apache.  After the update, CPU usage would only spike for a split second.</p>
<p>For ASP.NET, Microsoft released an out-of-band security update on December 29, 2011.  This update limits the number of parameters allowed in a single request to 1000.  To test our servers running ASP.NET, we used this code:</p>
<p><pre class="brush: csharp;">
string data = String.Join(&quot;=test&amp;&quot;, Enumerable.Range(0, 1020));
           
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(&quot;http://example.com&quot;);

request.Method = &quot;POST&quot;;
request.ContentType = &quot;application/x-www-form-urlencoded&quot;;
request.ContentLength = data.Length;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    writer.Write(data);
</pre></p>
<p>The code above generates 1020 parameters for our POST request.  Before the update, we should expect to see a normal page response.  After the update, ASP.NET should throw an exception:</p>
<p><img src="http://nivlam.files.wordpress.com/2012/02/error.png?w=600" style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" /></p>
<p>As you can see in the stacktrace, the exception is being thrown in the <code>ThrowIfMaxHttpCollectionKeysExceeded()</code> method, which checks to see if the number of parameters has exceeded 1000.</p>
<p>On January 10, 2012, PHP released version 5.3.9, which added the <code>max_input_vars</code> directive.  This is similiar to Microsoft&#8217;s workaround, which limits the number of parameters in a single request.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=205&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2012/02/06/hash-collision-vulnerability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>

		<media:content url="http://nivlam.files.wordpress.com/2012/02/error.png" medium="image" />
	</item>
		<item>
		<title>Loading types at runtime</title>
		<link>http://malvinly.com/2011/09/23/loading-types-at-runtime/</link>
		<comments>http://malvinly.com/2011/09/23/loading-types-at-runtime/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 11:43:31 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=191</guid>
		<description><![CDATA[I have a library that contains a factory, which creates instances of a type that implement an interface. Currently the factory, interface, and the implementations are in the same assembly. For maintainability, I&#8217;d like to separate the implementations from the core library and put them into their own assemblies. This will allow me to version [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=191&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have a library that contains a factory, which creates instances of a type that implement an interface.  Currently the factory, interface, and the implementations are in the same assembly.  For maintainability, I&#8217;d like to separate the implementations from the core library and put them into their own assemblies.  This will allow me to version each implementation independently from the core library.  Any applications that need to use certain implementations will simply need to reference the core library and any number of libraries that contain the implementations.  The factory in the core library should be able to determine which implementations are available at runtime.</p>
<p>Here I have a class library <code>Demo.Core</code> that contains an interface definition and a factory.  There are no implementations for <code>IPrinter</code> in the core library.  I have two additional class libraries called <code>Demo.GreenPrinter</code> and <code>Demo.RedPrinter</code> that contains implementations for <code>IPrinter</code>.  The two libraries that contain the implementations references the core library, but the core library does not reference either of the libraries that contain the implementations.  The console application needs to references the core library and at least one of the additional libraries.</p>
<p><img src="http://nivlam.files.wordpress.com/2011/09/solution_explorer.png?w=600" style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" /></p>
<p>The interface contains two methods.  <code>CanHandle</code> contains logic to determine if a printer should be used and the <code>Print</code> method prints the message to the screen.</p>
<p><pre class="brush: csharp;">
public interface IPrinter
{
    bool CanHandle(string color);
    void Print(string message); 
}
</pre></p>
<p>The managed extensibility framework (MEF) makes it easy for me to accomplish what I&#8217;m trying to do.  The factory contains a static list of <code>IPrinter</code>s and is populated by the code in the static constructor using MEF.  Unfortunately, MEF does not support static imports. I did not use MEF to automatically compose the <code>PrinterFactory</code> because I think it&#8217;s unnecessary to new up instances of each printer when only one is going to be returned, hence the static list and constructor.  </p>
<p><pre class="brush: csharp;">
public class PrinterFactory
{
    private static IList&lt;IPrinter&gt; Printers { get; set; }

    static PrinterFactory()
    {
        var catalog = new DirectoryCatalog(Environment.CurrentDirectory, &quot;Demo.*&quot;);
        var container = new CompositionContainer(catalog);

        Printers = container.GetExportedValues&lt;IPrinter&gt;().ToList();
    }
        
    public IPrinter Create(string color)
    {
        foreach (IPrinter printer in Printers)
        {
            if (printer.CanHandle(color))
                return printer;
        }

        return null;
    }
}
</pre></p>
<p>I&#8217;m using the <code>DirectoryCatalog</code> to find the assemblies.  The core library and the libraries that contain the implementations will normally be copied to the same bin directory since they are both referenced by the console application. My console application can now create a <code>PrinterFactory</code> to create an <code>IPrinter</code>.</p>
<p><pre class="brush: csharp;">
static void Main(string[] args)
{
	var factory = new PrinterFactory();
	RedPrinter printer = factory.Create(&quot;red&quot;) as RedPrinter;

	printer.Print(&quot;hello world&quot;);
}
</pre></p>
<p>This is all I would need to do if my core library was targeting .NET 4.0.  However, if my core library needs to target an older version of the framework (2.0, 3.0, 3.5), I cannot use MEF without adding an extra dependency. At this point, I need to fall back to loading assemblies and using the <code>Activator</code> to create an instance of each <code>IPrinter</code>.  The factory&#8217;s static constructor now looks like:</p>
<p><pre class="brush: csharp;">
static PrinterFactory()
{
	string[] paths = Directory.GetFiles(Environment.CurrentDirectory, &quot;Demo.*.dll&quot;);

	foreach (string path in paths)
		Assembly.LoadFrom(path);

	Printers = AppDomain.CurrentDomain
		.GetAssemblies()
		.SelectMany(x =&gt; x.GetTypes())
		.Where(x =&gt; typeof(IPrinter).IsAssignableFrom(x) &amp;&amp; !x.IsAbstract &amp;&amp; !x.IsInterface)
		.Select(x =&gt; (IPrinter)Activator.CreateInstance(x))
		.ToList();
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/191/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=191&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/09/23/loading-types-at-runtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>

		<media:content url="http://nivlam.files.wordpress.com/2011/09/solution_explorer.png" medium="image" />
	</item>
		<item>
		<title>Disabling Visual Studio extension&#8217;s menu item</title>
		<link>http://malvinly.com/2011/08/16/disabling-visual-studio-extensions-menu-item/</link>
		<comments>http://malvinly.com/2011/08/16/disabling-visual-studio-extensions-menu-item/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 06:01:18 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=185</guid>
		<description><![CDATA[I&#8217;ve created a Visual Studio extension with a new menu item that appears under the Tools menu. However, when there are no solutions open, I want to disable the menu item. When I first create a Visual Studio package, an Initialize method is auto-generated: MenuCommand does not have any events that I can hook into, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=185&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created a Visual Studio extension with a new menu item that appears under the Tools menu.  However, when there are no solutions open, I want to disable the menu item. </p>
<p>When I first create a Visual Studio package, an <code>Initialize</code> method is auto-generated:</p>
<p><pre class="brush: csharp;">
protected override void Initialize()
{
	Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, &quot;Entering Initialize() of: {0}&quot;, this.ToString()));
	base.Initialize();

	// Add our command handlers for menu (commands must exist in the .vsct file)
	OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
	if ( null != mcs )
	{
		// Create the command for the menu item.
		CommandID menuCommandID = new CommandID(GuidList.guidMyVSPackageCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
		MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
		mcs.AddCommand( menuItem );
	}
}
</pre></p>
<p><code>MenuCommand</code> does not have any events that I can hook into, but changing the type to <code>OleMenuCommand</code> exposes the <code>BeforeQueryStatus</code> event that allows me to execute code before the status of the command is checked.</p>
<p><pre class="brush: csharp;">
OleMenuCommand menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID );
mcs.AddCommand( menuItem );

menuItem.BeforeQueryStatus += (sender, evt) =&gt;
{
	// I don't need this next line since this is a lambda.
	// But I just wanted to show that sender is the OleMenuCommand.
	OleMenuCommand item = (OleMenuCommand) sender;

	DTE service = (DTE) this.GetService(typeof (DTE));
	Solution solution = service.Solution;

	item.Enabled = solution.IsOpen;
};
</pre></p>
<p>However, this does not work as expected.  When I first launch Visual Studio without a solution loaded, the  menu item is still enabled.  What I found was that the <code>Initialize</code> method is not called until the first time you click on your menu item.  This code works after I&#8217;ve clicked on my menu item once, but I want the menu item disabled by default when Visual Studio first opens. </p>
<p>To disable the menu item by default, I&#8217;ll need to add a command flag.  In the .vsct file that is auto-generated, I can add a command flag to the button item to have it disabled by default.</p>
<p><pre class="brush: xml;">
  &lt;Button guid=&quot;guidMyVSPackageCmdSet&quot; id=&quot;cmdidMyCommand&quot; priority=&quot;0x0100&quot; type=&quot;Button&quot;&gt;
	&lt;Parent guid=&quot;guidMyVSPackageCmdSet&quot; id=&quot;MyMenuGroup&quot; /&gt;
	&lt;Icon guid=&quot;guidImages&quot; id=&quot;bmpPic1&quot; /&gt;
	&lt;CommandFlag&gt;DefaultDisabled&lt;/CommandFlag&gt;
	&lt;Strings&gt;
	  &lt;CommandName&gt;cmdidMyCommand&lt;/CommandName&gt;
	  &lt;ButtonText&gt;My Menu Item&lt;/ButtonText&gt;
	&lt;/Strings&gt;
  &lt;/Button&gt;
</pre></p>
<p>This does successfully disable the menu item by default when Visual Studio is launched without a solution.  However, since the button is now disabled, I cannot click on it, even after a solution is open.  Since the package isn&#8217;t loaded until the first time I click on the menu item, the package is never loaded and the <code>Initialize</code> method is never called.  </p>
<p>To fix this problem, I&#8217;ll need the package to automatically load once a solution is opened in Visual Studio, regardless if I&#8217;ve clicked on the menu item or not.  I need to add an attribute to my package class:</p>
<p><pre class="brush: csharp;">
[ProvideAutoLoad(&quot;f1536ef8-92ec-443c-9ed7-fdadf150da82&quot;)]
</pre></p>
<p>This attribute forces my package to load when a specific UI context is available.  In this case, the GUID here represents when a solution exists.  There are a number of other UI context GUIDs available under the <code>VSConstants</code> enum.</p>
<p>My extension now works as expected.  When I launch Visual Studio without a solution open, the menu item is disabled.  Once I open a solution and click on the the Tools menu, the <code>BeforeQueryStatus</code> event is triggered and my menu item is enabled.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/185/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=185&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/08/16/disabling-visual-studio-extensions-menu-item/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
		<item>
		<title>Promoting better coding practices in a corporate environment: A lost cause?</title>
		<link>http://malvinly.com/2011/08/15/promoting-better-coding-practices-in-a-corporate-environment-a-lost-cause/</link>
		<comments>http://malvinly.com/2011/08/15/promoting-better-coding-practices-in-a-corporate-environment-a-lost-cause/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 13:30:40 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=180</guid>
		<description><![CDATA[Testability seems to be a misunderstood concept. I regularly see this type of code in reviews: I often see code that violates the single responsibility principle and developers have a difficult time testing the component in question. To me, having code that is difficult to test should raise a red flag. But I see many [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=180&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Testability seems to be a misunderstood concept. I regularly see this type of code in reviews:</p>
<p><pre class="brush: csharp;">
public class MyBusinessClass
{
	private MyDataAccess _dataAccess;

	public MyBusinessClass(bool isTest)
	{
		if (isTest)
			_dataAccess = new MockDataAccess();
		else
			_dataAccess = new ProductionDataAccess();
	}
}
</pre></p>
<p>I often see code that violates the single responsibility principle and developers have a difficult time testing the component in question. To me, having code that is difficult to test should raise a red flag. But I see many developers continue and write the same code, thinking that this is just a side effect of unit testing.</p>
<p>People tend to gravitate towards the simplest working solution at the time for the code they are working on. Normally that is a great thing. We want to keep things simple. However, I don’t believe simple “hacks” are correct solution at any time. As the project grows, the maintainability issues that these simple hacks introduce are compounded. I honestly can’t tell if people can’t see the problems they are about to run into, or they simply don’t care.</p>
<p>I often suggest using an inversion of control container to help manage dependencies, but people seem to reject using a container due to the fact that it adds complexity. I completely understand that a container may not be appropriate in all situations, but when you start writing static classes/factories to manage dependencies, people should reconsider.</p>
<p>How do I help promote better coding practices? Is it possible to change this culture? In a corporate environment with project deadlines, pressure from management, etc…, should people care about these topics?</p>
<p>Perhaps I’m just naive and I want to believe that people care, but I see conflicting opinions everyday. I have been attempting to give feedback, but have been met with much resistance. I am fully aware of the <a href="http://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect" target="_blank">Dunning-Kruger effect</a>, and strive to understand a topic before speaking about it. I realize that there isn’t one right way to do things and I absolutely respect other people’s opinion. However, I fully believe that there are ways to do things that introduce unnecessary pain and pressure points.</p>
<p>Is it worth trying to educate and encourage? In my current situation, I feel powerless to instigate real change. Is this a lost cause and I should just accept the fact that this will always be a problem in a corporate environment that is focused on product delivery rather than quality?  What can I do to get things started?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=180&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/08/15/promoting-better-coding-practices-in-a-corporate-environment-a-lost-cause/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
		<item>
		<title>Dynamically composing predicates</title>
		<link>http://malvinly.com/2011/07/12/dynamically-composing-predicates/</link>
		<comments>http://malvinly.com/2011/07/12/dynamically-composing-predicates/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 12:39:36 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=154</guid>
		<description><![CDATA[Typically when I&#8217;m using LINQ to filter collections, my conditions aren&#8217;t very complex and usually contain a single boolean condition. For example, if I wanted to filter a collection of WorkItems, I&#8217;ll use the Where extension method: I&#8217;ve come across a scenario where I needed to chain multiple &#8220;OR&#8221; expressions. It&#8217;s easy to chain multiple [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=154&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Typically when I&#8217;m using LINQ to filter collections, my conditions aren&#8217;t very complex and usually contain a single boolean condition.  For example, if I wanted to filter a collection of <code>WorkItems</code>, I&#8217;ll use the <code>Where</code> extension method:</p>
<p><pre class="brush: csharp;">
workItems.Where(x =&gt; x.Status == Status.Done);
</pre></p>
<p>I&#8217;ve come across a scenario where I needed to chain multiple &#8220;OR&#8221; expressions.  It&#8217;s easy to chain multiple &#8220;OR&#8221; expressions during compile time:</p>
<p><pre class="brush: csharp;">
workItems.Where(x =&gt; x.Status == Status.Done || x.Status == Status.Processing);
</pre></p>
<p>However, this is difficult to do if the conditions are determined at runtime.  In an application, I&#8217;m asking the user to select from a list of statuses. They may select one or more statuses.</p>
<p><img src="http://nivlam.files.wordpress.com/2011/07/checkboxes.png?w=600" style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" /></p>
<p>Since I still work a lot with stored procedures, I&#8217;ll typically use dynamic SQL to generate a WHERE clause if these status were stored in a database.  But with a collection, this isn&#8217;t easy to do without some help.</p>
<p>After looking around, I stumbled across the <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" title="PredicateBuilder" target="_blank">PredicateBuilder</a> class that ships as part of <a href="http://www.albahari.com/nutshell/linqkit.aspx" title="LINQKit" target="_blank">LINQKit</a>.</p>
<p>The <code>PredicateBuilder</code> allows me to easily chain multiple &#8220;AND&#8221; and &#8220;OR&#8221; conditions together at runtime.  I can append an &#8220;OR&#8221; condition to the predicate if the respective checkbox is checked: </p>
<p><pre class="brush: csharp;">
var predicate = PredicateBuilder.False&lt;WorkItem&gt;();

if (cbIdle.Checked)
    predicate = predicate.Or(x =&gt; x.Status == Status.Idle);

if (cbProcessing.Checked)
    predicate = predicate.Or(x =&gt; x.Status == Status.Processing);

if (cbDone.Checked)
    predicate = predicate.Or(x =&gt; x.Status == Status.Done);

workItems.Where(predicate.Compile());
</pre></p>
<p>The <code>PredicateBuilder</code> also allows me to nest predicates and create complex conditions.  For example, if I wanted to create a predicate that is equivalent to the following:</p>
<p><pre class="brush: csharp;">
workItems.Where(x =&gt; 
    x.Status == Status.Done &amp;&amp;
    (x.Description.Contains(&quot;report&quot;) || x.Description.Contains(&quot;summary&quot;))
);
</pre> </p>
<p>&#8230; I can create two predicates and append them together.</p>
<p><pre class="brush: csharp;">
var description = PredicateBuilder.False&lt;WorkItem&gt;();
description = description.Or(x =&gt; x.Description.Contains(&quot;report&quot;));
description = description.Or(x =&gt; x.Description.Contains(&quot;summary&quot;));

var condition = PredicateBuilder.True&lt;WorkItem&gt;();
condition = condition.And(x =&gt; x.Status == Status.Done);
condition = condition.And(description);

workItems.Where(condition.Compile());
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=154&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/07/12/dynamically-composing-predicates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>

		<media:content url="http://nivlam.files.wordpress.com/2011/07/checkboxes.png" medium="image" />
	</item>
		<item>
		<title>Custom model binding</title>
		<link>http://malvinly.com/2011/06/22/custom-model-binding/</link>
		<comments>http://malvinly.com/2011/06/22/custom-model-binding/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 12:08:07 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=135</guid>
		<description><![CDATA[The default model binder in ASP.NET MVC does a good job of binding items that follow a certain naming convention. But if you&#8217;re using third party controls, you typically do not have control over the HTML they generate. One of the third party controls I&#8217;m using in my project allows a user to select a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=135&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The default model binder in ASP.NET MVC does a good job of binding items that follow a certain naming convention. But if you&#8217;re using third party controls, you typically do not have control over the HTML they generate.  One of the third party controls I&#8217;m using in my project allows a user to select a date range.  At a minimum, the generated HTML looks something like this:</p>
<p><pre class="brush: xml;">
&lt;p&gt;
    Start date:
    &lt;input type=&quot;text&quot; name=&quot;StartMonth&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;StartDay&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;StartYear&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
    End date:
    &lt;input type=&quot;text&quot; name=&quot;EndMonth&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;EndDay&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;EndYear&quot; /&gt;
&lt;/p&gt;
</pre></p>
<p>I want to bind the results to my <code>DateRange</code> model:</p>
<p><pre class="brush: csharp;">
public class DateRange
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}
</pre></p>
<p>ASP.NET MVC provides an extension point to do custom model binding. We simply need to implement <code>IModelBinder</code> and register it in <code>global.asax.cs</code> in the <code>Application_Start</code> method:</p>
<p><pre class="brush: csharp;">
public class DateRangeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        int startMonth = this.GetValue(bindingContext, &quot;StartMonth&quot;);
        int startDay = this.GetValue(bindingContext, &quot;StartDay&quot;);
        int startYear = this.GetValue(bindingContext, &quot;StartYear&quot;);

        int endMonth = this.GetValue(bindingContext, &quot;EndMonth&quot;);
        int endDay = this.GetValue(bindingContext, &quot;EndDay&quot;);
        int endYear = this.GetValue(bindingContext, &quot;EndYear&quot;);

        return new DateRange
        {
            Start = new DateTime(startYear, startMonth, startDay),
            End = new DateTime(endYear, endMonth, endDay)
        };
    }

    private int GetValue(ModelBindingContext context, string name)
    {
        return (int)context
            .ValueProvider
            .GetValue(name)
            .ConvertTo(typeof(int));
    }
}
</pre></p>
<p><pre class="brush: csharp;">
ModelBinders.Binders.Add(typeof(DateRange), new DateRangeModelBinder());
</pre></p>
<p>I&#8217;m omitting error checking in the custom model binder for this post, but normally you&#8217;ll want to catch format exceptions, add model state errors, etc&#8230;. At this point, I can add a <code>DateRange</code> as the action method parameter and get the expected result.</p>
<p><pre class="brush: csharp;">
[HttpPost]
public ActionResult Index(DateRange input)
{
    return View();
}
</pre></p>
<p>If I want to keep my form submission as simple as this, everything works perfectly. However, the model binding above does not work if I create a new model with <code>DateRange</code> as a property.</p>
<p><pre class="brush: csharp;">
public class Incoming
{
    public string Name { get; set; }
    public DateRange DateRange { get; set; }
}
</pre></p>
<p>In the <code>Incoming</code> model, I&#8217;ve added a <code>Name</code> property and a <code>DateRange</code> property.  My form submission now includes other information in addition to a date range.  When I swap out the <code>DateRange</code> parameter in my action method with the <code>Incoming</code> class, the model binding does not work as expected. If I put a breakpoint inside the action method and inspect the input, I&#8217;ll see that the date range is null.</p>
<p>The problem seems to indicate that the <code>DefaultModelBinder</code> in ASP.NET MVC does not check the type, but rather matches on names. In this case, my property is named &#8220;DateRange&#8221; and since my HTML is generated by a third party control, I can&#8217;t change the names.  What I need to do is override the <code>DefaultModelBinder</code> and replace it with a custom one that checks property types.</p>
<p><pre class="brush: csharp;">
public class CustomModelBinder : DefaultModelBinder
{
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.PropertyType != typeof(DateRange))
        {
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            return;
        }

        DateRangeModelBinder modelBinder = new DateRangeModelBinder();
        object dateProperty = modelBinder.BindModel(controllerContext, bindingContext);
        propertyDescriptor.SetValue(bindingContext.Model, dateProperty);
    }
}
</pre></p>
<p>I&#8217;ve created a custom model binder and have overridden the <code>BindProperty</code> method. If the property is a type of <code>DateRange</code>, I&#8217;ll use the <code>DateRangeModelBinder</code> and manually map the property on the model.  I also need to register it in <code>global.asax.cs</code> in the <code>Application_Start</code> method:</p>
<p><pre class="brush: csharp;">
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
</pre></p>
<p>At this point, I&#8217;ve set the default model binder to the one I&#8217;ve created and everything works as expected.  But this only works for the date range control.  If I had multiple third party controls, I don&#8217;t want to pollute the <code>CustomModelBinder</code> with multiple <code>if</code> statements. To make it easier to add additional model binders in the future, I&#8217;ve created an interface:</p>
<p><pre class="brush: csharp;">
public interface IPropertyBinder
{
    bool ShouldHandle(Type propertyType);
    void Bind(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor);
}
</pre></p>
<p>The implementation for the date range control looks like this:</p>
<p><pre class="brush: csharp;">
public class DateRangePropertyBinder : IPropertyBinder
{
    public bool ShouldHandle(Type propertyType)
    {
        return propertyType == typeof(DateRange);
    }

    public void Bind(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        DateRangeModelBinder modelBinder = new DateRangeModelBinder();
        object dateProperty = modelBinder.BindModel(controllerContext, bindingContext);
        propertyDescriptor.SetValue(bindingContext.Model, dateProperty);
    }
}
</pre></p>
<p>The <code>ShouldHandle</code> method will check to see if it&#8217;s the type expected. The <code>Bind</code> method will use <code>DateRangeModelBinder</code> and manually set the property. To use this new interface, I&#8217;ll need to update the <code>CustomModelBinder</code>.</p>
<p><pre class="brush: csharp;">
public class CustomModelBinder : DefaultModelBinder
{
    private readonly IPropertyBinder[] propertyBinders;

    public CustomModelBinder(IPropertyBinder[] propertyBinders)
    {
        this.propertyBinders = propertyBinders;
    }

    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        foreach (IPropertyBinder binder in propertyBinders)
        {
            if (binder.ShouldHandle(propertyDescriptor.PropertyType))
            {
                binder.Bind(controllerContext, bindingContext, propertyDescriptor);
                return;
            }
        }

        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }
}
</pre></p>
<p>I&#8217;ve updated the <code>BindProperty</code> method to loop over the list of property binders. The list of property binders is typically supplied by an inversion of control container.  Again, I&#8217;ll need to update the <code>Application_Start</code> method in <code>global.asax.cs</code>.</p>
<p><pre class="brush: csharp;">
CustomModelBinder modelBinder = container.Resolve&lt;CustomModelBinder&gt;();
ModelBinders.Binders.DefaultBinder = modelBinder;
</pre></p>
<p>To add additional model binders in the future, I simply need to implement the <code>IPropertyBinder</code> interface and make sure that it&#8217;s registered with my inversion of control container.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=135&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/06/22/custom-model-binding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET MVC Areas</title>
		<link>http://malvinly.com/2011/06/16/asp-net-mvc-areas/</link>
		<comments>http://malvinly.com/2011/06/16/asp-net-mvc-areas/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 00:47:55 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=98</guid>
		<description><![CDATA[One of the new additions in ASP.NET MVC 2 was areas, which allows me to organize my projects into smaller sections. The ASP.NET MVC project that I&#8217;ve been working on has grown to a point where splitting it into smaller sections made sense. After adding areas, I noticed a few quirks. Here I&#8217;ve created a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=98&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the new additions in ASP.NET MVC 2 was areas, which allows me to organize my projects into smaller sections.  The ASP.NET MVC project that I&#8217;ve been working on has grown to a point where splitting it into smaller sections made sense.  After adding areas, I noticed a few quirks.</p>
<p><img src="http://nivlam.files.wordpress.com/2011/06/newarea.png?w=600" style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" /></p>
<p>Here I&#8217;ve created a new area called &#8220;About&#8221; with a <code>HomeController</code>. I&#8217;ve also added a <code>HomeController</code> to my root controllers folder.  At this point, I&#8217;d expect to be able to visit &#8220;/Home&#8221; and &#8220;/About/Home&#8221;, each displaying their own pages.  However, I received an error whenever I try to visit &#8220;/Home&#8221;.</p>
<p><pre class="brush: plain;">
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
MvcDemo.Controllers.HomeController
MvcDemo.Areas.About.Controllers.HomeController
</pre></p>
<p>Turns out that I can&#8217;t have duplicate controller names across my areas. To remedy this issue, I needed to make an adjustment to the default route found in <code>global.asax.cs</code>.</p>
<p><pre class="brush: csharp;">
routes.MapRoute(
    &quot;Default&quot;,
    &quot;{controller}/{action}/{id}&quot;,
    new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional },
    new string[] { &quot;MvcDemo.Controllers&quot; }  
);
</pre></p>
<p>Adding a fourth argument to the <code>MapRoute</code> method informs the handler that it should look in the specified namespace first. If it can&#8217;t find an appropriate controller in that namespace, fall back to the default behavior.  After adding that, I was able to successfully visit &#8220;/Home&#8221; and &#8220;/About/Home&#8221; and hit the correct controllers.</p>
<p><img src="http://nivlam.files.wordpress.com/2011/06/newarea2.png?w=600" style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" /></p>
<p>Here I&#8217;ve created an additional controller in the About area named <code>ContactMeController</code>.  I was able to successfully visit &#8220;/About/ContactMe&#8221; and have it display the correct page.  When I visit &#8220;/ContactMe&#8221;, I expected to receive a HTTP 404 back.  However, I received a different error.</p>
<p><pre class="brush: plain;">
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/ContactMe/Index.aspx
~/Views/ContactMe/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/ContactMe/Index.cshtml
~/Views/ContactMe/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
</pre></p>
<p>This indicates that the handler found a controller, but the <code>ViewResult</code> could not find a view. This is the same scenario we encountered above with the two <code>HomeControllers</code> since the handler by default does not respect area namespaces.  The namespace I added earlier was a namespace that has priority, but does not constrain it to that namespace. At this point, I want the controllers in the root controllers folder to be restricted to their namespace.  </p>
<p><pre class="brush: csharp;">
Route route = routes.MapRoute(
    &quot;Default&quot;,
    &quot;{controller}/{action}/{id}&quot;,
    new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional },
    new string[] { &quot;MvcDemo.Controllers.*&quot; }  
);

route.DataTokens[&quot;UseNamespaceFallback&quot;] = false;
</pre></p>
<p>I&#8217;ve updated the namespace to include everything in that namespace and below. I&#8217;ve added an extra <code>DataToken</code> &#8220;UseNamespaceFallback&#8221; and set it to <code>false</code>.  This extra <code>DataToken</code> indicates that the handler should not fall back to other namespaces in the project if it cannot find a controller in the specified namespace.  Now I can visit &#8220;/ContactMe&#8221; and receive back the expected HTTP 404.  At the same time, I can visit &#8220;/About/ContactMe&#8221; and have it return the correct page.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/98/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=98&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/06/16/asp-net-mvc-areas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>

		<media:content url="http://nivlam.files.wordpress.com/2011/06/newarea.png" medium="image" />

		<media:content url="http://nivlam.files.wordpress.com/2011/06/newarea2.png" medium="image" />
	</item>
		<item>
		<title>WCF Data Services and maxReceivedMessageSize</title>
		<link>http://malvinly.com/2011/05/09/wcf-data-services-and-maxreceivedmessagesize/</link>
		<comments>http://malvinly.com/2011/05/09/wcf-data-services-and-maxreceivedmessagesize/#comments</comments>
		<pubDate>Mon, 09 May 2011 15:49:14 +0000</pubDate>
		<dc:creator>Malvin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://malvinly.com/?p=89</guid>
		<description><![CDATA[We have a WCF Data Service (Astoria) that uses most of the default configuration settings. Every once in a while, a client would receive the following exception: This exception only occurs when the client sends a large message. The default message size in WCF is 65536. Since we&#8217;re using a WCF Data Service and not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=89&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We have a WCF Data Service (Astoria) that uses most of the default configuration settings.  Every once in a while, a client would receive the following exception:</p>
<p><pre class="brush: plain;">
System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---&gt; System.Data.Services.Client.DataServiceClientException: BadRequest
</pre></p>
<p>This exception only occurs when the client sends a large message. The default message size in WCF is 65536.  Since we&#8217;re using a WCF Data Service and not a normal WCF Service, adding a WCF Data Service (.svc) to the project does not auto-generate the service/binding information in the config.  To increase the message size, I needed to manually enter the service/binding information:</p>
<p><pre class="brush: xml;">
&lt;system.serviceModel&gt;
  &lt;services&gt;
    &lt;service name=&quot;MyNamespace.MyService&quot;&gt;
      &lt;endpoint bindingConfiguration=&quot;msgSize&quot; address=&quot;&quot; binding=&quot;webHttpBinding&quot; contract=&quot;System.Data.Services.IRequestHandler&quot; /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  &lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;!-- 2097152 = 2 MB--&gt;
      &lt;binding name=&quot;msgSize&quot; maxReceivedMessageSize=&quot;2097152&quot; maxBufferSize=&quot;2097152&quot; /&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
&lt;/system.serviceModel&gt;
</pre></p>
<p>The service name is the class (.svc) that inherits from <code>System.Data.Services.DataService&lt;T&gt;</code>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nivlam.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nivlam.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nivlam.wordpress.com/89/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=malvinly.com&amp;blog=20601338&amp;post=89&amp;subd=nivlam&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://malvinly.com/2011/05/09/wcf-data-services-and-maxreceivedmessagesize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d3dde70979847001714452779e70fc4?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">nivlam</media:title>
		</media:content>
	</item>
	</channel>
</rss>
