<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Leaning Into Windows : Sytem.AddIn, Visual Basic</title><link>http://msmvps.com/blogs/kathleen/archive/tags/Sytem.AddIn/Visual+Basic/default.aspx</link><description>Tags: Sytem.AddIn, Visual Basic</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Why You Care About System.AddIn</title><link>http://msmvps.com/blogs/kathleen/archive/2008/01/03/why-you-care-about-system-addin.aspx</link><pubDate>Thu, 03 Jan 2008 19:31:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1435614</guid><dc:creator>Kathleen</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/kathleen/rsscomments.aspx?PostID=1435614</wfw:commentRss><comments>http://msmvps.com/blogs/kathleen/archive/2008/01/03/why-you-care-about-system-addin.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;When I was fighting with AppDomains to support XML Linq code generation in my new Workflow based code generator, Bill McCarthy said “Hey did you look at System.AddIn” and I said “No, silly I’m not writing add-ins.” &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Well, a few months later, I’m still trying to make it work, and have come to think it’s worth the trouble. So, first, what System.AddIn namespace offers, then why it’s so painful, then what I’m doing to fix your pain. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Simply put, System.AddIn provides abstracted/isolated app domain access. AppDomains are the boundary at which some security stuff happens, and the unit which must be unloaded as a group. You can load individual assemblies into an app domain, but to unload them, you need to unload the entire app domain. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;There are a few scenarios where this is important – sandboxing code you’re running as a plug-in to your application being the one the designers had in mind. I want to use it so I can load my code generator and have it recognize changes in .NET assemblies that are generating code. With my first tool, I never solved this problem because I didn’t think brute force code generation prior to XML literals in Visual Basic made very much sense. You had a lot of the problems with XSLT (whitespace) and a nearly complete inability to search your templates (since we cannot search separately in quoted text. XML literal code generation is the best way yet to generate code – as powerful as XSLT and as easy as CodeSmith. Anyway, I can get carried away on that – it’s why I was willing to invest heavily in System.AddIn.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Along the way, I gained great respect for the complex model that supplies isolation/abstraction. If you’ve ever played&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;with plug-ins you know that the first version of your app and its plug-ins is OK, but keeping things in sync while multiple synergistic applications evolve is nearly impossible. The isolation model means the host only speaks to an adapter, and the add-in only needs to speak to an adapter. The adapter’s functionality and the contract can change in whatever manner is needed. This model, combined with the app domain management may lead System.AddIn to have an important role in your application if your application needs to provide variants for individual clients. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Hopefully you have a good idea what sorts of things clients are going to want to customize, and you place this into an API you hit via the add-in model. If you got it 75% correct out of the chute, it would be a miracle, so the capacity for change built into the isolation model is what actually makes this work &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Literally, you load code on the fly, with whatever security limitations you want, with the ability to unload at your convenience, and pick the correct code from what’s available in a specific directory location. Cool huh!&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;In WinForms, the WinForms threading model prohibits UI’s in the add-in. I understand this is fixed in WPF, although I haven’t yet written a WPF add-in user interface.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;So, now that you have some idea why System.AddIn is worth the trouble, why is it so painful. How could I have possibly spent so long getting it running in a sample (I just output a single quoted string right now). To provide the isolation there is a minimum of seven projects/assemblies involved. These must be deployed in a very specific directory structure for the AddIn system to find the pieces it needs when it needs it. Then there is the error reporting problem – I’ve blogged about a particularly nasty “The target application domain has been unloaded” error.&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;So, once you hold your mouth just right, and all your code is perfect, it’s cool. But how many of you right perfect code? And what’s this about an easy maintenance model if you have to change SEVEN assemblies to alter the API. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;I’m working on an article and tool for my column in Visual Studio Magazine that will take either metadata for the API, or the interface and build the simple pass through model. This gets you started. Later when you have interface changes, the isolation model pays for itself, but at that point you understand what’s happening. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;It’s going to be a pretty cool example of the “overwrite until edited” mode that my tool supports. Before I’ve used this for editable files that were pretty much empty. Now, I want to separate changes due to metadata changes – that could be significant – from those for actual mapping you did in the adapters. With luck partial methods will lead to a pretty robust set of code you can alter as you need, while still generating the main API stream. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;I find it very cool to see so many fragments coming together. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1435614" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/kathleen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Code+Generation/default.aspx">Code Generation</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Design/default.aspx">Design</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Sytem.AddIn/default.aspx">Sytem.AddIn</category></item><item><title>System.Addin “The target application domain has been unloaded”</title><link>http://msmvps.com/blogs/kathleen/archive/2008/01/03/system-addin-the-target-application-domain-has-been-unloaded.aspx</link><pubDate>Thu, 03 Jan 2008 19:29:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1435612</guid><dc:creator>Kathleen</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/kathleen/rsscomments.aspx?PostID=1435612</wfw:commentRss><comments>http://msmvps.com/blogs/kathleen/archive/2008/01/03/system-addin-the-target-application-domain-has-been-unloaded.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;I have just escaped an Alice in Wonderland type maze with System.AddIn namespace. It’s a bit of a nightmare to create the plumbing for an add-in so stay tuned in Visual Studio Magazine for a column on how to generate this so you only do the logical work &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="MARGIN:0in 0in 0pt 0.5in;TEXT-INDENT:-0.25in;mso-list:l0 level1 lfo1;"&gt;&lt;span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;"&gt;&lt;span style="mso-list:Ignore;"&gt;&lt;font face="Calibri" size="3"&gt;a)&lt;/font&gt;&lt;span style="FONT:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Calibri" size="3"&gt;Define the add-in API&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="MARGIN:0in 0in 10pt 0.5in;TEXT-INDENT:-0.25in;mso-list:l0 level1 lfo1;"&gt;&lt;span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;"&gt;&lt;span style="mso-list:Ignore;"&gt;&lt;font face="Calibri" size="3"&gt;b)&lt;/font&gt;&lt;span style="FONT:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Calibri" size="3"&gt;Write the add-in&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;If you have created one of these in .NET 3.5 you know how tremendously painful it can be. The particular gotcha that I spent numerous days spread over the last three months is that the host adapter much set the contract handle:&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;Private&lt;/span&gt; _handle &lt;span style="COLOR:blue;"&gt;As&lt;/span&gt; System.AddIn.Pipeline.ContractHandle&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;Public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;Sub&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;New&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;ByVal&lt;/span&gt; contract &lt;span style="COLOR:blue;"&gt;As&lt;/span&gt; ILinqGenerator)&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;Me&lt;/span&gt;.contract = contract&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;_handle = &lt;span style="COLOR:blue;"&gt;New&lt;/span&gt; System.AddIn.Pipeline.ContractHandle(contract)&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Lucida Console&amp;#39;;mso-bidi-font-family:&amp;#39;Times New Roman&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;End&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Now, you might be like me and look at that line of code and say “Hey, no one anywhere is using the _handle variable, so why not just drop it.” That would be consistent with the fact the same sample has numerous Friend methods that are never used. Heck, FxCop is even going to tell me to get rid of this line of code.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;However, it’s required. Without this line of code, you get the error “The target application domain has been unloaded.” &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;But I said I spent days on this, over months which meant I kept coming back its fair to ask why? But that’s the subject for another post. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1435612" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/kathleen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://msmvps.com/blogs/kathleen/archive/tags/Sytem.AddIn/default.aspx">Sytem.AddIn</category></item></channel></rss>