<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Anton Gochev's Weblog</title>
	
	<link>http://devblog.antongochev.net</link>
	<description>Programming is an art...</description>
	<pubDate>Tue, 09 Dec 2008 13:09:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/antongochev/feed" type="application/rss+xml" /><item>
		<title>COMException- Unsupported type for field or property setter</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/479508646/</link>
		<comments>http://devblog.antongochev.net/2008/12/09/comexception-unsupported-type-for-field-or-property-setter/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 13:09:08 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/2008/12/09/comexception-unsupported-type-for-field-or-property-setter/</guid>
		<description><![CDATA[I need a custom role based security for our project. It should be based on resources and resources&#8217; permissions. All resources are somehow objects. 
Firstly, I&#8217;ve implemented custom principal inherited from GenericPrincipal class, custom permission that implements IPermission and IUnrestrictedPermission. And than as it is expected I implement my realization of CodeAccessSecurityAttribute in order to [...]]]></description>
			<content:encoded><![CDATA[<p>I need a custom role based security for our project. It should be based on resources and resources&#8217; permissions. All resources are somehow objects. </p>
<p>Firstly, I&#8217;ve implemented custom principal inherited from GenericPrincipal class, custom permission that implements IPermission and IUnrestrictedPermission. And than as it is expected I implement my realization of CodeAccessSecurityAttribute in order to be able to have declarative security. It contains a property for setting/getting the resource type. I also pass all the path to sign my assembly and put it into GAC etc. And then I&#8217;ve tried to see how it works and Bum! I got the exception &quot;COMException- Unsupported type for field or property setter&quot; which told me that what is wrong with my security attribute properties. I supposed that the reason is my resource type property and I was right. Here is a <a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94116" target="_blank" onclick="javascript:urchinTracker ('/outbound/article/connect.microsoft.com');">bug report</a> about this problem. Actually, it is not a bug but a normal situation when it is a design problem :(.</p>
<p>It is clear that I can use type with an implementation of a custom security attribute based on CodeAccessSecurityAttribute. But what do you think about using an aspect? I chose to use an aspect attribute in order to achieve a declarative security. I used <a href="http://www.postsharp.org/" target="_blank" onclick="javascript:urchinTracker ('/outbound/article/www.postsharp.org');">PostSharp</a> to create my aspect attribute. </p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> [AttributeUsage(AttributeTargets.Event | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = <span style="color: #0000ff">true</span>, Inherited = <span style="color: #0000ff">true</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> PermissionAspectAttribute : OnMethodBoundaryAspect</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> Type ResourceType { get; <span style="color: #0000ff">private</span> set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> Permission NeededPermission { get; <span style="color: #0000ff">private</span> set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">public</span> PermissionAspectAttribute(Type resourceType, Permission neededPermission)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>     {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>         ResourceType = resourceType;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>         NeededPermission = neededPermission;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>     }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnEntry(MethodExecutionEventArgs eventArgs)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>     {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>         ResourcePermission permission = <span style="color: #0000ff">new</span> ResourcePermission(ResourceType, NeededPermission);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>         permission.Demand();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>     }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span> }</pre>
</p></div>
</div>
<p>You can see that I instantiate my permission and call its Demand() method. Then it was easy to use the attribute in a code. Let&#8217;s assume that I have a method Get() as in the snippet below.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> [PermissionAspect(<span style="color: #0000ff">typeof</span>(User), Permission.View)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> ICollection&lt;User&gt; Get()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     ICollection&lt;User&gt; users = DummyMethod();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">return</span> users;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span> }</pre>
</p></div>
</div>
<p>PostSharp includes the aspect code into the method decorated during the compilation of the program. This way I have a way to use my custom security declaratively and all people of the team do not need to right each time permission initialization code. </p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/479508646" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/12/09/comexception-unsupported-type-for-field-or-property-setter/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/12/09/comexception-unsupported-type-for-field-or-property-setter/</feedburner:origLink></item>
		<item>
		<title>FormsAuthentication.Encrypt(ticket) returns null</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/476005162/</link>
		<comments>http://devblog.antongochev.net/2008/12/05/formsauthenticationencryptticket-returns-null/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 20:06:40 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/2008/12/05/formsauthenticationencryptticket-returns-null/</guid>
		<description><![CDATA[I&#8217;ve done a sample today and I need to create FormsAuthenticationTicket manually in order to make it inexpiable. I create specifying null for user data in the constructor.


   1: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName,
   2:     DateTime.Now, expiredTime, persist, null, FormsAuthentication.FormsCookiePath);
   3:&#160; 
   [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve done a sample today and I need to create <a href="http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx" target="_blank" onclick="javascript:urchinTracker ('/outbound/article/msdn.microsoft.com');">FormsAuthenticationTicket</a> manually in order to make it inexpiable. I create specifying null for user data in the constructor.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> FormsAuthenticationTicket ticket = <span style="color: #0000ff">new</span> FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>     DateTime.Now, expiredTime, persist, <span style="color: #0000ff">null</span>, FormsAuthentication.FormsCookiePath);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> HttpCookie cookie = <span style="color: #0000ff">new</span> HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));</pre>
</p></div>
</div>
<p>Firstly, I wasn&#8217;t able to continue to default page of the site and I was always returned to the login page. After checking that all of my code is ok I observed created cookie and what I&#8217;ve seen was that it doesn&#8217;t contain any data. Normally, it should contain encrypted ticket. Then I saw that the call of FormsAuthentication.Encrypt(ticket) returns null. </p>
<p>After replacing null with string.Empty in the constructor of FormsAuthenticationTicket all went fine.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> FormsAuthenticationTicket ticket = <span style="color: #0000ff">new</span> FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>     DateTime.Now, expiredTime, persist, <span style="color: #0000ff">string</span>.Empty, FormsAuthentication.FormsCookiePath);</pre>
</p></div>
</div>
<p>I do not know what is the reason of such behavior but for me it is unnatural behavior. </p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/476005162" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/12/05/formsauthenticationencryptticket-returns-null/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/12/05/formsauthenticationencryptticket-returns-null/</feedburner:origLink></item>
		<item>
		<title>Access an web service from web application with different default credentials</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/473621204/</link>
		<comments>http://devblog.antongochev.net/2008/12/03/access-an-web-service-from-web-application-with-different-default-credentials/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 14:01:39 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/2008/12/03/access-an-web-service-from-web-application-with-different-default-credentials/</guid>
		<description><![CDATA[Today, a developer from the team asked me how to access a web service in our domain with user default credentials but for different user that those in WindowsIdentity.GetCurrent().
Let&#8217;s say that there is a web site that is configured to use Windows authentication in ISS and in the web configuration file we have Windows authentication [...]]]></description>
			<content:encoded><![CDATA[<p>Today, a developer from the team asked me how to access a web service in our domain with user default credentials but for different user that those in WindowsIdentity.GetCurrent().</p>
<p>Let&#8217;s say that there is a web site that is configured to use Windows authentication in ISS and in the web configuration file we have Windows authentication with no impersonation. In addition, there is a web service that is configured in the same way. In such configuration HttpContext.User and Thread.CurrentPrincipal contain domain\user_name and WindowsIdentity is machine\aspnet. in such case when the web application tries to access the web service machine\aspnet user is used and such user doesn&#8217;t have rights to access the web service.</p>
<p>How to solve this?</p>
<p>The easiest way I have in mind is to impersonate the user from HttpContext.User. It is shown in the snippet below. This way the web service is called with right credentials.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> CustomWebService.ServiceName service = <span style="color: #0000ff">new</span> CustomWebService.ServiceName();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> sws.Url = <span style="color: #006080">&quot;&lt;url&gt;&quot;</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> sws.UseDefaultCredentials = <span style="color: #0000ff">true</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> CustomWebService.RetrievedData data; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span> <span style="color: #0000ff">using</span> (WindowsImpersonationContext context = WindowsIdentity.Impersonate((HttpContext.Current.User.Identity <span style="color: #0000ff">as</span> WindowsIdentity).Token))</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>     data = service.GetSomeData();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span> }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span> // use data retrieved from the service</pre>
</p></div>
</div>
<p>Note that I use using statement in order to guarantee that after the scope I need impersonation it will be undone. i do not call directly Undo() method of WindowsImpersonationContext but it is called internally by Dispose() method.</p>
<p><a href="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/12/impersonation-context-dispose-method.jpg" ><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="68" alt="impersonation_context_dispose_method" src="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/12/impersonation-context-dispose-method-thumb.jpg" width="244" border="0" /></a></p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/473621204" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/12/03/access-an-web-service-from-web-application-with-different-default-credentials/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/12/03/access-an-web-service-from-web-application-with-different-default-credentials/</feedburner:origLink></item>
		<item>
		<title>My new blog at blog.windowsclient.net</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/472479822/</link>
		<comments>http://devblog.antongochev.net/2008/12/02/my-new-blog-at-blogwindowsclientnet/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 13:46:42 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/?p=147</guid>
		<description><![CDATA[Since today I have a new blog at http://blogs.windowsclient.net/tbbtuk/default.aspx. I&#8217;d like to improve myself in the area of WPF and I&#8217;ll post all connected with WinForms and WPF there. I&#8217;ll also continue to blog here in my personal blog.
The idea to have an additional blog especially for WPF and WinForms came to me in the [...]]]></description>
			<content:encoded><![CDATA[<p>Since today I have a new blog at <a href="http://blogs.windowsclient.net/tbbtuk/default.aspx" onclick="javascript:urchinTracker ('/outbound/article/blogs.windowsclient.net');">http://blogs.windowsclient.net/tbbtuk/default.aspx</a>. I&#8217;d like to improve myself in the area of WPF and I&#8217;ll post all connected with WinForms and WPF there. I&#8217;ll also continue to blog here in my personal blog.<br />
The idea to have an additional blog especially for WPF and WinForms came to me in the last week. It is based on my intention to specialize in WPF and I&#8217;ve decided that it will be a good idea to have a separated blog space for that.</p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/472479822" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/12/02/my-new-blog-at-blogwindowsclientnet/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/12/02/my-new-blog-at-blogwindowsclientnet/</feedburner:origLink></item>
		<item>
		<title>Execute unit tests against DB and keep it unchanged using AOP</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/471196767/</link>
		<comments>http://devblog.antongochev.net/2008/12/01/execute-unit-tests-against-db-and-keep-it-unchanged-using-aop/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 11:09:09 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/?p=145</guid>
		<description><![CDATA[Few weeks ago, I wrote about how to extend Nunit to support RollBack attribute and how to execute unit tests against database and keep it unchanged.
Today, I&#8217;ve tried to achieve the same functionality but using AOP. Actually, I&#8217;ll use PostSharp platform in order to weave needed code in each test method decoreted with RollBackAttribute. PostSharp [...]]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago, I wrote about <a href="http://devblog.antongochev.net/2008/10/30/how-to-extend-nunit-to-support-transaction-rollback/" target="_self" >how to extend Nunit to support RollBack attribute</a> and <a href="http://devblog.antongochev.net/2008/11/03/execute-unit-tests-against-db-and-keep-it-unchanged/" target="_self" >how to execute unit tests against database and keep it unchanged</a>.</p>
<p>Today, I&#8217;ve tried to achieve the same functionality but using <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_self" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">AOP</a>. Actually, I&#8217;ll use <a href="http://www.postsharp.org/about/" target="_self" onclick="javascript:urchinTracker ('/outbound/article/www.postsharp.org');">PostSharp platform</a> in order to weave needed code in each test method decoreted with RollBackAttribute. PostSharp does it at compile time and do nothing in the run-time. Many people like to have an option to change the aspect during run-time but in my case I do not need it and PostSharp is ideal for doing what I need.</p>
<p>Let me start with RollBackAttribute class. It inherited from OnMethodBoundaryAspect and override OnEntry and OnExit methods. It begins a new transaction per each call and it rollbacks it after execution.</p>
<pre class="syntax-highlight:csharp">
[Serializable]
public class RollBackAttribute : OnMethodBoundaryAspect
{
	ITransaction transation = null;

	public override void OnEntry(MethodExecutionEventArgs eventArgs)
	{
		transation = SessionManager.CurrentSession.BeginTransaction();
	}

	public override void OnExit(MethodExecutionEventArgs eventArgs)
	{
		if (transation != null)
		{
			transation.Rollback();
			transation.Dispose();
		}
	}
}
</pre>
<p>Now we are ready to decorate any test method with RollBack attribute.</p>
<pre class="syntax-highlight:csharp">
[Test]
[RollBack]
public void AddNewModule()
{
	Module module = CreateModule();

	ModuleRepository repository = new ModuleRepository(SessionManager.CurrentSession);
	repository.Add(module);

	Module result = RetrieveModelFromatabase(module, SessionManager.CurrentSession);

	Assert.IsNotNull(result);
}
</pre>
<p>But how it will look after compilation? Below is a screen shot from .Net Reflector.</p>
<p><a href='http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/12/aop_rollback.jpg'><img src="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/12/aop_rollback-300x180.jpg" alt="" title="aop_rollback" width="300" height="180" class="alignnone size-medium wp-image-146" /></a></p>
<p>In red is the code we have in AddNewModule test method and in light blue is the execution of OnEntry and OnExit methods of OnMethodBoundaryAspect. As you can see the code we needed is weaved into the method during compile time and will be executed.</p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/471196767" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/12/01/execute-unit-tests-against-db-and-keep-it-unchanged-using-aop/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/12/01/execute-unit-tests-against-db-and-keep-it-unchanged-using-aop/</feedburner:origLink></item>
		<item>
		<title>My attempt to use Composite Web Application Block with ASP.Net MVC Application</title>
		<link>http://feeds.feedburner.com/~r/antongochev/feed/~3/459847347/</link>
		<comments>http://devblog.antongochev.net/2008/11/20/my-attempt-to-use-composite-web-application-block-with-aspnet-mvc-application/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 18:20:13 +0000</pubDate>
		<dc:creator>ganton</dc:creator>
		
		<category><![CDATA[ASP.Net]]></category>

		<category><![CDATA[ASP.Net MVC]]></category>

		<guid isPermaLink="false">http://devblog.antongochev.net/?p=138</guid>
		<description><![CDATA[Let&#8217;s say that there is an application written in ASP.Net MVC and that there is a  need to make it composite. I&#8217;ve searched the Interenet and I didn&#8217;t find so much about how to do it. For sure using Web Client Software Factory (WCSF) it can be done but it is not suitable for my [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say that there is an application written in ASP.Net MVC and that there is a  need to make it composite. I&#8217;ve searched the Interenet and I didn&#8217;t find so much about how to do it. For sure using <a href="http://www.codeplex.com/websf" target="_blank" onclick="javascript:urchinTracker ('/outbound/article/www.codeplex.com');">Web Client Software Factory (WCSF)</a> it can be done but it is not suitable for my case. And then i found <a href="http://www.pnpguidance.net/post/ASPNETMVCFrameworkCompositeWebApplicationBlockCWAB.aspx" target="_self" onclick="javascript:urchinTracker ('/outbound/article/www.pnpguidance.net');">an article by David Hayden</a>. This gave me an idea to try to do this my self and to see what will have on the end.</p>
<p>Here I&#8217;d like to present you the standard ASP.Net MVC application which is generated when you create an ASP.Net MVC application using Visual Studio. I&#8217;ve just reorganized it a bit in order to add into it Composite Web Application Block (CWAB). Let me say that this block is the core of the WCSF and it is created in order to cover its needs but using it as an infrastructure I think we are able to create an ASP.Net MVC application composite.</p>
<p>You can download the source code from <a href="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/compositemvcapplication.zip" target="_blank" >here</a>.</p>
<p>The first what I&#8217;ve did was to create a new MVC application and to add Microsoft.Practices.CompositeWeb.dll, Microsoft.Practices.CompositeWeb.EnterpriseLibrary.dll, Microsoft.Practices.ObjectBuilder.dll into its root folder (in a separate directory :)). I add three additional libraries into my solution Utils, Shell and HelpModule. It is depicted at the picture below.</p>
<p><a href="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/mvc_app_structure.jpg" ><img class="alignnone size-medium wp-image-139" title="mvc_app_structure" src="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/mvc_app_structure-108x300.jpg" alt="" width="108" height="300" /></a></p>
<p>The Utils library is not used as a <a href="http://www.codeplex.com/websf/Wiki/View.aspx?title=ModularityQuickStart&amp;referringTitle=ModularityDocumentation" target="_blank" onclick="javascript:urchinTracker ('/outbound/article/www.codeplex.com');">business or foundational module</a> but it contains class used in my application to register my own information about modules. ModuleMenuInfo class is an info class which contains module information that I use in web application.</p>
<pre class="syntax-highlight:csharp">

public class ModuleMenuInfo
{
	public string Key { get; set; }
	public string Title { get; set; }
	public string Controller { get; set; }
	public string Action { get; set; }
}
</pre>
<p>The second class is ModuleMenuManager which manages module information. It allows adding, removing a module and retrieving all available modules.</p>
<pre class="syntax-highlight:csharp">

public class ModuleMenuManager : IModuleMenuManager
{
	private IDictionary&lt;string, ModuleMenuInfo&gt; _modules;

	public ModuleMenuManager()
	{
		_modules = new Dictionary&lt;string, ModuleMenuInfo&gt;();
	}

	#region IModuleManager Members

	public void AddModule(ModuleMenuInfo moduleInfo)
	{
		// code ...
	}

	public void RemoveModule(ModuleMenuInfo moduleInfo)
	{
		code ...
	}

	public IEnumerable&lt;ModuleMenuInfo&gt; GetModules()
	{
		return _modules.Values;
	}

	#endregion
}
</pre>
<p>Then I&#8217;ve created a module named Shell which is a required module for the application at whole. All additional modules depend on it. Normally, it should contain registration of any global services and other configuration operations. In my case I use it only for registering ModuleMenuManager class as a global service in order to use it in other modules for registering their selves. in addition, It registers itself to ModuleMenuManager because I need to display initial information in the shell. In a standard WCSF application it is a functional module and it doesn&#8217;t have any connection with the web site. In my case it is the same but it just adds some information used by presentation layer.</p>
<pre class="syntax-highlight:csharp">

public class ShellModuleInitializer : ModuleInitializer
{
	private IModuleMenuManager _moduleMenuManager;

	public override void Load(CompositionContainer container)
	{
		base.Load(container);

		AddGlobalServices(container.Parent.Services);
		RegisterSiteMapInformation();
	}

	protected virtual void AddGlobalServices(IServiceCollection serviceCollection)
	{
		_moduleMenuManager = new ModuleMenuManager();
		serviceCollection.Add&lt;IModuleMenuManager&gt;(_moduleMenuManager);
	}

	protected virtual void RegisterSiteMapInformation()
	{
		_moduleMenuManager.AddModule(new ModuleMenuInfo()
		{
			Key = &quot;Shell&quot;,
			Title = &quot;Home&quot;,
			Controller = &quot;Shell&quot;,
			Action = &quot;Index&quot;
		});
	}
}
</pre>
<p>As you can see ShellModuleInitializer inherits from ModuleInitializer class. It is a class from CWAB and each module should implement a class which inherits from him in order to register its services, types or whatever it needs to do at the time of module initialization.</p>
<p>The second module that I&#8217;ve created is a business module named HelpModule. It doesn&#8217;t contain any special functionality but a simple class named HelpLogic which only returns a method that returns a title name.</p>
<pre class="syntax-highlight:csharp">

public class HelpLogic
{
	public string GetTitle()
	{
		return &quot;About Page&quot;;
	}
}
</pre>
<p>It also has its own HelpModuleInitializer class where it retrieves an instance of IModuleMenuManager and use it in order to register its self.</p>
<pre class="syntax-highlight:csharp">

public override void Load(CompositionContainer container)
{
	base.Load(container);
	RegisterSiteMapInformation(container.Parent.Services.Get&lt;IModuleMenuManager&gt;(true));
}
</pre>
<p>Finally, modules of my composite MVC application are ready for use and I can change the standard MVC application in the way that it will behave as a composite application. First of all I went to global.asax.cs file and change it as it is shown below.</p>
<pre class="syntax-highlight:csharp">

public class MvcApplication : WebClientApplication
{
	public static void RegisterRoutes(RouteCollection routes)
	{
	routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;);

	routes.MapRoute(
		&quot;Default&quot;,                                              // Route name
		&quot;{controller}/{action}/{id}&quot;,                           // URL with parameters
		new { controller = &quot;Shell&quot;, action = &quot;Index&quot;, id = &quot;&quot; }  // Parameter defaults
	);

	}

	protected override void Start()
	{
		base.Start();
		RegisterRoutes(RouteTable.Routes);
		ControllerBuilder.Current.SetControllerFactory(new CompositionControlerFactory());
		base.AddRequiredServices();
	}
}
</pre>
<p>I&#8217;ve change it to inherit from WebClientApplication file which is a class from CWAB which inherits from HttpApplication and extends it. Further, I delete Application_Start method and override Start method of WebClientApplication. It is executed only once when the application is started. There I registered the routes and I set my custom controller factory. AddRequiredServices of WebClientApplication registers all missed services used of CWAB.</p>
<p>CompositionControlerFactory class I need for executing WebClientApplication.BuildItemWithCurrentContext in order to build up the controller class requested and it will use ObjectBuilder to inject all dependencies a controller needs.</p>
<pre class="syntax-highlight:csharp">

public class CompositionControlerFactory : DefaultControllerFactory
{
	protected override IController GetControllerInstance(Type controllerType)
	{
		if (controllerType != null)
		{
			object controller = Activator.CreateInstance(controllerType);
			WebClientApplication.BuildItemWithCurrentContext(controller);
			return controller as IController;
		}
		return null;
	}
}
</pre>
<p>Further, I needed a BaseController class for getting all registered modules information and put it into ViewData. You can note that it has a property which holds an instance of IModuleMenuManager and that this property is decorated with ServiceDependency attribute from CWAB. Re-writing GetControllerInstance of DefaultControllerFactory I call WebClientApplication.BuildItemWithCurrentContext which will build up and inject needed class. But here I use ServiceDependency which tells CWAB that I need a service. Here I&#8217;ll get into ModuleMenuManager property a singleton of target class because I registered it as a global service and because I use ServiceDependency attribute. If I needed an unique instance for my controller I would use CreateNew attribute instead of ServiceDependency attribute.</p>
<pre class="syntax-highlight:csharp">

[HandleError]
public class BaseController : Controller
{
	[ServiceDependency]
	public IModuleMenuManager ModuleMenuManager { get; set; }

	protected void PresetMenuInfo()
	{
		foreach (var item in ModuleMenuManager.GetModules())
		{
			ViewData[item.Key + &quot;_module&quot;] = item;
		}
	}
}
</pre>
<p>I also created a HelpController class which is connected with HelpModule business module. Note that here I use CreateNew attribute in order to tell CWAB to build up for me a new instance of HelpLogic class each time I create HelpController.</p>
<pre class="syntax-highlight:csharp">

public class HelpController : BaseController
{
	[CreateNew]
	public HelpLogic HelpLogic { get; set; }

	public ActionResult About()
	{
		base.PresetMenuInfo();

		ViewData[&quot;Title&quot;] = HelpLogic.GetTitle();

		return View();
	}
}
</pre>
<p>Finally, I changed a bit the master page of the application in order to analyze the information from ViewData and according to it to add and remove menus.</p>
<pre class="syntax-highlight:html">
&lt;ul id=&quot;menu&quot;&gt;
&lt;%
if (ViewData[&quot;Shell_module&quot;] != null)
{
	Utils.ModuleMenuInfo shellModule = (ViewData[&quot;Shell_module&quot;] as Utils.ModuleMenuInfo);
%&gt;
&lt;li&gt;&lt;%= Html.ActionLink(shellModule.Title, shellModule.Action, shellModule.Controller)%&gt;&lt;/li&gt;
&lt;%
}
if (ViewData[&quot;Help_module&quot;] != null)
{
	Utils.ModuleMenuInfo helpModule = (ViewData[&quot;Help_module&quot;] as Utils.ModuleMenuInfo);
%&gt;
&lt;li&gt;&lt;%= Html.ActionLink(helpModule.Title, helpModule.Action, helpModule.Controller)%&gt;&lt;/li&gt;
&lt;%
}
%&gt;
&lt;/ul&gt;
</pre>
<p>Oh, I almost forgot to share that I also change Web.config file of the application where I&#8217;ve added information that CWAB uses to register my Shel foundational module.</p>
<pre class="syntax-highlight:xml">

&lt;compositeWeb&gt;
	&lt;modules&gt;
		&lt;module name=&quot;Shell&quot; assemblyName=&quot;Shell&quot; virtualPath=&quot;~/&quot;/&gt;
	&lt;/modules&gt;
&lt;/compositeWeb&gt;
</pre>
<p>I&#8217;ve also changed the Web.config file which is placed into Views folder. There I&#8217;ve added information about HelpModule business module. Note that it has a tag dependencies where one can list all modules the current depends on.</p>
<pre class="syntax-highlight:xml">

&lt;compositeWeb&gt;
	&lt;modules&gt;
		&lt;module name=&quot;HelpModule&quot; assemblyName=&quot;HelpModule&quot; virtualPath=&quot;~/Views/Help&quot;&gt;
			&lt;dependencies&gt;
				&lt;dependency module=&quot;Shell&quot; /&gt;
			&lt;/dependencies&gt;
		&lt;/module&gt;
	&lt;/modules&gt;
&lt;/compositeWeb&gt;
</pre>
<p>I hope that you note virtualPath attribute in both XML snippets. It is not used in any case in this realization but it is used by CWAB and without it it will not work properly.</p>
<p>And now let me show two screens one when HelpModule module is registered via Web.config and one when it doesn&#8217;t registered. Below is the first snapshot where you can see two menus home and about.</p>
<p><a href="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/composite_mvc_app_both_menus.jpg" ><img class="alignnone size-medium wp-image-141" title="composite_mvc_app_both_menus" src="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/composite_mvc_app_both_menus-300x61.jpg" alt="" width="300" height="61" /></a></p>
<p>And here is the second snapshot with only one menu attached because HelpModule module is not registered in the configuration file.</p>
<p><a href="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/composite_mvc_app_one_menu.jpg" ><img class="alignnone size-medium wp-image-142" title="composite_mvc_app_one_menu" src="http://devblog.antongochev.net/wordpress/wp-content/uploads/2008/11/composite_mvc_app_one_menu-300x62.jpg" alt="" width="300" height="62" /></a></p>
<img src="http://feeds.feedburner.com/~r/antongochev/feed/~4/459847347" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://devblog.antongochev.net/2008/11/20/my-attempt-to-use-composite-web-application-block-with-aspnet-mvc-application/feed/</wfw:commentRss>
		<feedburner:origLink>http://devblog.antongochev.net/2008/11/20/my-attempt-to-use-composite-web-application-block-with-aspnet-mvc-application/</feedburner:origLink></item>
	</channel>
</rss>
