<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Tobi + C# = T#</title> <link>http://www.fsmpi.uni-bayreuth.de/~dun3</link> <description>This is a collection of interesting stuff that I found on the web or stuff I worked on at the time.</description> <lastBuildDate>Tue, 08 May 2012 09:55:41 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/dun3" /><feedburner:info uri="dun3" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>Programmatically change power options using C#</title><link>http://feedproxy.google.com/~r/dun3/~3/9t4fff6rDJ4/519.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/programmatically-change-power-options-using-cshar/519.html#comments</comments> <pubDate>Tue, 08 May 2012 09:21:19 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=519</guid> <description><![CDATA[During the development of my little Set Power Scheme daemon, I did a dive into the Power Options API of Windows. Basically I needed a way to programmatically set the active power scheme. The power management functions are located in the PowrProf.dll. Unfortunately there is no managed wrapper to said functionality, but it is a [...]]]></description> <content:encoded><![CDATA[<p>During the development of my little <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/automatically-set-power-scheme">Set Power Scheme daemon</a>, I did a dive into the Power Options API of Windows. Basically I needed a way to programmatically set the active power scheme.  The <a
href="http://msdn.microsoft.com/en-us/library/aa373163.aspx">power management functions</a> are located in the PowrProf.dll. Unfortunately there is no managed wrapper to said functionality, but it is a pretty easy API to program against. Specifically we will look at PowerEnumerate, PowerReadFriendlyName, PowerGetActiveScheme and PowerSetActiveScheme.</p><p>The first thing one notices is that all power schemes have a unique Guid assigned, which is used to identify the available schemes. So, let’s just get all available Guids. For that we will use PowerEnumerate.</p><div
class="igBar"><span
id="lcsharp-7"><a
href="#" onclick="javascript:showCodeTxt('csharp-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-7"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">internal</span> <span
class="kw1">static</span> <span
class="kw4">class</span> WinAPI</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#91;</span>DllImport<span
class="br0">&#40;</span><span
class="st0">"PowrProf.dll"</span><span
class="br0">&#41;</span><span
class="br0">&#93;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">extern</span> UInt32 PowerEnumerate<span
class="br0">&#40;</span>IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, UInt32 AcessFlags, UInt32 Index, <span
class="kw1">ref</span> Guid Buffer, <span
class="kw1">ref</span> UInt32 BufferSize<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw4">enum</span> AccessFlags : <span
class="kw4">uint</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; ACCESS_SCHEME = <span
class="nu0">16</span>,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; ACCESS_SUBGROUP = <span
class="nu0">17</span>,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; ACCESS_INDIVIDUAL_SETTING = <span
class="nu0">18</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>Calling the function once will yield back the Guid of the power scheme at the specified schemeIndex. So, in order to get all we simply call the function in a while loop until no more schemes are found (= the return value of the function is not 0).</p><div
class="igBar"><span
id="lcsharp-8"><a
href="#" onclick="javascript:showCodeTxt('csharp-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-8"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> IEnumerable&lt;Guid&gt; FindAll<span
class="br0">&#40;</span><span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var schemeGuid = Guid.<span
class="me1">Empty</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">uint</span> sizeSchemeGuid = <span
class="br0">&#40;</span><span
class="kw4">uint</span><span
class="br0">&#41;</span>Marshal.<a
href="http://www.google.com/search?q=sizeof+msdn.microsoft.com"><span
class="kw3">SizeOf</span></a><span
class="br0">&#40;</span><a
href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span
class="kw3">typeof</span></a><span
class="br0">&#40;</span>Guid<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">uint</span> schemeIndex = <span
class="nu0">0</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">while</span> <span
class="br0">&#40;</span>WinAPI.<span
class="me1">PowerEnumerate</span><span
class="br0">&#40;</span>IntPtr.<span
class="me1">Zero</span>, IntPtr.<span
class="me1">Zero</span>, IntPtr.<span
class="me1">Zero</span>, <span
class="br0">&#40;</span><span
class="kw4">uint</span><span
class="br0">&#41;</span>WinAPI.<span
class="me1">AccessFlags</span>.<span
class="me1">ACCESS_SCHEME</span>, schemeIndex, <span
class="kw1">ref</span> schemeGuid, <span
class="kw1">ref</span> sizeSchemeGuid<span
class="br0">&#41;</span> == <span
class="nu0">0</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; yield <span
class="kw1">return</span> schemeGuid;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; schemeIndex++;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>Of course only reading the Guid is not very nice, so we will translate the Guid into a friendly name using the PowerReadFriendlyName call.</p><div
class="igBar"><span
id="lcsharp-9"><a
href="#" onclick="javascript:showCodeTxt('csharp-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-9"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="br0">&#91;</span>DllImport<span
class="br0">&#40;</span><span
class="st0">"PowrProf.dll"</span><span
class="br0">&#41;</span><span
class="br0">&#93;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">extern</span> UInt32 PowerReadFriendlyName<span
class="br0">&#40;</span>IntPtr RootPowerKey, <span
class="kw1">ref</span> Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, IntPtr Buffer, <span
class="kw1">ref</span> UInt32 BufferSize<span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>This API is not the easiest to use for us that are not using COM Interop on a daily basis. In order to call this API, we have to allocate memory for the friendly name to be written to and when we are done free said memory.</p><div
class="igBar"><span
id="lcsharp-10"><a
href="#" onclick="javascript:showCodeTxt('csharp-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-10"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">string</span> ReadFriendlyName<span
class="br0">&#40;</span>Guid schemeGuid<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">uint</span> sizeName = <span
class="nu0">1024</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; IntPtr pSizeName = Marshal.<span
class="me1">AllocHGlobal</span><span
class="br0">&#40;</span><span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>sizeName<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> friendlyName;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">try</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; WinAPI.<span
class="me1">PowerReadFriendlyName</span><span
class="br0">&#40;</span>IntPtr.<span
class="me1">Zero</span>, <span
class="kw1">ref</span> schemeGuid, IntPtr.<span
class="me1">Zero</span>, IntPtr.<span
class="me1">Zero</span>, pSizeName, <span
class="kw1">ref</span> sizeName<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; friendlyName = Marshal.<span
class="me1">PtrToStringUni</span><span
class="br0">&#40;</span>pSizeName<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">finally</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Marshal.<span
class="me1">FreeHGlobal</span><span
class="br0">&#40;</span>pSizeName<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> friendlyName;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>Notice that the free call is executed in a finally block, therefore guaranteeing that it is even called in any exception case.</p><p>Finally we can now use the Guid to either get or set the active power scheme.</p><div
class="igBar"><span
id="lcsharp-11"><a
href="#" onclick="javascript:showCodeTxt('csharp-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-11"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="br0">&#91;</span>DllImport<span
class="br0">&#40;</span><span
class="st0">"PowrProf.dll"</span><span
class="br0">&#41;</span><span
class="br0">&#93;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">extern</span> <span
class="kw4">uint</span> PowerGetActiveScheme<span
class="br0">&#40;</span>IntPtr UserRootPowerKey, <span
class="kw1">ref</span> IntPtr ActivePolicyGuid<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#91;</span>DllImport<span
class="br0">&#40;</span><span
class="st0">"PowrProf.dll"</span><span
class="br0">&#41;</span><span
class="br0">&#93;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">extern</span> <span
class="kw4">uint</span> PowerSetActiveScheme<span
class="br0">&#40;</span>IntPtr UserRootPowerKey, <span
class="kw1">ref</span> Guid SchemeGuid<span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><div
class="igBar"><span
id="lcsharp-12"><a
href="#" onclick="javascript:showCodeTxt('csharp-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-12"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">void</span> SetActive<span
class="br0">&#40;</span>Guid powerSchemeId<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var schemeGuid = powerSchemeId;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; WinAPI.<span
class="me1">PowerSetActiveScheme</span><span
class="br0">&#40;</span>IntPtr.<span
class="me1">Zero</span>, <span
class="kw1">ref</span> schemeGuid<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">public</span> Guid GetActive<span
class="br0">&#40;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; IntPtr pCurrentSchemeGuid = IntPtr.<span
class="me1">Zero</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; WinAPI.<span
class="me1">PowerGetActiveScheme</span><span
class="br0">&#40;</span>IntPtr.<span
class="me1">Zero</span>, <span
class="kw1">ref</span> pCurrentSchemeGuid<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var currentSchemeGuid = <span
class="br0">&#40;</span>Guid<span
class="br0">&#41;</span>Marshal.<span
class="me1">PtrToStructure</span><span
class="br0">&#40;</span>pCurrentSchemeGuid, <a
href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span
class="kw3">typeof</span></a><span
class="br0">&#40;</span>Guid<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> currentSchemeGuid;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>All this functionally is wrapped in a nice repository pattern in Set Power Scheme:</p><p><a
href="https://github.com/dun3/Com.Hertkorn.SetPowerScheme/blob/master/Com.Hertkorn.SetPowerScheme/Domain/PowerSchemeRepository.cs">PowerSchemeRepository.cs</a></p><p>Happy coding.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=9t4fff6rDJ4:PGPBjMt1m4k:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=9t4fff6rDJ4:PGPBjMt1m4k:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/9t4fff6rDJ4" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/programmatically-change-power-options-using-cshar/519.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/programmatically-change-power-options-using-cshar/519.html</feedburner:origLink></item> <item><title>The trouble of making a project public</title><link>http://feedproxy.google.com/~r/dun3/~3/m-yRtAG_VM4/511.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/the-trouble-of-making-a-project-public/511.html#comments</comments> <pubDate>Wed, 07 Mar 2012 12:27:40 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Project Management]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=511</guid> <description><![CDATA[I just wrote a little helper program that lets you track your currently set power scheme. And reset it, if any external sources (like Windows group policies) change said power options. I wrote this, because I have sometimes long running tasks that collide head-on with our IT departments wise decision to force a power scheme [...]]]></description> <content:encoded><![CDATA[<p>I just wrote a little helper program that lets you track your currently set power scheme. And reset it, if any external sources (like Windows group policies)  change said power options. I wrote this, because I have sometimes long running tasks that collide head-on with our IT departments wise decision to force a power scheme that puts my laptop to sleep after an hour. A laptop. What's the power consumption of that thing? And most of the time it's not even plugged in on site, spending company money.</p><p>Writing the tool was pretty easy, done in about 45 minutes of coding to get a basic sweet little exe that does nothing but parse a command-line argument, and force-set a power scheme. Which was more than fine by me, since I used the task scheduler to run this program every 5 minutes, knew how to correctly pass a guid to the program, was not surprised that it did not give any feedback, ...</p><p>I started showing the tool to a couple of friends, who - not surprisingly - had the same problems. <del>How come all IT departments of all major companies have the same stupid ideas? <img
src='http://www.fsmpi.uni-bayreuth.de/~dun3/tsharp/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </del> This is when the suggestions and the feedback started rolling in. So, at first I wrote "documentation" on how to use it. Then I added more elaborate command line parsing, because it seems people do not read manuals. Strange.<br
/> Then I set up a git repository on github, so I could start saying "is there a pull request for what you are suggesting" <del>and be all hip and stuff</del>. This did not work out so great, because I ended up adding a daemon mode. Some poor individuals apparently do not even have admin rights on their laptop - and could not set up a task in task scheduler.<br
/> And finally I added the possibility to copy and paste the power scheme guids from within the program because some people can't be bothered to use powercfg.exe.</p><p>And I feel like it is still not done. What are the todos? Write a blog post about the tool and set up a dedicated website for it.</p><p>So, going public meant going from a 45 min solution that worked brilliantly, to working more than 10 hours for it. And suddently feel like I am not done.</p><p>Is it always that hard to give back to the community? What is your experience?</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=m-yRtAG_VM4:feL1wNlPZh0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=m-yRtAG_VM4:feL1wNlPZh0:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/m-yRtAG_VM4" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/the-trouble-of-making-a-project-public/511.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/the-trouble-of-making-a-project-public/511.html</feedburner:origLink></item> <item><title>Signaling IIS that the app pool is still busy – Running Tasks in IIS</title><link>http://feedproxy.google.com/~r/dun3/~3/iRkAhX1cAOE/523.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/signaling-iis-that-the-app-pool-is-still-busy-running-tasks-in-iis/523.html#comments</comments> <pubDate>Wed, 08 Feb 2012 09:53:41 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=523</guid> <description><![CDATA[Unfortunately IIS is really not at all nice during app pool shutdown to threads running in its context. They simply are killed hard when a shutdown is initiated e.g. during an app pool recycle. Of course this is by no means a desired behavior and can lead to serious data corruption. Phill Haack already did [...]]]></description> <content:encoded><![CDATA[<p>Unfortunately IIS is really not at all nice during app pool shutdown to threads running in its context. They simply are killed hard when a shutdown is initiated e.g. during an app pool recycle.</p><p>Of course this is by no means a desired behavior and can lead to serious data corruption. Phill Haack already did an excellent blog post on this subject, so please go read <a
href="http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx">The Dangers of Implementing Recurring Background Tasks In ASP.NET</a> now. I’ll wait here.</p><p>Today I want to talk about an addition to RegisterObject. The IIS does check if an app pool is busy or idle and may initiate a shutdown simply because it wants to save resources by shutting down long idle app pools. So simply put, even though one correctly uses RegisterObject, the app pool still does not know that there is stuff going on, which is relevant and should mark the app pool as busy.</p><p>Like RegisterObject this is done using the <a
href="http://msdn.microsoft.com/en-us/library/1677yx1a.aspx">System.Web.Hosting.HostingEnvironment</a> class, specifically IncrementBusyCount() and DecrementBusyCount(). With a busy count larger than zero the app pool is marked as busy.</p><p>When running task I would recommend that any given tasks does two things before starting its work: Register an object as managed by the hosting environment using RegisterObject and increment the busy counter.</p><p>It goes without saying that it must be guaranteed that a task, even a failed one, does a proper cleanup, unregistering and decrementing the busy count.</p><p>Final side-note: Please do realize that the IIS will still initiate app pool shutdowns even on busy app pools. So there is still the need to use RegisterObject.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=iRkAhX1cAOE:BOZtxNW1alw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=iRkAhX1cAOE:BOZtxNW1alw:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/iRkAhX1cAOE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/signaling-iis-that-the-app-pool-is-still-busy-running-tasks-in-iis/523.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/signaling-iis-that-the-app-pool-is-still-busy-running-tasks-in-iis/523.html</feedburner:origLink></item> <item><title>Using conditional references to influence which assemblies are used for a specific project configuration or platform</title><link>http://feedproxy.google.com/~r/dun3/~3/Btq91z5xif0/496.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html#comments</comments> <pubDate>Tue, 22 Nov 2011 18:52:07 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[Uncategorized]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=496</guid> <description><![CDATA[Introduction When using specific third party assemblies it is necessary to distinguish between platforms. Best example are e.g. log4net, sqlite, … which come in both x86 and x64 but not in the general Any Cpu flavor. A typical development scenario could be that an ASP.NET application is developed using the IIS Express and deployed on [...]]]></description> <content:encoded><![CDATA[<h3>Introduction</h3><p>When using specific third party assemblies it is necessary to distinguish between platforms. Best example are e.g. log4net, sqlite, … which come in both x86 and x64 but not in the general Any Cpu flavor. A typical development scenario could be that an ASP.NET application is developed using the IIS Express and deployed on a 64bit IIS in production.<br
/> A different development scenario could be that a Silverlight application uses Design Time data in order to create a richer design time experience for the developer. In this situation a best practice is to put the classes containing the design time data into a separate assembly that is only referenced during development and is not deployed in production. Here one needs to distinguish between solution configuration Debug and Release.<br
/> In order to support these scenarios the project files allow for conditional references.<br
/> Unfortunately Visual Studio does not expose the conditional references feature in its UI. Therefore one has to edit the corresponding .csproj file by hand. This can be simply done using Visual Studio using the following steps:</p><ol><li>Right click the project one wants to edit</li><li>Select "Unload Project" from the popup menu</li><li>Right click the unloaded project</li><li>Select "Edit <projectfilename>"</li></ol><h3>Basic project file layout (Visual Studio 2010)</h3><p>A project file (.csproj) is an XML file containing an XML header and a root element <em>Project</em>. The <em>Project</em> can contain multiple elements of different types, most notably the <em>PropertyGroup</em>, <em>ItemGroup</em>, <em>Import</em> and <em>Target</em> elements. In this post I will not describe the specific use of each of the elements, but will concentrate on conditional assemblies. Documentation for these elements can be found on msdn.<br
/> Assembly or project references are found in an ItemGroup using either <em>Reference</em> or <em>ProjectReference</em> elements.<br
/> A typical .csproj producing either an x86 or x64 executable looks like this:</p><div
class="igBar"><span
id="lxml-18"><a
href="#" onclick="javascript:showCodeTxt('xml-18'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-18"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;</span>?xml <span
class="re0">version</span>=<span
class="st0">"1.0"</span> <span
class="re0">encoding</span>=<span
class="st0">"utf-8"</span>?<span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;Project</span> <span
class="re0">ToolsVersion</span>=<span
class="st0">"4.0"</span> <span
class="re0">DefaultTargets</span>=<span
class="st0">"Build"</span> <span
class="re0">xmlns</span>=<span
class="st0">"http://schemas.microsoft.com/developer/msbuild/2003"</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Configuration</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)' == '' "</span><span
class="re2">&gt;</span></span>Debug<span
class="sc3"><span
class="re1">&lt;/Configuration<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Platform</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Platform)' == '' "</span><span
class="re2">&gt;</span></span>x86<span
class="sc3"><span
class="re1">&lt;/Platform<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ProductVersion<span
class="re2">&gt;</span></span></span>8.0.30703<span
class="sc3"><span
class="re1">&lt;/ProductVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;SchemaVersion<span
class="re2">&gt;</span></span></span>2.0<span
class="sc3"><span
class="re1">&lt;/SchemaVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ProjectGuid<span
class="re2">&gt;</span></span></span>{5793FBD7-6122-484B-AD86-AA211162D0EB}<span
class="sc3"><span
class="re1">&lt;/ProjectGuid<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputType<span
class="re2">&gt;</span></span></span>Exe<span
class="sc3"><span
class="re1">&lt;/OutputType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;AppDesignerFolder<span
class="re2">&gt;</span></span></span>Properties<span
class="sc3"><span
class="re1">&lt;/AppDesignerFolder<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;RootNamespace<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences<span
class="sc3"><span
class="re1">&lt;/RootNamespace<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;AssemblyName<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences<span
class="sc3"><span
class="re1">&lt;/AssemblyName<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;TargetFrameworkVersion<span
class="re2">&gt;</span></span></span>v4.0<span
class="sc3"><span
class="re1">&lt;/TargetFrameworkVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;TargetFrameworkProfile<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;/TargetFrameworkProfile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;FileAlignment<span
class="re2">&gt;</span></span></span>512<span
class="sc3"><span
class="re1">&lt;/FileAlignment<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Debug|x86' "</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x86<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugSymbols<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/DebugSymbols<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>full<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\Debug\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>DEBUG;TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;WarningLevel<span
class="re2">&gt;</span></span></span>4<span
class="sc3"><span
class="re1">&lt;/WarningLevel<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Release|x86' "</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x86<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>pdbonly<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\Release\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;WarningLevel<span
class="re2">&gt;</span></span></span>4<span
class="sc3"><span
class="re1">&lt;/WarningLevel<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)|$(Platform)' == 'Debug|x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugSymbols<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/DebugSymbols<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\x64\Debug\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>DEBUG;TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>full<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x64<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span>bin\Debug\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.exe.CodeAnalysisLog.xml<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span>GlobalSuppressions.cs<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span>MinimumRecommendedRules.ruleset<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)|$(Platform)' == 'Release|x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\x64\Release\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>pdbonly<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x64<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span>bin\Release\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.exe.CodeAnalysisLog.xml<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span>GlobalSuppressions.cs<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span>MinimumRecommendedRules.ruleset<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"CustomMarshalers"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Core"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Xml.Linq"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.DataSetExtensions"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"Microsoft.CSharp"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Xml"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Compile</span> <span
class="re0">Include</span>=<span
class="st0">"Program.cs"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Compile</span> <span
class="re0">Include</span>=<span
class="st0">"Properties\AssemblyInfo.cs"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;None</span> <span
class="re0">Include</span>=<span
class="st0">"app.config"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;Import</span> <span
class="re0">Project</span>=<span
class="st0">"$(MSBuildToolsPath)\Microsoft.CSharp.targets"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="coMULTI">&lt;!-- To modify your build process, add your task inside one of the targets below and uncomment it. </span></div></li><li
class="li2"><div
class="de2"><span
class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp;Other similar extension points exist, see Microsoft.Common.targets.</span></div></li><li
class="li1"><div
class="de1"><span
class="coMULTI">&nbsp; &lt;Target Name=&quot;BeforeBuild&quot;&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/Target<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;Target</span> <span
class="re0">Name</span>=<span
class="st0">"AfterBuild"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/Target<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; --&gt;</div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/Project<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><h3>Conditionals</h3><p>In the above project one can already spot the mechanism used to distinguish between e.g. configurations and platforms.</p><div
class="igBar"><span
id="lxml-19"><a
href="#" onclick="javascript:showCodeTxt('xml-19'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-19"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Release|x86' "</span><span
class="re2">&gt;</span></span></div></li></ol></div></div></div><p></p><p>An XML node annotated with the <em>Condition</em> attribute is only included when parsing the file if the condition holds true. This mechanism can be applied to any element in the project file's XML structure.</p><h3>Using conditions for assembly references</h3><p>If one uses an assembly that is specifically designed for x86 or x64 one can use the conditional attribute to include the correct dll for the specified build platform. One of the best known dll of that type is SQLite:</p><p>Without the conditional a dll reference looks like this:</p><div
class="igBar"><span
id="lxml-20"><a
href="#" onclick="javascript:showCodeTxt('xml-20'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-20"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><p>To distinguish between the two states of 32bit and 64bit one simply doubles the<em>Reference</em> element and applies mutually exclusive <em>Condition</em> attributes to both:</p><div
class="igBar"><span
id="lxml-21"><a
href="#" onclick="javascript:showCodeTxt('xml-21'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-21"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(PlatformTarget)' == 'x86'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\x86\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(PlatformTarget)' == 'x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\x64\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><p>Note that the HintPath differs depending on the platform.</p><h3>Using conditions for project references</h3><p>If you have a project that should not get deployed as part of the final released product, why not use the <em>Condition</em> attribute to only include the project during debugging. One good example for these kinds of projects is Design-time data. Simply distinguish on the <em>Configuration</em> setting.</p><div
class="igBar"><span
id="lxml-22"><a
href="#" onclick="javascript:showCodeTxt('xml-22'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-22"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;ProjectReference</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="re0">Include</span>=<span
class="st0">"..\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design.csproj"</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)' == 'Debug'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;Project<span
class="re2">&gt;</span></span></span>{C7EAA6CA-1AF6-4D72-929B-D7D1177868D2}<span
class="sc3"><span
class="re1">&lt;/Project<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;Name<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design<span
class="sc3"><span
class="re1">&lt;/Name<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/ProjectReference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><h3>Hint</h3><p><strong>Keep in mind</strong> that the Visual Studio UI does not reflect conditional references within the "References" folder of a project. Therefore all references are visible in any given configuration or platform selection. The compiler and Intellisense on the other hand are aware of conditional references, honoring the correct settings both with visual feedback and error notification during builds.</p><h3>Summary</h3><p>Today we looked at the possibility to customize which assemblies or projects are referenced in each individual configuration or platform selection. The customization cannot be done using the UI, but is accomplished by manually editing the project file.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=Btq91z5xif0:Aq-TiS0K1Do:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=Btq91z5xif0:Aq-TiS0K1Do:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/Btq91z5xif0" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html</feedburner:origLink></item> <item><title>An updated look at Duck Typing in C# 4.0</title><link>http://feedproxy.google.com/~r/dun3/~3/OEv-GBQk6f8/490.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html#comments</comments> <pubDate>Wed, 05 Jan 2011 20:29:19 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=490</guid> <description><![CDATA[Download the source: blogducktypingreloaded.zip It's time to do an update on my blog post from the end of 2008, when I did take a first look at the dynamic capabilities in .NET 4.0. From the beta to the final version of 4.0 they did unfortunatelly change the API used for dynamic dispatching and dynamic objects. [...]]]></description> <content:encoded><![CDATA[<p><em>Download the source: <a
href='http://www.fsmpi.uni-bayreuth.de/~dun3/tsharp/wp-content/uploads/2011/01/blogducktypingreloaded.zip'>blogducktypingreloaded.zip</a></em></p><p>It's time to do an update on my <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/first-look-ducktyping-c-4-0-idynamicobject-metaobject/202.html">blog post from the end of 2008</a>, when I did take a first look at the dynamic capabilities in .NET 4.0. From the beta to the final version of 4.0 they did unfortunatelly change the API used for dynamic dispatching and dynamic objects. Instead of MetaObject and IDynamicObject all we need in the updated version of the API is System.Dynamics.DynamicObject. It takes the place of IDynamicObject and MetaObject is gone for good.</p><p>This makes it really pleasently easy to implement the same behaviour as I did years ago:</p><div
class="igBar"><span
id="lcsharp-25"><a
href="#" onclick="javascript:showCodeTxt('csharp-25'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-25"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw4">class</span> Duck : DynamicObject</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">private</span> Dictionary&lt;string, string&gt; dictionary = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Dictionary&lt;string, string&gt;<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TryGetMember<span
class="br0">&#40;</span>GetMemberBinder binder, <span
class="kw1">out</span> <span
class="kw4">object</span> result<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw4">string</span> resultString;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; var didWork = TryGetMember<span
class="br0">&#40;</span>binder.<span
class="me1">Name</span>, <span
class="kw1">out</span> resultString<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; result = resultString;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> didWork;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">private</span> <span
class="kw4">bool</span> TryGetMember<span
class="br0">&#40;</span><span
class="kw4">string</span> name, <span
class="kw1">out</span> <span
class="kw4">string</span> result<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> dictionary.<span
class="me1">TryGetValue</span><span
class="br0">&#40;</span>name, <span
class="kw1">out</span> result<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TrySetMember<span
class="br0">&#40;</span>SetMemberBinder binder, <span
class="kw4">object</span> value<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> TrySetMember<span
class="br0">&#40;</span>binder.<span
class="me1">Name</span>, value<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">private</span> <span
class="kw4">bool</span> TrySetMember<span
class="br0">&#40;</span><span
class="kw4">string</span> name, <span
class="kw4">object</span> value<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">try</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary<span
class="br0">&#91;</span>name<span
class="br0">&#93;</span> = value == <span
class="kw1">null</span> ? <span
class="kw1">null</span> : value.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">true</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">catch</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TryConvert<span
class="br0">&#40;</span>ConvertBinder binder, <span
class="kw1">out</span> <span
class="kw4">object</span> result<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">try</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = Generator.<span
class="me1">GenerateProxy</span><span
class="br0">&#40;</span>binder.<span
class="me1">Type</span>, <span
class="kw1">this</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">true</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">catch</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = <span
class="kw1">null</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>This is a dynamic Duck that can store any value when given via Property and will return the same value as a string when asked for it via the dynamic keyword.</p><div
class="igBar"><span
id="lcsharp-26"><a
href="#" onclick="javascript:showCodeTxt('csharp-26'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-26"><div
class="csharp"><ol><li
class="li1"><div
class="de1">dynamic d = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Duck<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">d.<span
class="me1">Test</span> = i;</div></li><li
class="li1"><div
class="de1">dynamic a = d.<span
class="me1">Test</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">Console.<span
class="me1">WriteLine</span><span
class="br0">&#40;</span>a<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">Console.<span
class="me1">WriteLine</span><span
class="br0">&#40;</span>a.<span
class="me1">GetType</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>.<span
class="me1">FullName</span> + <span
class="st0">" (Should be System.String)"</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>When you download the sample you can see that the code also includes the ability to do a cast from the dynamic Duck to any given interface, in this case an IQuack. So now the totally dynamic property bag is usable via a strongly typed interface!</p><p><em>Download the source: <a
href='http://www.fsmpi.uni-bayreuth.de/~dun3/tsharp/wp-content/uploads/2011/01/blogducktypingreloaded.zip'>blogducktypingreloaded.zip</a></em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=OEv-GBQk6f8:gvqFU9WyiWk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=OEv-GBQk6f8:gvqFU9WyiWk:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/OEv-GBQk6f8" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html</feedburner:origLink></item> <item><title>Creating an improved ASP.NET MVC 3 Html.ImageActionLink</title><link>http://feedproxy.google.com/~r/dun3/~3/mD9C0U6uhG0/483.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html#comments</comments> <pubDate>Sun, 05 Dec 2010 08:20:06 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=483</guid> <description><![CDATA[The old version of Html.ImageActionLink had a serious flaw - I had to resolve to replacing parts of the generated link after the fact in order to get non html encoded information in there. That obviously creates, though not really probable in this specific case, the problem of unwanted replacement is possible. So I learnt [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html">The old version of Html.ImageActionLink</a> had a serious flaw - I had to resolve to replacing parts of the generated link after the fact in order to get non html encoded information in there. That obviously creates, though not really probable in this specific case, the problem of unwanted replacement is possible.</p><p>So I learnt about UrlHelper and generate the whole MvcHtmlString in one go, without falling back on replacing stuff.</p><div
class="igBar"><span
id="lcsharp-29"><a
href="#" onclick="javascript:showCodeTxt('csharp-29'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-29"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> MvcHtmlString ImageActionLink<span
class="br0">&#40;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">this</span> HtmlHelper helper,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> imageUrl,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> altText,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> actionName,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> controllerName,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> routeValues,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> linkHtmlAttributes,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> imgHtmlAttributes<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var linkAttributes = AnonymousObjectToKeyValue<span
class="br0">&#40;</span>linkHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var imgAttributes = AnonymousObjectToKeyValue<span
class="br0">&#40;</span>imgHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var imgBuilder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"img"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"src"</span>, imageUrl<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"alt"</span>, altText<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttributes</span><span
class="br0">&#40;</span>imgAttributes, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var urlHelper = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> UrlHelper<span
class="br0">&#40;</span>helper.<span
class="me1">ViewContext</span>.<span
class="me1">RequestContext</span>, helper.<span
class="me1">RouteCollection</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var linkBuilder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"a"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; linkBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"href"</span>, urlHelper.<span
class="me1">Action</span><span
class="br0">&#40;</span>actionName, controllerName, routeValues<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; linkBuilder.<span
class="me1">MergeAttributes</span><span
class="br0">&#40;</span>linkAttributes, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var text = linkBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">StartTag</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; text += imgBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">SelfClosing</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; text += linkBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">EndTag</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">return</span> MvcHtmlString.<span
class="me1">Create</span><span
class="br0">&#40;</span>text<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">static</span> Dictionary&lt;string, object&gt; AnonymousObjectToKeyValue<span
class="br0">&#40;</span><span
class="kw4">object</span> anonymousObject<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var dictionary = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Dictionary&lt;string, object&gt;<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>anonymousObject != <span
class="kw1">null</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>PropertyDescriptor propertyDescriptor <span
class="kw1">in</span> TypeDescriptor.<span
class="me1">GetProperties</span><span
class="br0">&#40;</span>anonymousObject<span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary.<span
class="me1">Add</span><span
class="br0">&#40;</span>propertyDescriptor.<span
class="me1">Name</span>, propertyDescriptor.<span
class="me1">GetValue</span><span
class="br0">&#40;</span>anonymousObject<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> dictionary;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>The interface of the method is exactly the same so I can still use this version to create an image that is an actionlink in razor like this:</p><div
class="igBar"><span
id="lxml-30"><a
href="#" onclick="javascript:showCodeTxt('xml-30'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-30"><div
class="xml"><ol><li
class="li1"><div
class="de1">@Html.ImageActionLink(</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;Url.Content(&quot;~/Content/Images/&quot; + url),</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;alt,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;&quot;Details&quot;,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;&quot;Calender&quot;,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { area = &quot;&quot;, day = Model.Date.Day, month = Model.Date.Month, year = Model.Date.Year },</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;new { @class = &quot;SelectDay&quot;, onClick = string.Format(&quot;DoCallbackBookingDetails({0},{1},{2}); return false;&quot;, Model.Date.Day, Model.Date.Month, Model.Date.Year) },</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { style = &quot;border: 0px;&quot; })</div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=mD9C0U6uhG0:aPlvRDIzh9o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=mD9C0U6uhG0:aPlvRDIzh9o:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/mD9C0U6uhG0" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html/feed</wfw:commentRss> <slash:comments>13</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html</feedburner:origLink></item> <item><title>Creating an ASP.NET MVC 3 Html.ActionLink that is an Image</title><link>http://feedproxy.google.com/~r/dun3/~3/rukZ3MCqZ5g/479.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html#comments</comments> <pubDate>Sat, 04 Dec 2010 22:28:55 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=479</guid> <description><![CDATA[[UPDATE: There is an improved version of Html.ActionLink: Creating an improved ASP.NET MVC 3 Html.ImageActionLink] Have I told you that I really enjoy ASP.NET MVC 3 Razor? Damn, that's nice to work with. I am doing a small, fun project for a friend of mine and I stumbled across the situation that I wanted to [...]]]></description> <content:encoded><![CDATA[<p><em>[UPDATE: There is an improved version of Html.ActionLink: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p><p>Have I told you that I really enjoy ASP.NET MVC 3 Razor? Damn, that's nice to work with. I am doing a small, fun project for a friend of mine and I stumbled across the situation that I wanted to have an image as an ActionLink. And since the @Html.ActionLink syntax is really sexy, I wanted to write an Extension method that does the same with an image.</p><p>The solution is this quick hack:</p><p><em>[UPDATE: There is an improved version without the hacky replace: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p><div
class="igBar"><span
id="lcsharp-33"><a
href="#" onclick="javascript:showCodeTxt('csharp-33'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-33"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> MvcHtmlString ImageActionLink<span
class="br0">&#40;</span><span
class="kw1">this</span> HtmlHelper helper,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> imageUrl,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> altText,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> actionName,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> controllerName,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> routeValues,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> linkHtmlAttributes,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> imgHtmlAttributes<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var attributes = HtmlHelper.<span
class="me1">AnonymousObjectToHtmlAttributes</span><span
class="br0">&#40;</span>imgHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var builder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"img"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"src"</span>, imageUrl<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"alt"</span>, altText<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>var key <span
class="kw1">in</span> attributes.<span
class="me1">Keys</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; var value = attributes<span
class="br0">&#91;</span>key<span
class="br0">&#93;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw4">string</span> valueAsString = <span
class="kw1">null</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>value != <span
class="kw1">null</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueAsString = value.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span>key, valueAsString<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var link = helper.<span
class="me1">ActionLink</span><span
class="br0">&#40;</span><span
class="st0">"[placeholder]"</span>, actionName, controllerName, routeValues, linkHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var text = link.<span
class="me1">ToHtmlString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; text = text.<span
class="me1">Replace</span><span
class="br0">&#40;</span><span
class="st0">"[placeholder]"</span>, builder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">SelfClosing</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">return</span> MvcHtmlString.<span
class="me1">Create</span><span
class="br0">&#40;</span>text<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>We return a MvcHtmlString, so we can indicate that this sting is not supposed to be html encoded after we return it to the templating engine. Plus we use the handy AnonymousObjectToHtmlAttributes helper method in order to enable us to pass html attributes to the img tag using anonymous classes, just as we are used to by now.</p><p>Unfortunatelly we can not tell the helper.ActionLink call to not html encode what we pass as linkText - therefore we fall back on using a place holder that we replace after the fact.</p><p>So now we can do a</p><div
class="igBar"><span
id="lxml-34"><a
href="#" onclick="javascript:showCodeTxt('xml-34'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-34"><div
class="xml"><ol><li
class="li1"><div
class="de1">@Html.ImageActionLink(</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;Url.Content(&quot;~/Content/Images/&quot; + url),</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;alt,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;&quot;Details&quot;,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;&quot;Calender&quot;,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { area = &quot;&quot;, day = Model.Date.Day, month = Model.Date.Month, year = Model.Date.Year },</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;new { @class = &quot;SelectDay&quot;, onClick = string.Format(&quot;DoCallbackBookingDetails({0},{1},{2}); return false;&quot;, Model.Date.Day, Model.Date.Month, Model.Date.Year) },</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { style = &quot;border: 0px;&quot; })</div></li></ol></div></div></div><p></p><p>right inside Razor. Beautiful!</p><p><em>[UPDATE: There is an improved helper to create an image that has an actionlink url a href: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=rukZ3MCqZ5g:oFOyNdYTBc0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=rukZ3MCqZ5g:oFOyNdYTBc0:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/rukZ3MCqZ5g" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html</feedburner:origLink></item> <item><title>Active Directory Tip #9 – How to escape the Path property</title><link>http://feedproxy.google.com/~r/dun3/~3/svp6hfci8DE/458.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html#comments</comments> <pubDate>Mon, 17 May 2010 19:00:28 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=458</guid> <description><![CDATA[Did you get the dreaded 0x80005000 COMException "Unknown error"? Maybe you should check, if your AD entry contains a forward slash '/' in its DN. Because there is a subtile difference between a correctly escaped distinguished name and a correctly escaped ldap path. Observe: PLAIN TEXT PLAIN: Unescaped:&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description> <content:encoded><![CDATA[<p>Did you get the dreaded 0x80005000 COMException "Unknown error"? Maybe you should check, if your AD entry contains a forward slash '/' in its DN. Because there is a subtile difference between a correctly escaped distinguished name and a correctly escaped ldap path.</p><p>Observe:</p><div
class="igBar"><span
id="lplain-37"><a
href="#" onclick="javascript:showCodeTxt('plain-37'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PLAIN:</span><div
id="plain-37"><div
class="plain"><ol><li
class="li1"><div
class="de1">Unescaped:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CN=Test\ Me \\ huhu \/ cool / \\/ <span
class="co1">// \\// \\\/// \\\\////! /////,OU=AdTests,DC=test,DC=domain,DC=local</span></div></li><li
class="li2"><div
class="de2">Escaped DN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CN=Test\\ Me \\\\ huhu \\/ cool / \\\\/ <span
class="co1">// \\\\// \\\\\\/// \\\\\\\\////! /////,OU=AdTests,DC=test,DC=domain,DC=local</span></div></li><li
class="li1"><div
class="de1">Escaped Path: LDAP:<span
class="co1">//test.domain.local:389/CN=Test\\ Me \\\\ huhu \\\/ cool \/ \\\\\/ \/\/ \\\\\/\/ \\\\\\\/\/\/ \\\\\\\\\/\/\/\/! \/\/\/\/\/,OU=AdTests,DC=test,DC=domain,DC=local </span></div></li></ol></div></div></div><p></p><p>Notice how forward slashes need to be escaped in addition to how distinguished names get escaped. This is a very subtile thing that is often overlooked. So when converting between DNs and Paths you have to use the following methods:</p><div
class="igBar"><span
id="lcsharp-38"><a
href="#" onclick="javascript:showCodeTxt('csharp-38'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-38"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw1">readonly</span> <span
class="kw4">string</span> LDAP_PREFIX = <span
class="st0">@"LDAP://test.domain.local:389/"</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">string</span> ConvertDnToPath<span
class="br0">&#40;</span><span
class="kw4">string</span> dn<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; var exploded = dn.<span
class="me1">ToCharArray</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; StringBuilder sb = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> StringBuilder<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>var @<span
class="kw4">char</span> <span
class="kw1">in</span> exploded<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>@<span
class="kw4">char</span> == <span
class="st0">'/'</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; sb.<span
class="me1">Append</span><span
class="br0">&#40;</span><span
class="st0">'<span
class="es0">\\</span>'</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; sb.<span
class="me1">Append</span><span
class="br0">&#40;</span>@<span
class="kw4">char</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">return</span> LDAP_PREFIX + sb.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">string</span> ConvertPathToDn<span
class="br0">&#40;</span><span
class="kw4">string</span> path<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; path = path.<span
class="me1">Substring</span><span
class="br0">&#40;</span>path.<span
class="me1">IndexOf</span><span
class="br0">&#40;</span><span
class="st0">'/'</span>, <span
class="st0">"LDAP://"</span>.<span
class="me1">Length</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="co1">// remove leading /</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">while</span> <span
class="br0">&#40;</span>path.<span
class="me1">StartsWith</span><span
class="br0">&#40;</span><span
class="st0">"/"</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; path = path.<span
class="me1">Substring</span><span
class="br0">&#40;</span><span
class="nu0">1</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>!path.<span
class="me1">Contains</span><span
class="br0">&#40;</span><span
class="st0">'/'</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> path;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; StringBuilder sb = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> StringBuilder<span
class="br0">&#40;</span>path<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">bool</span> slashMode = <span
class="kw1">false</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">int</span> backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">for</span> <span
class="br0">&#40;</span><span
class="kw4">int</span> i = path.<span
class="me1">Length</span> - <span
class="nu0">1</span>; i&gt;= <span
class="nu0">0</span>; i--<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>path<span
class="br0">&#91;</span>i<span
class="br0">&#93;</span> == <span
class="st0">'<span
class="es0">\\</span>'</span> &amp;&amp; slashMode<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount++;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">else</span> <span
class="kw1">if</span> <span
class="br0">&#40;</span>slashMode<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span><span
class="br0">&#40;</span>backslashCount % <span
class="nu0">2</span><span
class="br0">&#41;</span> != <span
class="nu0">0</span><span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; sb.<span
class="me1">Remove</span><span
class="br0">&#40;</span>i + <span
class="nu0">1</span>, <span
class="nu0">1</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; slashMode = <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>path<span
class="br0">&#91;</span>i<span
class="br0">&#93;</span> == <span
class="st0">'/'</span><span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; slashMode = <span
class="kw1">true</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">return</span> sb.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=svp6hfci8DE:7Viw5Ww3tSw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=svp6hfci8DE:7Viw5Ww3tSw:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/svp6hfci8DE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html</feedburner:origLink></item> <item><title>Active Directory Tip #8 – Set Password of user</title><link>http://feedproxy.google.com/~r/dun3/~3/AO_RXZfCrTM/453.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html#comments</comments> <pubDate>Fri, 14 May 2010 18:21:26 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=453</guid> <description><![CDATA[The password of a person entry is not a regular property with direct access for reading and writing. Instead it can't be read just indirectly written to via the setPassword method. PLAIN TEXT C#: string password = "secret"; directoryEntry.Invoke&#40;"setPassword", password&#41;; directoryEntry.RefreshCache&#40;&#41;; The caller must have the User-Force-Change-Password Extended Right to set the password with this [...]]]></description> <content:encoded><![CDATA[<p>The password of a person entry is not a regular property with direct access for reading and writing. Instead it can't be read just indirectly written to via the setPassword method.</p><div
class="igBar"><span
id="lcsharp-40"><a
href="#" onclick="javascript:showCodeTxt('csharp-40'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-40"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw4">string</span> password = <span
class="st0">"secret"</span>;</div></li><li
class="li2"><div
class="de2">directoryEntry.<span
class="me1">Invoke</span><span
class="br0">&#40;</span><span
class="st0">"setPassword"</span>, password<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">directoryEntry.<span
class="me1">RefreshCache</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>The caller must have the <a
href="http://msdn.microsoft.com/en-us/library/ms684414.aspx">User-Force-Change-Password Extended Right</a> to set the password with this method.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=AO_RXZfCrTM:on6WE4L7YHM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=AO_RXZfCrTM:on6WE4L7YHM:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/AO_RXZfCrTM" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html</feedburner:origLink></item> <item><title>Active Directory Tip #7 – Handling a Bit Property (e.g. userAccountControl – Account is disabled)</title><link>http://feedproxy.google.com/~r/dun3/~3/DCPBjTSyXoo/450.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html#comments</comments> <pubDate>Wed, 12 May 2010 18:14:05 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=450</guid> <description><![CDATA[Bit properties like User-Account-Control Attribute in the Active Directory are represented by regular integers. Therefore all the usual bit operation may be performed on those fields. PLAIN TEXT C#: public static bool GetBitAttribute&#40;DirectoryEntry directoryEntry, string attributeName, int bitmask&#41; &#123; &#160; int value = &#40;int&#41;DirectoryEntryHelper.GetAdObjectProperty&#40;directoryEntry, attributeName&#41;; &#160; bool result = &#40;bitmask &#38; value&#41; == bitmask; &#160; [...]]]></description> <content:encoded><![CDATA[<p>Bit properties like <a
href="http://msdn.microsoft.com/en-us/library/ms680832.aspx">User-Account-Control Attribute</a> in the Active Directory are represented by regular integers. Therefore all the usual bit operation may be performed on those fields.</p><div
class="igBar"><span
id="lcsharp-44"><a
href="#" onclick="javascript:showCodeTxt('csharp-44'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-44"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw4">bool</span> GetBitAttribute<span
class="br0">&#40;</span>DirectoryEntry directoryEntry, <span
class="kw4">string</span> attributeName, <span
class="kw4">int</span> bitmask<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> value = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>DirectoryEntryHelper.<span
class="me1">GetAdObjectProperty</span><span
class="br0">&#40;</span>directoryEntry, attributeName<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">bool</span> result = <span
class="br0">&#40;</span>bitmask &amp; value<span
class="br0">&#41;</span> == bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">return</span> result;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">void</span> SetBitAttribute<span
class="br0">&#40;</span>DirectoryEntry directoryEntry, <span
class="kw4">string</span> attributeName, <span
class="kw4">int</span> bitmask, <span
class="kw4">bool</span> attributeValue<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; PropertyValueCollection property = directoryEntry.<span
class="me1">Properties</span><span
class="br0">&#91;</span>attributeName<span
class="br0">&#93;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> value = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>property.<span
class="me1">Value</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>attributeValue<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; value |= bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">else</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; value &amp;= ~bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; property.<span
class="me1">Value</span> = value;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>With these helper functions deactivating a user would be accomplished by using</p><div
class="igBar"><span
id="lcsharp-45"><a
href="#" onclick="javascript:showCodeTxt('csharp-45'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-45"><div
class="csharp"><ol><li
class="li1"><div
class="de1">SetBitAttribute<span
class="br0">&#40;</span>directoryEntry, <span
class="st0">"userAccountControl"</span>, ADS_UF_ACCOUNTDISABLE, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Just for convenience, here is the table form <a
href="http://msdn.microsoft.com/en-us/library/ms680832.aspx">User-Account-Control Attribute</a> converted to C# syntax:</p><div
class="igBar"><span
id="lcsharp-46"><a
href="#" onclick="javascript:showCodeTxt('csharp-46'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-46"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The logon script is executed.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SCRIPT = 0x00000001;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user account is disabled.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_ACCOUNTDISABLE = 0x00000002;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The home directory is required.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_HOMEDIR_REQUIRED = 0x00000008;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The account is currently locked out.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_LOCKOUT = 0x00000010;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// No password is required.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_PASSWD_NOTREQD = 0x00000020;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user cannot change the password.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_PASSWD_CANT_CHANGE = 0x00000040;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user can send an encrypted password.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0x00000080;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is an account for users whose primary account is in </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// another domain. This account provides user access to this </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// domain, but not to any domain that trusts this domain. </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// Also known as a local user account.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0x00000100;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a default account type that represents a typical user.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_NORMAL_ACCOUNT = 0x00000200;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a permit to trust account for a system domain that </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// trusts other domains.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0x00000800;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is a computer account for a computer that is a member </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// of this domain.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x00001000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a computer account for a system backup domain controller </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// that is a member of this domain.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SERVER_TRUST_ACCOUNT = 0x00002000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The password for this account will never expire.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_DONT_EXPIRE_PASSWD = 0x00010000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is an MNS logon account.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_MNS_LOGON_ACCOUNT = 0x00020000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user must log on using a smart card.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SMARTCARD_REQUIRED = 0x00040000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The service account (user or computer account), under which a </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// service runs, is trusted for Kerberos delegation. Any such service </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// can impersonate a client requesting the service.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_TRUSTED_FOR_DELEGATION = 0x00080000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The security context of the user will not be delegated to a service </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// even if the service account is set as trusted for Kerberos delegation.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_NOT_DELEGATED = 0x00100000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// Restrict this principal to use only Data Encryption Standard (DES) </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// encryption types for keys.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_USE_DES_KEY_ONLY = 0x00200000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This account does not require Kerberos pre-authentication for logon.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_DONT_REQUIRE_PREAUTH = 0x00400000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user password has expired. This flag is created by the system </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// using data from the Pwd-Last-Set attribute and the domain policy.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_PASSWORD_EXPIRED = 0x00800000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// The account is enabled for delegation. This is a security-sensitive </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// setting; accounts with this option enabled should be strictly </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// controlled. This setting enables a service running under the account </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// to assume a client identity and authenticate as that user to other </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// remote servers on the network.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x01000000;</div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=DCPBjTSyXoo:WnW5HIGNjNo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=DCPBjTSyXoo:WnW5HIGNjNo:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/DCPBjTSyXoo" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html</feedburner:origLink></item> </channel> </rss><!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching using disk

Served from: btfmx2.fs.uni-bayreuth.de @ 2012-05-14 02:42:30 -->

