<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>Rambling Comments</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/" />
    
    <id>tag:www.lenholgate.com,2010-12-10:/blog//12</id>
    <updated>2010-12-27T19:38:24Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 5.12</generator>

<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/LenHolgate/Net" /><feedburner:info uri="lenholgate/net" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>Hosting .Net takes me back to the 'Good ol' days of COM'</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/SaStcaYeVF8/hosting-net-takes-me-back-to-the-good-ol-days-of-com.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.768</id>

    <published>2007-07-31T12:15:57Z</published>
    <updated>2010-12-27T19:38:24Z</updated>

    <summary>It's interesting, no, really, how the more things change the more they stay the same... I've been doing COM for a long time. I first discovered COM back at Interlink when I was writing a windows version of a product...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        It's interesting, no, really, how the more things change the more they stay the same... I've been doing COM for a long time. I first discovered COM back at Interlink when I was writing a windows version of a product call CardMaster which did credit card production. The system supported lots of different machines and each of these machines needed different drivers, the drivers were written in C and usually spoke to the machine via a serial port. In the DOS (actually Concurrent DOS, but...) version of the product I'd used structures of function pointers that the driver populated during an 'open()' call and which were returned to the application so that the application could use this 'opaque' structure to access these machines polymorphically... Think v-tables without the rest of C++...
        &lt;p&gt;Moving on to the windows version I knew that I wanted to package each driver in a dll so that we could ship new drivers to users without needing to send them a complete new build of the product... I'd worked through the whole &lt;code&gt;LoadLibrary()&lt;/code&gt;, &lt;code&gt;GetProcAddress()&lt;/code&gt; malarky and had a rather nice design that provided multiple interfaces to drivers but had two small holes in it; it was hard to manage the lifetime of the dlls so that they stayed loaded for the right amount of time and I had to come up with a couple of interfaces that would represent all drivers forever as it would be a pain to have to change all the drivers if I changed the interfaces.&lt;/p&gt;

&lt;p&gt;&lt;iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;amp;o=2&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=1572313498&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;Just at this point in the development I went to a Microsoft conference. The hot new thing was COM, it seemed very similar to what I'd been designing and solved the dll unload problem with reference counts on the interfaces and the interface evolution problem with dynamic discovery using &lt;code&gt;QueryInterface()&lt;/code&gt;. I bought Inside COM, ripped out my custom dll loading system and embraced COM; complete with tear off interfaces and my own COM library that provided the boilerplate implementations for factories and IUnknown, etc. COM was easy then, most, or maybe all of it was in process and marshalling was easy... So easy that often the rules of COM were broken and we developed in process interfaces that could never be marshalled out of process due to data types or design features used...&lt;/p&gt;

&lt;p&gt;Time passed. I worked at CSFP which became CSFB and we did a lot of COM. Things were getting more complex, marshalling mattered, most of the time, dodgy designs started to fall away and things were good; well, most of the time, developing &lt;a href="http://www.lenholgate.com/archives/000547.html"&gt;OLE-DB providers&lt;/a&gt; was definitely not a 'good' period ;). COM contiunued to get gnarly and hairy and more complex and eventually had a + added on the end...&lt;/p&gt;

I haven't done much COM recently, but &lt;a href="http://www.lenholgate.com/archives/000683.html"&gt;hosting the CLR&lt;/a&gt; in &lt;a href="http://www.serverframework.com/"&gt;The&amp;nbsp;Server&amp;nbsp;Framework&lt;/a&gt; requires COM to communicate with it... What I'm finding is that working with the CLR COM interfaces and exposing C++ code to the CLR via COM makes me feel like I'm working back in the, ahem, good ol' days of COM, that is right back at the start before it got all hairy... I'm not quite sure why...
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/SaStcaYeVF8" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/07/hosting-net-takes-me-back-to-the-good-ol-days-of-com.html</feedburner:origLink></entry>

<entry>
    <title>Sometimes it almost seems that they don't want you to get the code to work...</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/Lt4CEvcmaGA/sometimes-it-almost-seems-that-they-dont-want-you-to-get-the-code-to-work.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.765</id>

    <published>2007-07-24T21:32:15Z</published>
    <updated>2010-12-27T19:37:19Z</updated>

    <summary>I've spent some time over the last few days playing around with my CLR hosting socket server example. I had stalled on a piece of client work, I've got a bit of a head cold at the moment and my...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        I've spent some time over the last few days playing around with my &lt;a href="http://www.serverframework.com/products---the-clr-hosting-option.html"&gt;CLR hosting&lt;/a&gt; socket server example. I had stalled on a piece of client work, I've got a bit of a head cold at the moment and my brain just wasn't in the right place for what I was supposed to be doing so I picked up one of more interesting the items on my todo list and took a look at that instead... The bulk of the work flowed nicely but then, just at the end things stopped working for no apparent reason... I'd done what was suggested in the book, the code compiled, the CLR called into my host in the right way and then I got a security policy exception... Hmmm.
        &lt;p&gt;I'm looking at having my CLR hosting server control how assemblies are loaded by the CLR. This involves implementing a few COM interfaces (who said .Net killed COM?), mainly &lt;code&gt;IHostAssemblyManager&lt;/code&gt; and &lt;code&gt;IHostAssemblyStore&lt;/code&gt;. These allow you to jump through some hoops and eventually control how assemblies are loaded... I'd got to the bit where the CLR was finally calling &lt;code&gt;IHostAssemblyStore.ProvideAssembly()&lt;/code&gt; on my host and I was checking the binding info and returning an &lt;code&gt;IStream&lt;/code&gt; that contained the contents of the assembly file. This is how SQL Server hosts code that lives in the database and it works quite nicely (once you're trained to jump through the required hoops without asking too many questions). &lt;/p&gt;

&lt;p&gt;&lt;iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;amp;o=2&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0735619883&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;
So, I was returning a valid &lt;code&gt;IStream&lt;/code&gt; which contained the correct assembly but my code still didn't run and I was getting security policy exceptions. I double checked my reference book, "&lt;a href="http://www.amazon.co.uk/gp/product/0735619883?ie=UTF8&amp;amp;tag=ramcom-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=0735619883"&gt;Customizing the Microsoft.NET Framework Common Language Runtime&lt;/a&gt;&lt;img src="http://www.assoc-amazon.co.uk/e/ir?t=ramcom-21&amp;amp;l=as2&amp;amp;o=2&amp;amp;a=0735619883" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;" by &lt;a href="http://www.google.com/search?sourceid=navclient&amp;amp;ie=UTF-8&amp;amp;rls=GGLC,GGLC:1970-01,GGLC:en&amp;amp;q=Steven+Pratschner"&gt;Steven Pratschner&lt;/a&gt;, and I seemed to be doing everything that I needed to be doing... I did a Google search and someone else had been having a &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=122334&amp;amp;SiteID=1"&gt;similar problem&lt;/a&gt; and his final comment was that the book did mention the issue that he had but he hadn't noticed it... I checked things out and finally found the problem mentioned in a different chapter, some way further on from the actual code that dealt with assembly store customisation... It seems that if you provide your own assembly store then you also have to provide a managed HostSecurityManager which adds some "evidence" onto the assembly that you've loaded so that the CAS stuff works properly... The chapter where this is explained goes on to show how to create custom evidence and all kinds of clever things that I have no interest in and then, right at the end, subtly includes a comment about how the security manager implementation needs to add more evidence (in addition to all the custom stuff that the rest of the chapter had been dealing with). If this additional evidence isn't added then the new custom evidence wont work... The good thing is, for me at least, just adding the standard evidence works well enough for now. The loaded assemblies will have full trust but that's fine for my example.&lt;/p&gt;

&lt;p&gt;So, the minimal managed security manager implementation that you need to get your custom assembly store working is something like this:&lt;/p&gt;
&lt;pre class="brush: cpp gutter: false"&gt;   public class SecurityManager : HostSecurityManager
   {
      public override HostSecurityManagerOptions Flags
      {
         get
         {
            return (HostSecurityManagerOptions.HostAssemblyEvidence | 
               HostSecurityManagerOptions.HostPolicyLevel);
         }
      }

      public override Evidence ProvideAssemblyEvidence(
         Assembly loadedAssembly, 
         Evidence inputEvidence)
      {
         if (loadedAssembly.HostContext == 42)
         {
            inputEvidence.AddHost(new Zone(SecurityZone.MyComputer));
         }&lt;/p&gt;

         return inputEvidence;
      }
   }
&lt;/pre&gt;
And now that I've worked around the fact that you can't reuse the &lt;code&gt;IStream&lt;/code&gt; that you give out in one call to &lt;code&gt;IHostAssemblyStore.ProvideAssembly()&lt;/code&gt; in another call (probably due to the cursor in the stream being wrong), everything seems to be working nicely.
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/Lt4CEvcmaGA" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/07/sometimes-it-almost-seems-that-they-dont-want-you-to-get-the-code-to-work.html</feedburner:origLink></entry>

<entry>
    <title>Echoes from the CLR</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/Cmds_caCf10/echoes-from-the-clr.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.737</id>

    <published>2007-03-20T12:51:41Z</published>
    <updated>2010-12-27T13:56:36Z</updated>

    <summary>The work on the CLR hosting socket server example is going pretty well. I now have a server that can pass server notifications to managed code that either runs in an AppDomain per connection or within a single AppDomain (depending...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        The work on the &lt;a href="http://www.lenholgate.com/archives/000675.html"&gt;CLR hosting&lt;/a&gt; socket server &lt;a href="http://www.lenholgate.com/archives/000683.html"&gt;example&lt;/a&gt; is going pretty well. I now have a server that can pass server notifications to managed code that either runs in an AppDomain per connection or within a single AppDomain (depending on how isolated you want the user code to be). I think I'm pretty much there as far as what I want to demonstrate is concerned; it works and the line between managed and unmanaged code is likely to vary depending on a client's particular requirements so there's little point in extending the example code any further. Hopefully I'll get a chance to clean things up a little more and then do some performance comparisons between a pure unmanaged server, a pure managed server and a hybrid...
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/Cmds_caCf10" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/03/echoes-from-the-clr.html</feedburner:origLink></entry>

<entry>
    <title>Socket Server that hosts the CLR</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/0wLZtcaTeCY/socket-server-that-hosts-the-clr.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.736</id>

    <published>2007-03-15T17:49:10Z</published>
    <updated>2010-12-27T13:56:14Z</updated>

    <summary><![CDATA[My investigations into CLR hosting are going well and today I built an echo sever based on The&nbsp;Server&nbsp;Framework and my CLR Hosting helper library. The idea is that the server can deal with the network IO and then hand off...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        My &lt;a href="http://www.lenholgate.com/archives/000675.html"&gt;investigations&lt;/a&gt; into &lt;a href="http://www.lenholgate.com/archives/000674.html"&gt;CLR hosting&lt;/a&gt; are going well and today I built an echo sever based on &lt;a href="http://www.serverframework.com/"&gt;The&amp;nbsp;Server&amp;nbsp;Framework&lt;/a&gt; and my &lt;a href="http://www.serverframework.com/products---the-clr-hosting-option.html"&gt;CLR Hosting helper library&lt;/a&gt;. The idea is that the server can deal with the network IO and then hand off the 'real work' to some .Net code. So far the integration is going pretty smoothly....
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/0wLZtcaTeCY" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/03/socket-server-that-hosts-the-clr.html</feedburner:origLink></entry>

<entry>
    <title>Yay .Net sockets stuff...</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/jShABhboWOc/yay-net-sockets-stuff.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.732</id>

    <published>2007-03-05T04:35:35Z</published>
    <updated>2010-12-27T12:46:17Z</updated>

    <summary>Nice to see that the new beta, sorry, CTP, of the next .Net Framework will increase the performance of .Net sockets by 70%! Way to go .Net dudes.... Interesting to see that it's a joint effort between the System.Net people...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;Nice to see that the new beta, sorry, CTP, of the next .Net Framework will &lt;a href="http://blogs.msdn.com/wndp/archive/2007/02/28/preview-new-networking-features-in-the-net-framework-quot-orcas-quot.aspx"&gt;increase the performance of .Net sockets by 70%!&lt;/a&gt; Way to go .Net dudes....&lt;/p&gt;

&lt;p&gt;Interesting to see that it's a joint effort between the System.Net people (who, I assume own the sockets code) and the CLR people (who own the platform)... I wonder what the CLR guys brought to the tuning table...&lt;/p&gt;

It would be nice to know how the new (and existing) .Net sockets performance compares with the Win32 equivalent...
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/jShABhboWOc" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/03/yay-net-sockets-stuff.html</feedburner:origLink></entry>

<entry>
    <title>Hmm, is this really a good fix?</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/lY8I9Zk6xYA/hmm-is-this-really-a-good-fix.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.731</id>

    <published>2007-03-05T04:30:38Z</published>
    <updated>2010-12-27T12:45:49Z</updated>

    <summary>Back in July Joe Duffy wrote an interesting piece on the CLR thread pool. I commented on it then, here. He's now written another piece about why they increased the maximum number of threads in "the thread pool" from 25/cpu...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        Back in July Joe Duffy wrote an interesting piece on the CLR thread pool. I commented on it then, &lt;a href="http://www.lenholgate.com/archives/000656.html"&gt;here&lt;/a&gt;. He's now written another piece about why they increased the maximum number of threads in "the thread pool" from 25/cpu to 250/cpu. Joe, I refer you to my previous comment... You're solving the wrong problem!
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/lY8I9Zk6xYA" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/03/hmm-is-this-really-a-good-fix.html</feedburner:origLink></entry>

<entry>
    <title>Dino Viehland on CLR hosting</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/75IftyW5_n4/dino-viehland-on-clr-hosting.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.729</id>

    <published>2007-02-13T12:13:08Z</published>
    <updated>2010-12-27T12:44:56Z</updated>

    <summary>This is mainly a reminder for me so that I can read this when I get back from Jackson Hole... Dino Viehland has written some interesting looking blog posts on how to implement the thread/task and syncrhonisation host managers for...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;This is mainly a reminder for me so that I can read this when I get back from Jackson Hole...&lt;/p&gt;

&lt;a href="http://blogs.msdn.com/dinoviehland/default.aspx"&gt;Dino Viehland&lt;/a&gt; has written some &lt;a href="http://blogs.msdn.com/dinoviehland/archive/2004/08/16/215140.aspx"&gt;interesting looking blog posts&lt;/a&gt; on how to implement the thread/task and syncrhonisation host managers for a hosted CLR. Note that this is all "Whidbey beta 1" stuff so, well, your milage may vary...
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/75IftyW5_n4" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/02/dino-viehland-on-clr-hosting.html</feedburner:origLink></entry>

<entry>
    <title>Lifetime management issues with CLR hosting</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/E0sCnF7B2QU/lifetime-management-issues-with-clr-hosting.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.728</id>

    <published>2007-02-12T16:41:03Z</published>
    <updated>2010-12-27T12:44:18Z</updated>

    <summary>I'm still playing with hosting the CLR in C++ (using "Customizing the Microsoft.NET Framework Common Language Runtime" by Steven Pratschner as my guide)... It's an interesting journey but, once again, I wonder who makes the important technical decisions at Microsoft...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Rants" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        I'm still playing with hosting the CLR in C++ (using "&lt;a href="http://www.amazon.co.uk/gp/product/0735619883?ie=UTF8&amp;amp;tag=ramcom-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=0735619883"&gt;Customizing the Microsoft.NET Framework Common Language Runtime&lt;/a&gt;&lt;img src="http://www.assoc-amazon.co.uk/e/ir?t=ramcom-21&amp;amp;l=as2&amp;amp;o=2&amp;amp;a=0735619883" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;" by &lt;a href="http://www.google.com/search?sourceid=navclient&amp;amp;ie=UTF-8&amp;amp;rls=GGLC,GGLC:1970-01,GGLC:en&amp;amp;q=Steven+Pratschner"&gt;Steven Pratschner&lt;/a&gt; as my guide)... It's an interesting journey but, once again, I wonder who makes the important technical decisions at Microsoft and how they sleep at night ;) .
        &lt;p&gt;&lt;iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;amp;o=2&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0735619883&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;I'm currently playing with the &lt;code&gt;ICLRRuntimeHost&lt;/code&gt; interface. This lets you load the CLR into your unmanaged process and gives you a remarkable amount of control over how the CLR that you're hosting operates. Unfortunately it's got some dubious design issues... Firstly you can only have one instance of the CLR hosted in your process; that's just about fair enough, I guess, but I'd still be curious to know why... Secondly, if you try and load a second or subsequent instance you'll get &lt;code&gt;S_FALSE&lt;/code&gt; returned from your call to &lt;code&gt;CorBindToRuntimeEx()&lt;/code&gt;... The error isn't currently documented in the online docs for the function... Interestingly, you get the same error whether you've requested the same version of the CLR with the same configuration settings or if you've requested a completely different version, so, it doesn't mean "you've already loaded the CLR like this" it means "you've already loaded a version of the CLR somehow". Again this isn't likely to be much of a problem as you can't &lt;b&gt;ever&lt;/b&gt; unload the CLR so the first one that you load is the only one you can ever load... &lt;/p&gt;

&lt;p&gt;Then there's the &lt;code&gt;Stop()&lt;/code&gt; method on &lt;code&gt;ICLRRuntimeHost&lt;/code&gt;... The docs for this are "interesting"; online we have "pre-release" docs which say: &lt;i&gt;"This method does not release resources to the host, unload application domains, or destroy threads. You must terminate the process to release these resources."&lt;/i&gt;, whilst VS 2005 SP1's help says &lt;i&gt;"Do not call this method. The host cannot unload the runtime from a process."&lt;/i&gt; which is amusing at best. What's more, if you DO call it then it seems to return &lt;code&gt;E_UNEXPECTED&lt;/code&gt; if you call it before starting the CLR (which is probably what you'd expect) and it seems to work like a 'reference counted' "Stop" (you can call it as many times as you've called &lt;code&gt;Start()&lt;/code&gt; and all except the last call returns &lt;code&gt;S_FALSE&lt;/code&gt; and the last call returns &lt;code&gt;S_OK&lt;/code&gt;), which, again, is pretty much what you might expect... Unfortunately, once it's returned &lt;code&gt;S_OK&lt;/code&gt; the CLR in your process is screwed, not only is it stopped (probably not that cleanly if the docs are anything to go by) but you can't ever start another CLR in your process; though calling &lt;code&gt;CorBindToRuntimeEx()&lt;/code&gt; still returns &lt;code&gt;S_FALSE&lt;/code&gt;...&lt;/p&gt;

&lt;p&gt;Of course none of this is really a "problem" if you use the code in the way that some of the docs that you might come across suggest that you should do but I do find myself wondering "Why was v2.0 of the .Net Common Language Runtime allowed to go out the door with this half arsed implementation of a new interface?" After all, if you use the 'backwards compatibility' excuse then, well, even if you were relying on using the functionality during the beta phase, you shouldn't be calling &lt;code&gt;Stop()&lt;/code&gt; anymore so surely it's better to break any code that was using it rather than leaving it "working"? Or, if you really couldn't change the interface at that late a date then surely a method that's documented as "don't call this" should be a no-op inside and just return &lt;code&gt;S_OK&lt;/code&gt; or &lt;code&gt;S_FALSE&lt;/code&gt; the whole time and do nothing?&lt;/p&gt;

&lt;p&gt;I'm also a bit concerned about the fact that you can't shut the CLR down cleanly once it's started... I'm a bit of a &lt;a href="http://www.lenholgate.com/archives/000085.html"&gt;fan&lt;/a&gt; of being able to start &lt;b&gt;and&lt;/b&gt; stop my code, no matter how complex it is. The fact that it looks like the original design allowed for stopping the CLR and the actual implementation can't do that is a little worrying...&lt;/p&gt;

&lt;p&gt;Of course this annoys me most simply because it makes testing any code that happens to host the CLR a much harder job. As is often the way, the implementation flaw in Microsoft's code leaks out and becomes a flaw in all the code that uses it... I can't test my code to load and host the CLR with various different, valid and invalid, options without starting a different process for each test... Bleugh... &lt;/p&gt;

I think I need a new category of blog postings, "&lt;a href="http://www.lenholgate.com/archives/cat_rants.html"&gt;Rants&lt;/a&gt;", though perhaps most of my postings could be categorised that way...
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/E0sCnF7B2QU" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/02/lifetime-management-issues-with-clr-hosting.html</feedburner:origLink></entry>

<entry>
    <title>Crapness in mscoree.h</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/pJUjlnlLToc/crapness-in-mscoreeh.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.727</id>

    <published>2007-02-08T15:49:33Z</published>
    <updated>2010-12-27T12:43:32Z</updated>

    <summary><![CDATA[I'm playing around with hosting the CLR in C++ at present and have come across a bit of crapness in the mscoree.h file... #if (_MSC_VER &lt; 1300 || _WIN32_WINNT &lt; 0x0500) typedef VOID ( __stdcall *WAITORTIMERCALLBACK )( PVOID __MIDL_0011, BOOL...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;I'm playing around with hosting the CLR in C++ at present and have come across a bit of crapness in the &lt;code&gt;mscoree.h&lt;/code&gt; file...&lt;/p&gt;
&lt;pre class="brush: cpp gutter: false"&gt;#if (_MSC_VER &amp;lt; 1300 || _WIN32_WINNT &amp;lt; 0x0500)
typedef VOID ( __stdcall *WAITORTIMERCALLBACK )(
    PVOID __MIDL_0011,
    BOOL __MIDL_0012);

#endif // (_MSC_VER &amp;lt; 1300 || _WIN32_WINNT &amp;lt; 0x0500)
&lt;/pre&gt;
        &lt;p&gt;This effectively says, if you're using VC6 or you're compiling for less than v5 of Windows NT then you don't have this typedef so here it is... Unfortunately, if you're compiling with VC6 but you're using the platform SDK then you already have that typedef in &lt;code&gt;winbase.h&lt;/code&gt; and, well, &lt;code&gt;mscoree.h&lt;/code&gt; wont compile...&lt;/p&gt;

&lt;p&gt;It's a pity that versions of the Platform SDK aren't identifiable by a &lt;code&gt;#define&lt;/code&gt; as this would make it easy to solve this kind of problem (and lots of others!) in a neat and maintainable way, but...&lt;/p&gt;

So far the best I've come up with is to wrap the inclusion of the real &lt;core&gt;mscoree.h&lt;/code&gt; in my own include and check for use of VC6 and lie about the compilers age to &lt;core&gt;mscoree.h&lt;/code&gt;... Anyone got any better suggestions?
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/pJUjlnlLToc" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2007/02/crapness-in-mscoreeh.html</feedburner:origLink></entry>

<entry>
    <title>CLR thread pool woes</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/NCop4EYmnOk/clr-thread-pool-woes.html" />
    <id>tag:www.socketframework.com,2006:/blog//12.709</id>

    <published>2006-07-12T07:47:38Z</published>
    <updated>2010-12-27T08:20:56Z</updated>

    <summary><![CDATA[Joe Duffy has written an interesting piece over on "Generalities &amp; Details: Adventures in the High-tech Underbelly" about problems with the CLR thread pool. Joe's a program manager on the CLR team at Microsoft, so he knows what he's talking...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;Joe Duffy has &lt;a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,fe3434c7-b3b7-42c8-9267-df996c9c222a.aspx"&gt;written an interesting piece&lt;/a&gt; over on &lt;a href="http://www.bluebytesoftware.com/blog/default.aspx"&gt;"Generalities &amp;amp; Details: Adventures in the High-tech Underbelly"&lt;/a&gt; about problems with the &lt;a href="http://msdn.microsoft.com/msdnmag/issues/03/06/NET/"&gt;CLR thread pool&lt;/a&gt;. Joe's a program manager on the CLR team at Microsoft, so he knows what he's talking about!&lt;/p&gt;

I find the issues that Joe raises interesting as I spent some time designing a flexible thread pool for my &lt;a href="http://www.lenholgate.com/archives/000637.html"&gt;C++ IOCP servers&lt;/a&gt; some time go and came across the problems that he's facing. In essence the problem is that you want to allow the thread pool to grow and shrink based on demand but you don't want it to grow too fast as threads are expensive things to create and creating too many hurts performance, yet you don't want it to grow too slowly as that hurts throughput of work items; especially if the work items are performing blocking operations.
        &lt;p&gt;Please bear in mind that I haven't used the CLR thread pool a great deal because most of my clients still require that I write in C++, so I may have things wrong, be gentle with me...&lt;/p&gt;

&lt;p&gt;It sounds like Joe's CLR thread pool design is similar to my first cut of a design. There are a minimum number of threads that the pool will hold and a throttling time that restricts creating new threads for a period after a thread has been created. The idea being that as work comes in you allocate each work item to a thread until all threads are busy and then you may decide to start a new thread, or you may decide to wait a while (because you just started a new thread). If threads are idle for too long then they are removed from the pool and the pool shrinks. If the work items arrive at a "reasonable" and consistent speed and the work items take a "reasonable" amount of time then you're fine. The pool will expand and contract as required and work will be done in a "reasonable" time. The problem is what happens when the work items occur in batches with gaps between and some or all of the work items take a long period of time on blocking operations (like database access, for example). &lt;/p&gt;

&lt;p&gt;My final design was a little more complex than Joe's design (possibly too complex!). I have an initial count of threads, which is the number that the thread pool creates when it starts. I have a minimum number of threads, which is the number below which the thread pool will not shrink. I have a maximum number of threads, which is the number above which the thread pool will not grow. There are also a maximum number of 'dormant' threads, which is the number of threads that aren't currently processing work items; once this limit is exceeded we start shutting some threads down. Then there are the dispatch timeout values. I have two; one for when the thread pool is not currently running at maximum threads, this is the timeout that determines how long to wait between starting new threads. The second is a timeout that is used when the pool is running at maximum threads, this timeout simply slows the dispatch monitoring loop so that we don't busy wait when we aren't allowed to increase the capacity of the pool. The final configuration parameter to the thread pool is the pool maintenance period; this determines how often the pool considers shrinking itself. There's a detailed, if old, design rationale in this CodeProject article &lt;a href="http://www.codeproject.com/internet/JBSocketServer2.asp"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In itself my design simply moves the problem around a little, however there are two advantages that I have when using my thread pool in a process: Firstly, I can tune the thread pool to suit the application; all of the configuration parameters are passed in to the constructor, they can be sourced from an external config system if required and thus can be adjusted on a per process level as you profile the process that's using them. Secondly, I can have more than one thread pool in a process; there is no need for a single process wide thread pool that has to be appropriate for all of the async work within a process. &lt;/p&gt;

&lt;p&gt;I can understand how the concept of &lt;a href="http://msdn.microsoft.com/msdnmag/issues/03/06/NET/"&gt;"THE CLR Thread pool"&lt;/a&gt; might have come about. It's convenient for programmers using various APIs if they don't have to worry about providing a thread pool and that the API just goes off an "grabs" THE thread pool. Unfortunately, &lt;a href="http://www.lenholgate.com/archives/000357.html"&gt;singletons are evil&lt;/a&gt; ;) and tempting as it may be to make it "easier" for people by hiding things and allowing access to global variables (the single thread pool object!) it's rarely appropriate. The problem is that for this design to work, the CLR team has to anticipate all possible usage patterns and optimise for all of them... Obviously needing to have multiple thread pools is unusual, but it may happen, it may be that there are some work items that are slow and should not take up all of your single thread pool's resources and some that are fast and must be processed reasonably quickly... If you, the programmer, can decide to use two, or more, appropriately tuned thread pools then tuning one pool to be just right for a mixed set of work item requirements is no longer an issue... After all, no matter how large a number of threads, N, you allow in your single pool you only need N+1 of your slow work items to prevent your faster work items from being processed at a "reasonable" rate. With two, or more, pools this is never an issue...&lt;/p&gt;

So, my advice to Joe is this: Firstly, allow the user access to all of the tuning parameters that the pool supports, not just the number of threads, but also any timeouts, etc. Default to "sensible" values, write MSDN articles on why you shouldn't use "foolish" values and then trust the user to do the right thing. Second, for shipping products that use the thread pool allow the user to configure the thread pool via an external source, such as a config file. Thirdly, consider adjusting all of the APIs that currently use THE thread pool to also be callable with A thread pool and then allow the user to create and configure additional pools within their process.
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/NCop4EYmnOk" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2006/07/clr-thread-pool-woes.html</feedburner:origLink></entry>

<entry>
    <title>Now I'm confused (C++/CLI destructors)</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/JOn5NSMYCxg/now-im-confused-ccli-destructors.html" />
    <id>tag:www.socketframework.com,2006:/blog//12.665</id>

    <published>2006-01-08T13:36:48Z</published>
    <updated>2010-12-26T12:23:30Z</updated>

    <summary>So here I am, writing a piece about how the C++/CLI destructor and finalizer stuff could have been a bit neater and I put together some sample code to demonstrate my point and it doesn't do what the docs I...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        So here I am, writing a piece about how the C++/CLI destructor and finalizer stuff could have been a bit &lt;a href="http://www.lenholgate.com/archives/000611.html"&gt;neater &lt;/a&gt;and I put together some sample code to demonstrate my point and it doesn't do what the docs I mentioned yesterday suggest that it should do...
        &lt;p&gt;Given this example class....&lt;/p&gt;
&lt;pre class="brush: cpp gutter: false"&gt;ref class Example
{
   public :
   
      Example(
         bool throws)
         : m_count(s_count++),
            m_string(gcnew String("MyString"))
      {
         Console::WriteLine(m_count + " - Example(throws = " + throws + ")");

         if (throws)
         {
            throw "Thrown";
         }
      }
   
      Example()
         : m_count(s_count++),
            m_string(gcnew String("MyString"))
      {
         Console::WriteLine(m_count + " - Example");
      }
   
      ~Example()
      {
         Console::WriteLine(m_count + " - ~Example");

         delete m_string;
	
         this-&amp;gt;!Example();            
      }

      !Example()
      {
         Console::WriteLine(m_count + " - !Example");
      }

   private :

      const int m_count;

      String ^m_string;

      static int s_count = 0;
};
&lt;/pre&gt;
&lt;p&gt;And the following test code...&lt;/p&gt;
&lt;pre class="brush: cpp gutter: false"&gt;int main(array&amp;lt;System::String&amp;gt; ^args)
{
   try
   {
      Example example(true);
   }
   catch(...)
   {
      Console::WriteLine("Caught");
   }
   
   try
   {
      Example ^example = gcnew Example(true);
   }
   catch(...)
   {
      Console::WriteLine("Caught");
   }
    
   return 0;
}
&lt;/pre&gt;
&lt;p&gt;I was expecting, based on &lt;a href="http://msdn2.microsoft.com/en-us/library/ms177197.aspx"&gt;these docs from MSDN&lt;/a&gt;, that the output would be something like this:&lt;/p&gt;
&lt;pre class="brush: text gutter: false"&gt;0 - Example
0 - ~Example
0 - !Example
Caught
1 - Example
Caught
1 - !Example
&lt;/pre&gt;
&lt;p&gt;But, instead, I actually get this:&lt;/p&gt;
&lt;pre class="brush: text gutter: false"&gt;0 - Example
Caught
1 - Example
Caught
1 - !Example
0 - !Example
&lt;/pre&gt;
&lt;p&gt;That is, the destructor for the stack based object isn't called (as the docs seem to imply it would be), but the finalizers are when the objects are garbage collected...&lt;/p&gt;

Am I being dense here?
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/JOn5NSMYCxg" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2006/01/now-im-confused-ccli-destructors.html</feedburner:origLink></entry>

<entry>
    <title>Why are the "event" classes in .Net STILL broken?</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/O21bwmnHZmA/why-are-the-event-classes-in-net-still-broken.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.632</id>

    <published>2005-11-15T18:01:45Z</published>
    <updated>2010-12-16T10:05:15Z</updated>

    <summary>Whilst I'm ranting about the little things... You still can't create named versions of the .Net ManualResetEvent and AutoResetEvent, even in .Net 2.0. Wasn't everything going to be fixed in Whidbey?...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Rants" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;Whilst I'm ranting about the little things...&lt;/p&gt;

&lt;p&gt;You &lt;a href="http://www.lenholgate.com/archives/000279.html"&gt;still&lt;/a&gt; can't create named versions of the .Net &lt;code&gt;ManualResetEvent&lt;/code&gt; and &lt;code&gt;AutoResetEvent&lt;/code&gt;, even in .Net 2.0. Wasn't &lt;i&gt;everything&lt;/i&gt; going to be fixed in Whidbey?&lt;/p&gt;
        &lt;p&gt;Of course, I realise that you can &lt;a href="http://groups.google.com/group/microsoft.public.dotnet.framework/browse_thread/thread/802347b7330e3116/17a9b3c08db2e396%2317a9b3c08db2e396?sa=X&amp;amp;oi=groupsr&amp;amp;start=0&amp;amp;num=2"&gt;"simply" access&lt;/a&gt; the underlying Win32 API to do it, but a) why should we have to? and b) it's not &lt;a href="http://groups.google.com.au/group/microsoft.public.dotnet.framework.clr/browse_frm/thread/570dcb950925b398/54f3f9ff615e8ddf?rnum=1#54f3f9ff615e8ddf"&gt;recommended&lt;/a&gt; that you do...&lt;/p&gt;

&lt;p&gt;I guess the fact that the P/Invoke route isn't recommended is the reason for the dearth of nicely packaged up solutions to this problem; surely anyone who needs this functionality would write an object that wraps it up nicely rather than doing it "long hand" like &lt;a href="http://www.dotnet247.com/247reference/msgs/54/270145.aspx"&gt;this&lt;/a&gt;? I particularly like the way that example uses the &lt;a href="http://www.lenholgate.com/archives/000577.html"&gt;hole in the encapsulation&lt;/a&gt; of &lt;code&gt;ManualResetEvent&lt;/code&gt; to replace the event handle so that he can use &lt;code&gt;WaitOne()&lt;/code&gt; on it...&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/O21bwmnHZmA" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2005/11/why-are-the-event-classes-in-net-still-broken.html</feedburner:origLink></entry>

<entry>
    <title>New C# v3 features explained in context</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/qfSJBd5SC8U/new-c-v3-features-explained-in-context.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.581</id>

    <published>2005-09-22T08:02:53Z</published>
    <updated>2010-12-24T06:38:36Z</updated>

    <summary>Ted Neward has a very nice piece about the new language features in C# v3 and how they work together to provide something quite powerful. Go read it! Given that implicit typing, object initialisers and extension methods are all designed...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;&lt;a href="http://blogs.tedneward.com/default.aspx"&gt;Ted Neward&lt;/a&gt; has a very nice &lt;a href="http://blogs.tedneward.com/2005/09/22/Language+Innovation+C+30+Explained.aspx"&gt;piece&lt;/a&gt; about the new language features in C# v3 and how they work together to provide something quite powerful. &lt;a href="http://blogs.tedneward.com/2005/09/22/Language+Innovation+C+30+Explained.aspx"&gt;Go read it&lt;/a&gt;!&lt;/p&gt;

Given that implicit typing, object initialisers and extension methods are all designed to allow LINQ to be a be able to generate classes on the fly and extend existing classes I'd still be happier if they could be &lt;a href="http://www.lenholgate.com/archives/000509.html"&gt;optionally restricted from use on 'normal' classes&lt;/a&gt; to help prevent the less experienced running amok with these new language features and creating code that has interesting maintenance properties...
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/qfSJBd5SC8U" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2005/09/new-c-v3-features-explained-in-context.html</feedburner:origLink></entry>

<entry>
    <title>Restricting the use of extension methods?</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/EpZ7Fq0QHPY/restricting-the-use-of-extension-methods.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.578</id>

    <published>2005-09-19T10:37:56Z</published>
    <updated>2010-12-24T06:35:31Z</updated>

    <summary>Having looked through the slides that Vagn pointed me to in a comment to my recent post about C# v3.0 Extension Methods I can understand, a little more, about the reasoning behind the change to the language. Given that the...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;Having looked through the &lt;a href="http://216.55.183.63/pdc2005/slides/TLN307_Hejlsberg.ppt"&gt;slides&lt;/a&gt; that Vagn pointed me to in a comment to my recent post about &lt;a href="http://www.lenholgate.com/archives/000504.html"&gt;C# v3.0 Extension Methods&lt;/a&gt; I can understand, a little more, about the reasoning behind the change to the language. Given that the C# v3.0 spec contains the following warning:&lt;/p&gt;

&lt;p&gt;&lt;i&gt;"Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible. Extension members of other kinds, such as properties, events, and operators, are being considered but are currently not supported."&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Perhaps it would be a good idea to make classes have to 'opt in' to enable extension methods on them. A keyword, such as &lt;code&gt;extensible&lt;/code&gt; or whatever could be added to a class to allow the use of extension methods on it, much in the same way that &lt;code&gt;sealed&lt;/code&gt; can be used to prevent extension via inheritance. This would reduce the likelyhood that the feature will be &lt;a href="http://dotnetdebug.blogspot.com/2005/09/ot-c-30-specifications-and-extension.html"&gt;missused&lt;/a&gt; by inexperienced programmers and would mean that classes could be 'closed for extension' by design. I would expect much of the .Net framework would naturally be 'closed', in the same way that much of it is 'sealed'. This wouldn't prevent the use of the feature where it was needed but might restrict the confusion that could be caused if it were overused.&lt;/p&gt;

Thoughts?
        
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/EpZ7Fq0QHPY" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2005/09/restricting-the-use-of-extension-methods.html</feedburner:origLink></entry>

<entry>
    <title>C# v3 Extension methods, syntactic sugar for the lack of free functions?</title>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LenHolgate/Net/~3/aU2v0AXVzqA/c-v3-extension-methods-syntactic-sugar-for-the-lack-of-free-functions.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.573</id>

    <published>2005-09-16T14:12:36Z</published>
    <updated>2010-12-24T06:28:32Z</updated>

    <summary>There's a lot of noise coming out of the Microsoft PDC right now. Something that interested me was the future direction of C#; you can grab the spec from here. It seems they're adding "extension methods" which, to me, appear...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term=".Net" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Dumbing down is dumb" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        &lt;p&gt;There's a lot of noise coming out of the &lt;a href="http://pdcbloggers.net/"&gt;Microsoft PDC&lt;/a&gt; right now. Something that interested me was the &lt;a href="http://msdn.microsoft.com/vcsharp/future/"&gt;future direction of C#&lt;/a&gt;; you can grab the spec from &lt;a href="http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%203.0%20specification.doc"&gt;here&lt;/a&gt;.&lt;/p&gt;

It seems they're adding "extension methods" which, to me, appear to be just &lt;a href="http://en.wikipedia.org/wiki/Syntactic_sugar"&gt;syntactic sugar&lt;/a&gt; to make up for the lack of free functions...
        &lt;p&gt;In C++ you can have functions that aren't part of an object. In C no functions are part of an object. These are "free" functions. You can't have these kind of things in C# and Java because everything has to be part of an object even if the only purpose for the object's existence is to provide a home for a collection of &lt;code&gt;static&lt;/code&gt; methods, go figure...&lt;/p&gt;

&lt;p&gt;In C++ a recent idiom is to &lt;a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=267612"&gt;prefer free functions&lt;/a&gt; over &lt;a href="http://www.gotw.ca/gotw/084.htm"&gt;object methods&lt;/a&gt; where possible. The argument is that creating non-member, non-friend, functions increases encapsulation because these functions don't need access to the object internals and therefore bundling them with the object simply reduces the encapsulation that the object provides by expanding the amount of code that has unnecessary access to the object's internals.&lt;/p&gt;

&lt;p&gt;This means that, in modern C++, you may often see things like this:&lt;br /&gt;
&lt;/p&gt;&lt;pre class="brush: cpp gutter: false"&gt;int ToInt32(const std::string &amp;amp;stringRep);&lt;/pre&gt;&lt;br /&gt;
which can be used like this:&lt;br /&gt;
&lt;pre class="brush: cpp gutter: false"&gt;const int i = ToInt32(customerNumber);&lt;/pre&gt;&lt;br /&gt;
Up until now the nearest that you could get to this in C# was something like this:&lt;br /&gt;
&lt;pre class="brush: cpp gutter: false"&gt;public class Stuff
{
  public static int ToInt32(string stringRep);
}&lt;/pre&gt;&lt;br /&gt;
which can be used like this:&lt;br /&gt;
&lt;pre class="brush: cpp gutter: false"&gt;int i = Stuff::ToInt32(customerNumber);&lt;/pre&gt;&lt;br /&gt;
The new "extension methods" proposal means that the call above can be rendered as this:&lt;br /&gt;
&lt;pre class="brush: cpp gutter: false"&gt;int i = customerNumber.ToInt32();&lt;/pre&gt;&lt;br /&gt;
Which, in my mind, is madness. The method is &lt;i&gt;not&lt;/i&gt; a member of the object so why should you call it using member syntax? What value does it add to be able to do this? If you're looking to remove the crufty &lt;code&gt;Stuff::&lt;/code&gt; from the front of the call then why not simply allow functions to be defined at namespace scope? Then you could bring the names in with a normal using statement and use them in a way that's expected:&lt;br /&gt;
&lt;pre class="brush: cpp gutter: false"&gt;int i = ToInt32(customerNumber);&lt;/pre&gt;&lt;br /&gt;
It's quite clear that &lt;code&gt;ToInt32&lt;/code&gt; comes from a namespace somewhere and isn't a member of the object in question... &lt;p&gt;&lt;/p&gt;

&lt;p&gt;So, my question to &lt;a href="http://blogs.msdn.com/ericgu/archive/2005/09/14/466510.aspx"&gt;all you .Net people&lt;/a&gt; is why is this new syntax a good thing? I can only see scope for confusion and the eventual banning of this kind of thing in local coding standards...&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/LenHolgate/Net/~4/aU2v0AXVzqA" height="1" width="1"/&gt;</content>
<feedburner:origLink>http://www.lenholgate.com/blog/2005/09/c-v3-extension-methods-syntactic-sugar-for-the-lack-of-free-functions.html</feedburner:origLink></entry>

</feed>

