tag:blogger.com,1999:blog-72443917474836053182024-02-19T16:43:32.191+02:00Rostyslav's blogFor C# programmers and others...Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7244391747483605318.post-51853303076525560942014-02-10T17:38:00.000+02:002014-02-11T09:45:29.432+02:00ASP.NET Session and concurrent access<h3>
<span style="color: #0b5394;">Problem</span></h3>
In one of my projects I found some <span style="font-family: "Times New Roman","serif"; font-size: 12pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">a strange at first sight issue</span> related to concurrent Session usages. During one long request the other parallel requests were waiting until the previous one is finished.<br />
This issue occurs when user tries to download file from ashx-handler. Handler requires Session to get some user-related configuration which is stored there.<br />
<br />
<h3>
<span style="color: #0b5394;">Overview</span></h3>
I've tried to dig deeper and that what I've found.<br />
By default no concurrent access to the asp.net session state is allowed. Requests with same SessionID will be locked exclusively to prevent potential corruption of its state.<br />
<br />
What does it mean:<br />
<br />
When you have request1 `in progress` and trying to do request2 - Session object will be locked by request1 and our code in request2 will be waiting for request1 completed.<br />
<br />
ASP.NET Session is thread safe and synchronization mechanism is based on System.Threading.ReaderWriterLock. <br />
<a name='more'></a><br />
This means that we can do many reads but only one writing at the same time.<br />
<em>ASP.NET Session object is configured for full (read\write) access, by default.</em><br />
<em></em><br />
That's why we need to configure Session object as 'read-only' on long-term pages to have non-blocking access to Session object from other pages.<br />
<br />
Be aware that even you don't use Session object explicitly you have this issue too.<br />
<br />
<h3>
<span style="color: #0b5394;">How to reproduce</span></h3>
To reproduce this issue let's create 2 asp.net pages.<br />
<br />
Default page: <br />
<pre class="brush: csharp"><%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello, SessionId " + Session.SessionID);
}
</script>
</pre>
<br />
Slow page (contains some long-term execution): <br />
<pre class="brush: csharp"><%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(10000);
Response.Write("Hello, SessionId " + Session.SessionID);
}
</script>
</pre>
<br />
web.config <br />
<pre class="brush: csharp"><configuration>
<system.web>
<sessionState mode="InProc"/>
</system.web>
</configuration></pre>
<div class="brush: csharp">
</div>
Try
to open Slow page and Default page at the same time.<br />
You may think that Default page will be opened immediately, but actually it will happen only after Slow page loading completed.<br />
<br />
<h3>
<span style="color: #0b5394;">Resolving</span></h3>
To resolve this issue we can re-configure ASP.NET Session object access.<br />
<br />
Session state is configured by using the sessionState element of the system.web configuration section.<br />
<br />
You can also configure session state by using the EnableSessionState value in the @ Page directive (<a href="http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.enablesessionstate(v=vs.100).aspx">more info</a>).<br />
<br />
<strong>If you need non-blocking access to read from Session:</strong><br />
<pre class="brush: csharp"><%@ Page Language="C#" EnableSessionState="ReadOnly"%>
</pre>
<br />
<strong>If you don't need Session:</strong><br />
<pre class="brush: csharp"><%@ Page Language="C#" EnableSessionState="False"%>
</pre>
Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com26tag:blogger.com,1999:blog-7244391747483605318.post-91031503810946317432013-01-04T15:44:00.000+02:002013-01-04T16:56:43.336+02:00Can developer do a Sushi? Yes :)Hello everybody, today I want to show you that developers can do more than only develop software.<br />
I'm going to show you step-by-step solution about how to do a sushi and what do we need for that.<br />
<br />
I'm junior sushi-developer and some of these cooking-steps are not perfect (as in original Japan cooking..).<br />
<br />
In this article I'm going to describe how to do some custom-sushi roll from that stuff which I found in my fridge. If you want to do such rolls like California, Philadelphia and other - the whole receipt will be the same but only with different ingredients.<br />
<br />
<h3>
<span style="color: #0b5394;">Content:</span></h3>
<ol>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#IngredientsOverview">Ingredients overview.</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#CookingRice">Cooking rice.</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#CookingTomago">Cooking Tomago (Japanese sweet egg omelet).</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#PrepareFilling">Prepare sushi filling.</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#MakeARoll">Make a sushi roll.</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#CutARoll">Cut a roll.</a></li>
<li><a href="http://ryaremchuk.blogspot.com/2013/01/sushi-development.html#Results">Results.</a></li>
</ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwFT65h6eDt8A9VtA85kNoysBdK5uy0u_qaYbwzMH4LKNPm65vkMv19QjJGLit5DN8gi4fKDSST2OpvjhAIyG4fdUwLWTJklF0f90UAOP42vt0FyZvB68nUAqrihX4SmWrWlV-MsVeSNnr/s1600/IMG_1983.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwFT65h6eDt8A9VtA85kNoysBdK5uy0u_qaYbwzMH4LKNPm65vkMv19QjJGLit5DN8gi4fKDSST2OpvjhAIyG4fdUwLWTJklF0f90UAOP42vt0FyZvB68nUAqrihX4SmWrWlV-MsVeSNnr/s400/IMG_1983.JPG" width="400" /></a></div>
<br />
<span style="color: red;"><b>*Be careful, a lot of photos...</b></span><br />
<br />
<a name='more'></a><br />
<h3>
<a href="http://www.blogger.com/blogger.g?blogID=7244391747483605318#IngredientsOverview" name="IngredientsOverview"></a>
<span style="color: #0b5394;">1. Ingredients overview.</span></h3>
<ul>
<li>500g Glutinous white rice (sushi rice)</li>
<li>400g Pickled salmon</li>
<li>150g Tobikko (flying fish caviar) </li>
<li>100g Sesame seeds</li>
<li>6 Nori sheets</li>
<li>1 Avocado</li>
<li>1 Philadelphia Cream Cheese (I used Almette cream cheese here)</li>
<li>2 Cucumber</li>
<li>1 Tomago</li>
<li>Sugar</li>
<li>Salt</li>
<li>Water</li>
<li>Rice vinegar</li>
</ul>
<div>
*Approximately rolls count = 11</div>
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="CookingRice"></a>
<span style="color: #0b5394;">2. Cooking rice.</span></h3>
1. Wash rice:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhV6e-bC_V0TPM3tnT6hFFUnW-RTfFq6-fQkt76DubqrAcKnd4Dw6EA2TuzcsjVjvifjLqa9Lrvb65H4JJ_8BIIXsnuLeZhMsttGE6SW_myRMt9vujCy1KIi_51nloI6Y6_BBSx89ZpBPlJ/s1600/IMG_1960.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhV6e-bC_V0TPM3tnT6hFFUnW-RTfFq6-fQkt76DubqrAcKnd4Dw6EA2TuzcsjVjvifjLqa9Lrvb65H4JJ_8BIIXsnuLeZhMsttGE6SW_myRMt9vujCy1KIi_51nloI6Y6_BBSx89ZpBPlJ/s200/IMG_1960.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLhE8TJkC-6P8LmymFLi19dsEy51-ayue4_xPaFyoXS5SoCELcm_1v0TYopcp1VHPaaHU09HCXIat76gFEffzy8-p4r7IsQ88V6dXdJJHKUbzUgrfYBOuRlxkWuNxfgpqffyAMg3SW5HLf/s1600/IMG_1961.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLhE8TJkC-6P8LmymFLi19dsEy51-ayue4_xPaFyoXS5SoCELcm_1v0TYopcp1VHPaaHU09HCXIat76gFEffzy8-p4r7IsQ88V6dXdJJHKUbzUgrfYBOuRlxkWuNxfgpqffyAMg3SW5HLf/s200/IMG_1961.JPG" width="200" /></a></div>
<br />
<br />
2.1 <strong>If you are using Saucepan</strong>:<br />
Put rice into the saucepan, add water (water amount = 1.2 * rice amount) in our situation we will use 600ml. Bring to a boil, then reduce the heat to low, cover and cook for 10 minutes. Rice should be tender and water should be absorbed. Do not take away the lid until the process is over!!!<br />
<br />
2.2 <strong>If you are using Steamer</strong>:<br />
Put rice into the steamer, add water (water amount = 1 * rice amount) in our situation we will use 500ml. Turn on the steamer and cook for 25 minutes (or better read the instruction for your steamer). Rice should be tender and water should be absorbed.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKf0IRm92vCW2SDSMgNqPC7JtgvMAEtwolBceKSdvO9fO7oQBtwf-LH_gxn3p9EDcAaLJPT5_vi6KR4CTUZdpnjwFqLG_VgHp7ek2WZboSaTl36yqK6saCeZifPcbWYNJRbvMKFy-MgWCm/s1600/IMG_1964.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKf0IRm92vCW2SDSMgNqPC7JtgvMAEtwolBceKSdvO9fO7oQBtwf-LH_gxn3p9EDcAaLJPT5_vi6KR4CTUZdpnjwFqLG_VgHp7ek2WZboSaTl36yqK6saCeZifPcbWYNJRbvMKFy-MgWCm/s200/IMG_1964.JPG" width="200" /></a></div>
<br />
<br />
3. Prepare vinegar-sauce<br />
Put the next ingredients into small saucepan:<br />
<ul>
<li>4 tablespoon rice vinegar</li>
<li>2 tablespoon sugar</li>
<li>2 teaspoon salt</li>
</ul>
Cook over medium heat until the sugar dissolves.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnRfMVISG7W49deBuB9xxFdemuVW0yJne410dnovzyBzpL4TqOn3u-7pCZ8M976BZciWRpSl7aVNOV6GYSmdP-ebE5rAuf32Z-4svk7E2rhFByoJfIkhuyGcWMHM9bQSh8yDqLA-CY7DYZ/s1600/IMG_1963.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnRfMVISG7W49deBuB9xxFdemuVW0yJne410dnovzyBzpL4TqOn3u-7pCZ8M976BZciWRpSl7aVNOV6GYSmdP-ebE5rAuf32Z-4svk7E2rhFByoJfIkhuyGcWMHM9bQSh8yDqLA-CY7DYZ/s200/IMG_1963.JPG" width="200" /></a></div>
<br />
<br />
4. Add vinegar-sauce to rice.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigvK-KN_NY9NwHH0yuX_WXCCTU5Movb-cVkP5YpozUm_GX0fAW_-2T71lhO1-JFHz5HXpXBSwRqJH-uWj3gHQ9p0lWEyfZYZlmdjwDuQS2X7dY7zJfwJ7gn_pTffFzXcbU9G7U5w_uJPFo/s1600/IMG_1965.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigvK-KN_NY9NwHH0yuX_WXCCTU5Movb-cVkP5YpozUm_GX0fAW_-2T71lhO1-JFHz5HXpXBSwRqJH-uWj3gHQ9p0lWEyfZYZlmdjwDuQS2X7dY7zJfwJ7gn_pTffFzXcbU9G7U5w_uJPFo/s200/IMG_1965.JPG" width="200" /></a></div>
<br />
<br />
Cool, then pour into the cooked rice and stir it. When you pour this into the rice it will seem very wet. Keep stirring and the rice will dry as it cools.<br />
Our rice for sushi is done.<br />
<br />
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="CookingTomago"></a>
<span style="color: #0b5394;">3. Cooking Tomago (Japanese sweet egg omelet).</span></h3>
Combine next ingredients in the plate:<br />
<ul>
<li>4 eggs</li>
<li>1 tablespoon sugar</li>
<li>1 tablespoon rice vinegar</li>
<li>0.5 teaspoon salt</li>
<li>1 tablespoon vegetable oil</li>
<li>1 tablespoon soy</li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMZAiD8NC_-ApZohkBi3CctGQDP8KPj39LY8ahi0CRQXfViPTM_2X_-mGTRy9YNpzDlfpR0LR0iqKwxP83JfUz0UfWWVy9IzJcKuih1WK2shfZacjP-SQZGmDQWqJiT78nqg_t5k770Ubq/s1600/IMG_1944.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMZAiD8NC_-ApZohkBi3CctGQDP8KPj39LY8ahi0CRQXfViPTM_2X_-mGTRy9YNpzDlfpR0LR0iqKwxP83JfUz0UfWWVy9IzJcKuih1WK2shfZacjP-SQZGmDQWqJiT78nqg_t5k770Ubq/s200/IMG_1944.JPG" width="200" /></a></div>
<br />
Stir it slightly we should avoid bubbles...<br />
As you can see on the next photos, there are a lot of big white areas (this is egg white) on the pan. This is because I'm too lazy to stir it a lot.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdsx3u61JM2J5sRGsZQ2fcQhFy660C69ASocy8PiJ7Ovb8uNZzhe5h_fhI6mTeOIep9RVx1cJ-e7LrctyxHQ_B1CR2wCl_NE5IHsfaFKAGs6OC62UsZqGyU62w_3JQlC_AFBgk3IKatJsc/s1600/IMG_1945.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdsx3u61JM2J5sRGsZQ2fcQhFy660C69ASocy8PiJ7Ovb8uNZzhe5h_fhI6mTeOIep9RVx1cJ-e7LrctyxHQ_B1CR2wCl_NE5IHsfaFKAGs6OC62UsZqGyU62w_3JQlC_AFBgk3IKatJsc/s200/IMG_1945.JPG" width="200" /></a></div>
<br />
<br />
Heat a frying pan at medium heat.<br />
Pour the ~1/4 part of egg mix into the pan. When the egg looks opaque and you can begin to roll it up. After you have rolled up this - pour another 1/4 part and do the same things. Repeat this until the whole egg mix is not used.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiogjr4pL6POHsKUeusHrmejGOghOI24ExqQFsZmahodGlaIsXrJ-Dp3L57OInaouB_Ymop7lmY65P5uq0XFXMsce9EL5rU6nW_pZkl9qXTY95-ryZD14Wv4rbkrqLTV5wFfzukuyNtLx9F/s1600/IMG_1947.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiogjr4pL6POHsKUeusHrmejGOghOI24ExqQFsZmahodGlaIsXrJ-Dp3L57OInaouB_Ymop7lmY65P5uq0XFXMsce9EL5rU6nW_pZkl9qXTY95-ryZD14Wv4rbkrqLTV5wFfzukuyNtLx9F/s200/IMG_1947.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTsLLWf9xed_D4xM6pGvsxRYxbQEH3D4z9xulB29faq11bKkiqIpXSevHaPtFKlcdlBHQHuDJ214ZrTv2_VXxP1t5mm5rJR_LEzFVHqfPxwokyvg4gIyBMZnLy_L3SsErhGuT7fb0x089o/s1600/IMG_1948.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTsLLWf9xed_D4xM6pGvsxRYxbQEH3D4z9xulB29faq11bKkiqIpXSevHaPtFKlcdlBHQHuDJ214ZrTv2_VXxP1t5mm5rJR_LEzFVHqfPxwokyvg4gIyBMZnLy_L3SsErhGuT7fb0x089o/s200/IMG_1948.JPG" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBcjZ-JhbHCn6WdCByIj5mOm71uLKC-V5qhs3-h4dj1bqtDwlmub9g7HkB-hqrHWuzWRGxs4gndwhIinP6rkBXylmIjLv8-8_eG8ZJ4_RIm7PQa7sCSEQ6h5pyxYuSMpSf87NMKvjSZwiL/s1600/IMG_1950.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBcjZ-JhbHCn6WdCByIj5mOm71uLKC-V5qhs3-h4dj1bqtDwlmub9g7HkB-hqrHWuzWRGxs4gndwhIinP6rkBXylmIjLv8-8_eG8ZJ4_RIm7PQa7sCSEQ6h5pyxYuSMpSf87NMKvjSZwiL/s200/IMG_1950.JPG" width="200" /></a></div>
<br />
<br />
Get few wipes and roll up our tomago into it. (Wipes will remove all useless oil from omelet) Also you may put something heavy on omlete to make a nice shape.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4VEcb2_8DKYX1LHoLsVgs3-ECmlFloDHT0UdSrwA0oAYZn0lvEdhmybmvmjaUekUb4UAL-mWhV87zarp5UluWy6kQbp522akH-U4XJZ1Hnaq6UNMifgF-Yx4PsoTm2C80D0LRltx95ri3/s1600/IMG_1952.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4VEcb2_8DKYX1LHoLsVgs3-ECmlFloDHT0UdSrwA0oAYZn0lvEdhmybmvmjaUekUb4UAL-mWhV87zarp5UluWy6kQbp522akH-U4XJZ1Hnaq6UNMifgF-Yx4PsoTm2C80D0LRltx95ri3/s200/IMG_1952.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1B7586pRZ4LFt0-8EGPx2tFSfrVW2-C7I7zy7Mc2Aa8bM-Hq6-J5BnhvkpYcmsOxQNvklvhCQeUgynDG5vZZt3QPMRf2bjQVZRDrQGX9AJwnNa2zGttT0JmW_zmO3cXd8p5ALtUvFa_s3/s1600/IMG_1954.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1B7586pRZ4LFt0-8EGPx2tFSfrVW2-C7I7zy7Mc2Aa8bM-Hq6-J5BnhvkpYcmsOxQNvklvhCQeUgynDG5vZZt3QPMRf2bjQVZRDrQGX9AJwnNa2zGttT0JmW_zmO3cXd8p5ALtUvFa_s3/s200/IMG_1954.JPG" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvD8RIco0BGQf6I04WvBxy7ErD3kUQzIaju8bKpJlpyv3tfTgY37R8izTAPsKlt61d81FRGD3GMu06k2WUzGf0rd1o4OyMHbOL8_t6X-o45deCe9O4iWoNPpJ0ccTm-FfuCcNbasmrFiwZ/s1600/IMG_1957.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="135" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvD8RIco0BGQf6I04WvBxy7ErD3kUQzIaju8bKpJlpyv3tfTgY37R8izTAPsKlt61d81FRGD3GMu06k2WUzGf0rd1o4OyMHbOL8_t6X-o45deCe9O4iWoNPpJ0ccTm-FfuCcNbasmrFiwZ/s200/IMG_1957.JPG" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="PrepareFilling"></a>
<span style="color: #0b5394;">4. Prepare sushi fillings.</span></h3>
Clean cucumbers and avocado. <br />
Cut all ingredients into small pieces like on the picture below:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCXagw1blEagBSG6l3Bj4mDiWk4dI3rEwi21lwlRJ4eRG4ygPp0mlYeQjutfDWi6XzhkDCZA2SFQC7BQK50-yF5RdkHpRiE5Sx88VqcwhR9vNvlZpogzzpvHIoNkeT0sOHpyF6q_dRUf8U/s1600/IMG_1955.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCXagw1blEagBSG6l3Bj4mDiWk4dI3rEwi21lwlRJ4eRG4ygPp0mlYeQjutfDWi6XzhkDCZA2SFQC7BQK50-yF5RdkHpRiE5Sx88VqcwhR9vNvlZpogzzpvHIoNkeT0sOHpyF6q_dRUf8U/s200/IMG_1955.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtecFkfOaUO5GZV87fZrW3z9x9VeEguW4cJyke5pMiF0i6MXNR5Rh2GM4yzhp2IhJUp_HVTZ0Hq9fbO9Cl-TWMKoAW3N3ENAYsHyKBQdJ2uAQWZWZbDD9cCdrekiPBD3GRs-MMrTxjzXMa/s1600/IMG_1959.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtecFkfOaUO5GZV87fZrW3z9x9VeEguW4cJyke5pMiF0i6MXNR5Rh2GM4yzhp2IhJUp_HVTZ0Hq9fbO9Cl-TWMKoAW3N3ENAYsHyKBQdJ2uAQWZWZbDD9cCdrekiPBD3GRs-MMrTxjzXMa/s200/IMG_1959.JPG" width="200" /></a></div>
<br />
<br />
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="MakeARoll"></a>
<span style="color: #0b5394;">5. Make a sushi roll.</span></h3>
While making a sushi <strong>your hands should be a bit wet</strong>, to avoid rice sticking. I have separate plate with clean water for that. <br />
<br />
<strong>Nori inside:</strong><br />
This sushi usually are bigger than 'rice inside'. You can put a lot of filling and don't worry about rolling.<br />
<br />
1. Take 0.5 of Nori sheet. <br />
2. Put rice on the rough side of Nori sheet like on the picture. Do not use a lot of rice. <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJSzsMd_0Mia6rr7K6IjRkSbnqYTSo-7ZvzeLWAx1JrHMuaomoGQTSpAWO6sV6exzx5lSgN4PuD6c93os82ojR-s8Be9ToGHAEHE45KiHT8QCxP4inx8qkXGuOVx0a47J64ICfZK6A5owW/s1600/IMG_1967.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJSzsMd_0Mia6rr7K6IjRkSbnqYTSo-7ZvzeLWAx1JrHMuaomoGQTSpAWO6sV6exzx5lSgN4PuD6c93os82ojR-s8Be9ToGHAEHE45KiHT8QCxP4inx8qkXGuOVx0a47J64ICfZK6A5owW/s200/IMG_1967.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeJt4lAqNTTeP0-wMAc1f8WYHHQhoWnBgXJoB4zagggjMazDlYZihg7rlL7RM8SPP1I12QQ-O0vsl_HmesaGKXDo09RhqBWETk8isK1C9yFEpgdAa-MDZ0oQ5pTRCGeKUoQhdnsYDIdsr9/s1600/IMG_1970.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeJt4lAqNTTeP0-wMAc1f8WYHHQhoWnBgXJoB4zagggjMazDlYZihg7rlL7RM8SPP1I12QQ-O0vsl_HmesaGKXDo09RhqBWETk8isK1C9yFEpgdAa-MDZ0oQ5pTRCGeKUoQhdnsYDIdsr9/s200/IMG_1970.JPG" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiF3n5UK53kwyt-J_R30DPnOWrTpeIZj-gpdjhZNaN8Bc5BWfzEyhoscA6vXw9gwiy0aPi4WwqKPXifVG9KxEHRG9WRlKk-rztgWGI-8neU8ZjVANJrmMNvAQIYTeTq1W3YQJkTcsrnh5aP/s1600/Nori+inside+scheme.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiF3n5UK53kwyt-J_R30DPnOWrTpeIZj-gpdjhZNaN8Bc5BWfzEyhoscA6vXw9gwiy0aPi4WwqKPXifVG9KxEHRG9WRlKk-rztgWGI-8neU8ZjVANJrmMNvAQIYTeTq1W3YQJkTcsrnh5aP/s1600/Nori+inside+scheme.PNG" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
3. Put fillings (cream chees, tomago, salmon, avocado, cucumber)<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBw1E_AP2ysJpLCykZ6sv1tA_2GxJmOuvoDaVDAanvE8cW6Q3iS9-q0wpu62WG0Af4-fRWpM3ZJid045tg99k8lb0hqq2G76K4qbxIE1dUySR-NMdaQaVUjhD-D8hTsExzRuXZ46j3AsUQ/s1600/IMG_1971.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBw1E_AP2ysJpLCykZ6sv1tA_2GxJmOuvoDaVDAanvE8cW6Q3iS9-q0wpu62WG0Af4-fRWpM3ZJid045tg99k8lb0hqq2G76K4qbxIE1dUySR-NMdaQaVUjhD-D8hTsExzRuXZ46j3AsUQ/s200/IMG_1971.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjO-xC9DJ9NvH9PE9am-oicqmdXT8KCkx98XqpbLpsiJZ5Jiui3AA-GMboewIyXmMqHvOECGcGQfqeJR1e0pqHwCPSjl2HzzwWL0kixG6BnqdG1GDiCOHY8TitiNTOtUcqxGcegloDjoVc/s1600/IMG_1972.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="128" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjO-xC9DJ9NvH9PE9am-oicqmdXT8KCkx98XqpbLpsiJZ5Jiui3AA-GMboewIyXmMqHvOECGcGQfqeJR1e0pqHwCPSjl2HzzwWL0kixG6BnqdG1GDiCOHY8TitiNTOtUcqxGcegloDjoVc/s200/IMG_1972.JPG" width="200" /></a></div>
<br />
4. Roll it.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj89VdkTG8Hlzk3s_tmJvbOiXEB3xjiMdX9ElohC-i0C3IUjP172DhyphenhyphentrWW1h3EykJAie8rDp0czYHB6nExZQdtd5dBH4MuLosQXuqGlC6p7HZHVDwcRs0CBGZ_uKgDjyaK2l7Rc47KYzC5/s1600/IMG_1973.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj89VdkTG8Hlzk3s_tmJvbOiXEB3xjiMdX9ElohC-i0C3IUjP172DhyphenhyphentrWW1h3EykJAie8rDp0czYHB6nExZQdtd5dBH4MuLosQXuqGlC6p7HZHVDwcRs0CBGZ_uKgDjyaK2l7Rc47KYzC5/s200/IMG_1973.JPG" width="200" /></a></div>
<br />
5. Cover the roll with Tobikko or Sesame seeds or thin parts of salmon/avocado<br />
6. You may grip you roll one more time.<br />
7. Your roll is Done!<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYcXhkYVi1Ix-89r2WVzSA5nC4hy3_K0rZp6lJms3QkPz1VUfzCN1ZKGJ-NNe1m49AQN5Luj9sSAi729L9boxyJi-IV-U7XQ9VsrmaNMzHujbB_3RoEL3DMEhRIYrrbUuvKJqpMU96ZKl/s1600/IMG_1974.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYcXhkYVi1Ix-89r2WVzSA5nC4hy3_K0rZp6lJms3QkPz1VUfzCN1ZKGJ-NNe1m49AQN5Luj9sSAi729L9boxyJi-IV-U7XQ9VsrmaNMzHujbB_3RoEL3DMEhRIYrrbUuvKJqpMU96ZKl/s200/IMG_1974.JPG" width="200" /></a></div>
<br />
<strong><br /></strong>
<strong>Rice inside:</strong><br />
This sushi usually are smaller than 'nori inside' and contains only one or two types of filling.<br />
<br />
1. Take 0.5 of Nori sheet. <br />
2. Put rice on the rough side of Nori sheet like on the picture. Do not use a lot of rice. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl2kKmyqWDAY2hylw9GjoekIKbf2MA0DSn8LohHHZ0wVcY6UsuCRZ387ezQz1w6iAff0aMkVmF8g-xX5jnI0LDErFfjiHvVqUOEUm8XcZK_7K0EfYssIpHyz1jqHz_Uaktp_1VuwDJABPG/s1600/IMG_1975.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl2kKmyqWDAY2hylw9GjoekIKbf2MA0DSn8LohHHZ0wVcY6UsuCRZ387ezQz1w6iAff0aMkVmF8g-xX5jnI0LDErFfjiHvVqUOEUm8XcZK_7K0EfYssIpHyz1jqHz_Uaktp_1VuwDJABPG/s200/IMG_1975.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDkCHDxc-riXmeM1MjjwedJFywALdVMOK-7eBEp4varVn3KjA_lgM5ZOaPKt5RlGu6hB6sn2r_NI68CgppKgcEIVOLmAKUzzZ9dttp2NTGm9I9dhAPqMdV1yOgdEWEzPhNFXTkoFXqlfD2/s1600/IMG_1976.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDkCHDxc-riXmeM1MjjwedJFywALdVMOK-7eBEp4varVn3KjA_lgM5ZOaPKt5RlGu6hB6sn2r_NI68CgppKgcEIVOLmAKUzzZ9dttp2NTGm9I9dhAPqMdV1yOgdEWEzPhNFXTkoFXqlfD2/s200/IMG_1976.JPG" width="200" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr0nXbjwvTkWZ1Fhtsc3_mFH8ggubi4CFVqz45PULi4jd_SC378gDDU9fG48qKeQ_JImR4IKwKOtVwcWrAOcoTKUI47AMhLhUaIrOn291ZELCpzeCcy6usDIsGI0rKv0NIabfZtw5ldvB0/s1600/Rise+inside+scheme.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr0nXbjwvTkWZ1Fhtsc3_mFH8ggubi4CFVqz45PULi4jd_SC378gDDU9fG48qKeQ_JImR4IKwKOtVwcWrAOcoTKUI47AMhLhUaIrOn291ZELCpzeCcy6usDIsGI0rKv0NIabfZtw5ldvB0/s1600/Rise+inside+scheme.PNG" /></a></div>
<br />
3. Put salmon<br />
4. Roll it.<br />
7. Your roll is Done!<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEig9HOn62jLsNsn_Dhwqd8KRKyY3VmTb05zOUVje5A2R491WR4BIbg2oMizJjYM1t09tTF8ed4N1coCN77Fs_wcPnXqMT4mMC1D2tK7UA1eO5TBBjllKMwaSRTQjt7HrPbhZJBgCXp8_Mod/s1600/IMG_1977.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEig9HOn62jLsNsn_Dhwqd8KRKyY3VmTb05zOUVje5A2R491WR4BIbg2oMizJjYM1t09tTF8ed4N1coCN77Fs_wcPnXqMT4mMC1D2tK7UA1eO5TBBjllKMwaSRTQjt7HrPbhZJBgCXp8_Mod/s200/IMG_1977.JPG" width="200" /></a></div>
<br />
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="CutARoll"></a>
<span style="color: #0b5394;">6. Cut a roll.</span></h3>
Cut all rolls to 8 pieces. <br />
Knife should be sharp and wet (to avoid rice sticking).<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjafv0jEbLgFALq8LgBAX9vWLIYG3oEquZQMhLLXA3THkOeHQsIqmPfSK7DDf8IerDyavna16WxIW5zdEJyR0ilmWReKo4t78xcyjteGMhH8mxipRX0xpEZXdAIKW3FgIGqHTCrmjXjuuAQ/s1600/IMG_1979.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjafv0jEbLgFALq8LgBAX9vWLIYG3oEquZQMhLLXA3THkOeHQsIqmPfSK7DDf8IerDyavna16WxIW5zdEJyR0ilmWReKo4t78xcyjteGMhH8mxipRX0xpEZXdAIKW3FgIGqHTCrmjXjuuAQ/s200/IMG_1979.JPG" width="200" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNH7XHI-zSaNOayFeMfAleyB1EZ9SDxuVLFw3Nv_uBBCyLJRJZ4ZDoetzLX2ELJwdoTls9aSzkQOSyHzKHqJW47fxXD5uJzgwcfb6EW6v6PbjsrIDUHT0DxwmiQ0Cv1eBhwmOZdLMTXhyphenhyphenS/s1600/IMG_1981.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNH7XHI-zSaNOayFeMfAleyB1EZ9SDxuVLFw3Nv_uBBCyLJRJZ4ZDoetzLX2ELJwdoTls9aSzkQOSyHzKHqJW47fxXD5uJzgwcfb6EW6v6PbjsrIDUHT0DxwmiQ0Cv1eBhwmOZdLMTXhyphenhyphenS/s200/IMG_1981.JPG" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgks_Rgj2DXsvkxsDpe0hLhVcU3Rqr4rMko1tnAMNZXQ12euh1DdzuAVUYSvKpb0Su6IP0kw2syGXkxdbddifGEczgDvwHSQqJEPqAUp5bFdiAQRb4Jn1cMFZJL3FdmbOycl6qW1cXmbpEz/s1600/IMG_1984.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgks_Rgj2DXsvkxsDpe0hLhVcU3Rqr4rMko1tnAMNZXQ12euh1DdzuAVUYSvKpb0Su6IP0kw2syGXkxdbddifGEczgDvwHSQqJEPqAUp5bFdiAQRb4Jn1cMFZJL3FdmbOycl6qW1cXmbpEz/s320/IMG_1984.JPG" width="240" /></a></div>
<span style="font-size: x-small;">*This is not all result sushi. Before doing this photo I've already ate few rolls. :)</span><br />
<span style="font-size: x-small;"><br /></span>
<br />
<h3>
<a href="http://ryaremchuk.blogspot.com/" name="Results"></a>
<span style="color: #0b5394;">7. Results.</span></h3>
Here are all the sushi I've done from all these ingredients.<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjtf5TQmK2O59QP9nCvGNEQd6RdBivqLsx1RpoN9Gkq8dui6WI8kDrkEuuoliZepcd69f2amdnGDgm7FumfdTNqFamqDlTa0akrBMEKn7lY4SNWW5vjv_CqYHi6VH4l5NoSpKmhVx1rV3I/s1600/IMG_1982.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjtf5TQmK2O59QP9nCvGNEQd6RdBivqLsx1RpoN9Gkq8dui6WI8kDrkEuuoliZepcd69f2amdnGDgm7FumfdTNqFamqDlTa0akrBMEKn7lY4SNWW5vjv_CqYHi6VH4l5NoSpKmhVx1rV3I/s400/IMG_1982.JPG" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwFT65h6eDt8A9VtA85kNoysBdK5uy0u_qaYbwzMH4LKNPm65vkMv19QjJGLit5DN8gi4fKDSST2OpvjhAIyG4fdUwLWTJklF0f90UAOP42vt0FyZvB68nUAqrihX4SmWrWlV-MsVeSNnr/s1600/IMG_1983.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwFT65h6eDt8A9VtA85kNoysBdK5uy0u_qaYbwzMH4LKNPm65vkMv19QjJGLit5DN8gi4fKDSST2OpvjhAIyG4fdUwLWTJklF0f90UAOP42vt0FyZvB68nUAqrihX4SmWrWlV-MsVeSNnr/s400/IMG_1983.JPG" width="400" /></a></div>
<br />
<br />
<br />
Do not afraid to experiment and feel free with different illings.<br />
<br />
<h3>
Bon appetite.</h3>
<br />Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com1tag:blogger.com,1999:blog-7244391747483605318.post-84736224430613212022012-12-15T22:33:00.000+02:002012-12-17T12:36:46.952+02:00.NET: Create EventSource in Windows Event Log (with Admin privileges on Windows7)<span style="color: #0b5394; font-family: inherit; font-size: large;"><b>Problem</b></span><br />
<span style="font-family: inherit;">We had one project which logs exception and some system information to Windows Event log. We had to write all logs in appropriate EventSource (created only for our application in Application log). As you may know creating EventSource requires Administrative Privileges on Windows Vista and higher. For deployment we use ClickOnce which does not have such a functionality (i.e. to create event source).</span><br />
<span style="font-family: inherit;"></span><br />
In this article I want to describe solutions that we found, I think maybe most of them (but not all) are obviously for you, but I hope this article will be useful .<br />
<br />
<span style="color: #0b5394; font-family: inherit; font-size: large;"><b>Resolving</b></span><br />
<span style="font-family: inherit;">This is what msdn says about EventSource creation (</span><a href="http://msdn.microsoft.com/en-us/library/2awhba7a.aspx"><span style="font-family: inherit;">a full article</span></a><span style="font-family: inherit;">):</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">To create an event source in Windows Vista and later or Windows Server 2003, you must have administrative privileges. </span></blockquote>
<blockquote class="tr_bq">
<span style="font-family: inherit;">The reason for this requirement is that all event logs, including security, must be searched to determine whether the event source is unique. Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.</span></blockquote>
<blockquote class="tr_bq">
<span style="font-family: inherit;">In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses the security log, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.</span></blockquote>
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">We don't want to force users to run our application manually by administrator. And we don't want always to run our application with administrative privileges (automatically) to be sure that we can create EventSource whenever we need it.</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">You may ask: why always to run with admin privileges?</span><br />
<span style="font-family: inherit;">Because it is a standard Windows Vista security mechanism. On application startup Windows will analyze manifest and show 'run as administrator' UAC dialog to a user. </span><br />
<span style="font-family: inherit;">For more information about manifest file, run application with administrator privileges and others - you can </span><a href="http://msdn.microsoft.com/en-us/library/bb756929.aspx"><span style="font-family: inherit;">read here</span></a><span style="font-family: inherit;">.</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">I started to investigate this problem and found few solutions:</span><br />
<span style="font-family: inherit;"></span><br />
<a name='more'></a><br />
<span style="color: #0b5394; font-family: inherit;"><b>Solution 1</b></span><br />
<span style="font-family: inherit;">Add EventSource directly into registry. </span><br />
<span style="font-family: inherit;">We may create file with next data and run it from our application (with admin privileges):</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">Windows Registry Editor Version 5.00</span><br />
<span style="font-family: inherit;">[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\<Your application name>]</span><br />
<span style="font-family: inherit;">"EventMessageFile"=hex(2):43,00,3a,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,\</span><br />
<span style="font-family: inherit;"> 00,73,00,5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,74,00,2e,00,\</span><br />
<span style="font-family: inherit;"> 4e,00,45,00,54,00,5c,00,46,00,72,00,61,00,6d,00,65,00,77,00,6f,00,72,00,6b,\</span><br />
<span style="font-family: inherit;"> 00,5c,00,76,00,34,00,2e,00,30,00,2e,00,33,00,30,00,33,00,31,00,39,00,5c,00,\</span><br />
<span style="font-family: inherit;"> 45,00,76,00,65,00,6e,00,74,00,4c,00,6f,00,67,00,4d,00,65,00,73,00,73,00,61,\</span><br />
<span style="font-family: inherit;"> 00,67,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,00,00</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">This script will create a key in the registry with EventMessageFile value for our required EventSource.</span><br />
<span style="font-family: inherit;">EventMessageFile stores the location of the file that contains event identifiers for the events generated by the source program, and a text describing each event (</span><a href="http://technet.microsoft.com/en-us/library/cc781323(v=ws.10).aspx"><span style="font-family: inherit;">more info</span></a><span style="font-family: inherit;">). </span><br />
<span style="font-family: inherit;">In this script I used default for value .NET 4.0 application*: </span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll</span></blockquote>
<span style="font-family: inherit; font-size: x-small;">*This value is the same which .NET framework 4.0 will set to created event source after using EventLog.CreateEventSource method.</span><br />
<span style="font-family: inherit;"></span><br />
After running this script user will see a window like that:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbjl1gMEuGWrzk-Uxl-16-DUnC48LKdyXBK2in1zY02Jfuk-JbpVHTIbpFeUmUmUWObeqhc2jlT44vizlRILhgOON6PVgX13yj1JyT_HQS1e65cREoiJIck-FDpHa-okGdjbFQ9hboo5hN/s1600/Screenshot+-+12_11_2012+,+11_11_38+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="123" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbjl1gMEuGWrzk-Uxl-16-DUnC48LKdyXBK2in1zY02Jfuk-JbpVHTIbpFeUmUmUWObeqhc2jlT44vizlRILhgOON6PVgX13yj1JyT_HQS1e65cREoiJIck-FDpHa-okGdjbFQ9hboo5hN/s320/Screenshot+-+12_11_2012+,+11_11_38+PM.png" width="320" /></span></a></div>
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">As for me this is not a perfect solution, because </span><span style="font-family: inherit;">as you may see this window is not credible and user-friendly. As a user I don't want to click 'Yes' on it and this is a problem.</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">BTW: You can remove needed EventSource only removing appropriate key from the registry.</span><br />
<span style="font-family: inherit;"></span><br />
<span style="color: #0b5394; font-family: inherit;"><b>Solution 2</b></span><br />
<span style="font-family: inherit;">Write an own tool which we will run in a separate process with administrative privileges when we need to create Event source.</span><br />
<span style="font-family: inherit;">Example code:</span><br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
using System.Diagnostics;
namespace EventSourceCreator
{
class Program
{
static void Main(string[] args)
{
const string sourceName = "EventSourceTest"; //write your event source name here
const string targetLogName = "Application";
EventLog.CreateEventSource(sourceName, targetLogName);
}
}
}
]]></script><span style="font-family: inherit;">
</span><br />
<span style="font-family: inherit;">After running this tool with admin privileges user will see this window:</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvxBEIN7X4tu59VH2jWI6SocE-6safSmv5bEZhOO5ZAuGpDbElLvBT5Y70aKZE4C5x8KP43kmuvt0hl9YXhwGzPQn7hr2A2MuEeweENxYNfzrnvPlzb1en2EqryEwhd2mY9592br5BrLVO/s1600/EventSourceCreatorUAC.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvxBEIN7X4tu59VH2jWI6SocE-6safSmv5bEZhOO5ZAuGpDbElLvBT5Y70aKZE4C5x8KP43kmuvt0hl9YXhwGzPQn7hr2A2MuEeweENxYNfzrnvPlzb1en2EqryEwhd2mY9592br5BrLVO/s320/EventSourceCreatorUAC.png" width="320" /></span></a></div>
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">We have here the same problem with not a credible window. </span><br />
<span style="font-family: inherit;">But, you may have your own certificate and sign this 'tool' to avoid this problem but in our situation we don't have it...</span><br />
<span style="font-family: inherit;"></span><br />
<b><span style="color: #0b5394; font-family: inherit;">Solution 3</span></b><br />
<span style="font-family: inherit;">Use a standard Windows tool <i>eventcreate.exe</i></span><br />
<span style="font-family: inherit;">Eventcreate.exe enables an administrator to create a custom event in a specified event log and create event log source if this does not exist. Read more info about this tool </span><a href="http://technet.microsoft.com/en-us/library/bb490899.aspx"><span style="font-family: inherit;">here</span></a><span style="font-family: inherit;">.</span><br />
<span style="font-family: inherit;">We can run next command from our code:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">eventcreate /ID 1 /L Application /T INFORMATION /SO "EventSourceTest" /D "EventSourceTest source created."</span></blockquote>
<span style="font-family: inherit;">This program is distributed with Windows XP and higher, and signed with Microsoft certificate; so, in such a way a screen with request for administrator privileges looks more pretty.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqwFNsqvrukWaLkVN_n6w2wGSZhMnGPixkApeQRep84ZqOOydyhLhKR5TDV69PjzadLY1cuq9JNxdyuhvyUfR-hoFw5NzAMNUUVcmofKFj6E6kuMjTibm5ARQwpvSJj7-eOoU6uoI8MBA4/s1600/eventcreateUAC.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqwFNsqvrukWaLkVN_n6w2wGSZhMnGPixkApeQRep84ZqOOydyhLhKR5TDV69PjzadLY1cuq9JNxdyuhvyUfR-hoFw5NzAMNUUVcmofKFj6E6kuMjTibm5ARQwpvSJj7-eOoU6uoI8MBA4/s320/eventcreateUAC.png" width="320" /></span></a></div>
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;"></span><br />
<b><span style="color: #0b5394; font-family: inherit; font-size: large;">What we choose</span></b><br />
<span style="font-family: inherit;">1. In our code we check if needed EventSource is already created. </span><br />
<span style="font-family: inherit;">2. If not then try to create one with current user privileges (not to disturb a user, maybe we already have appropriate permissions).</span><br />
<span style="font-family: inherit;">3. If this operation fails, create EventSource with <i>eventcreate.exe</i> running with administrative privileges.</span><br />
<span style="font-family: inherit;"></span><br />
<span style="color: #0b5394; font-family: inherit; font-size: large;"><b>Complete solution</b></span><br />
<span style="font-family: inherit;">Instead of a summary I want to show you our completed solution. If you have the same situation, you may use this code and be happy. </span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;"><script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public class EventSourceCreator
{
public static void CreateSourceIfNotExists(string sourceName, string targetLogName)
{
try
{
if (!EventLog.SourceExists(sourceName))
{
EventLog.CreateEventSource(sourceName, targetLogName);
EventLog.WriteEntry(sourceName, "Event source created");
}
}
catch(SecurityException)
{
CreateEventSourceWithEventCreateTool(sourceName, targetLogName);
}
}
private static void CreateEventSourceWithEventCreateTool(string sourceName, string targetLogName)
{
try
{
var eventLogCreateToolName = @"eventcreate";
var eventLogCreateToolArguments =
string.Format(
" /ID 1 /L {0} /T INFORMATION /SO \"{1}\" /D \"{1} source created.\"",
targetLogName,
sourceName);
var proc = new Process
{
StartInfo =
{
FileName = eventLogCreateToolName,
Arguments = eventLogCreateToolArguments,
Verb = "runas"
}
};
proc.Start();
proc.WaitForExit();
}
catch (Win32Exception)
{
//note: handle if user clicks 'No' on run as administrator UAC dialog
return;
}
}
}
]]></script></span><br />Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com6tag:blogger.com,1999:blog-7244391747483605318.post-60115770695712879602012-11-23T13:47:00.000+02:002012-11-23T13:47:31.525+02:00Encrypting and Decrypting Web.config Sections in .NET 4.0<span style="font-family: inherit;"><b><span style="color: #0b5394; font-size: large;">Problem</span></b> </span><br />
<span style="font-family: inherit;">Sometimes we need to store a lot of confidential data in web.config in our production environment (for examples: username\password for impersonation or for connect to database, some appSettings, etc.). And it is not secure to store that as clear text, obviously some people on your server may have access to this file and steal your data.</span><br />
<span style="font-family: inherit;">.NET Framework gives us a good solution. We can encrypt configuration sections in web.config files.</span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="color: #0b5394; font-family: inherit; font-size: large;">How to Encrypt a section</span></b><br />
<span style="font-family: inherit;">1. Find aspnet_regiis.exe on your PC.</span><br />
<span style="font-family: inherit;">2. Grand access to ApplicationPool Identity for NetFrameworkConfigurationKey RSA key contanier:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">aspnet_regiis -pa "NetFrameworkConfigurationKey" "<ApplicationPool Identity user>"</span></blockquote>
<span style="font-family: inherit;">3. Encrypt a section:</span><br />
<div>
<blockquote class="tr_bq">
<span style="font-family: inherit;">aspnet_regiis -pe
"<Path/to/section>" -app "/<YouWebApplication>"</span></blockquote>
<span style="font-family: inherit;"><b>*All these commands require administrative privileges</b>, so if you want to use command prompt for it - don't forget to 'run as administrator'. Otherwise you will get a lot of very strange errors.</span></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-hansi-theme-font: major-latin;">It looks very simple... </span><br />
<span style="font-family: inherit; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-hansi-theme-font: major-latin;">But let's consider all these steps in more detail.</span><br />
<span style="font-family: inherit;">
</span><br />
<a name='more'></a><b style="color: #0b5394;"><span style="font-family: inherit; font-size: large;">1. Find aspnet_regiis.exe on your PC</span></b><br />
<span style="font-family: inherit;">aspnet_regiis.exe is standard .NET Framework tool which allows us to configure our ASP.NET application. <a href="http://msdn.microsoft.com/en-us/library/k6h9cz8h(v=vs.80).aspx">Read more about aspnet_regiis on msdn</a></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">It is located here (for .NET 4.0):</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe</span></blockquote>
<span style="font-family: inherit;"> <span style="color: #0b5394; font-size: large;"><b>2. Access to NetFrameworkConfigurationKey </b></span></span><br />
<span style="font-family: inherit;">First of all I want to tell you about what NetFrameworkConfigurationKey is.</span><br />
<span style="font-family: inherit;">
<span style="mso-ansi-language: EN-US;">NetFrameworkConfigurationKey
is the name of the RSA key container that to be used when encrypting and decrypting
configuration section data. The 'NetFrameworkConfigurationKey' RSA
key container with public-private key pair is created when the .Net Framework is installed. Each machine will
have a different RSA keys. Therefore you cannot encrypt on one machine and decrypt on
another, because RSA keys will be different.<o:p></o:p></span></span><br />
<span style="font-family: inherit;">
</span><br />
<span style="font-family: inherit;">ApplicationPool Identity user should have access to this key container because IIS should decrypt web.config on the fly to get needed values.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">To grand access to this key container to a user you can run next command:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;"> aspnet_regiis -pa "NetFrameworkConfigurationKey" "<userName>"</span></blockquote>
<span style="font-family: inherit; font-size: x-small;">This key container is defined in machine.config (in .NET Config folder).</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-size: x-small;"></span>If you want to create your own key container (with RSA keys) and not to use standard one, <a href="http://msdn.microsoft.com/en-us/library/yxw286t2(v=vs.100).aspx">read this article</a>.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Example:</span><br />
<span style="font-family: inherit;">To access NetFrameworkConfigurationKey we need to grand Network Service user, because this is our application pool identity.</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pa "NetFrameworkConfigurationKey" "Network Service"</span></blockquote>
<span style="font-family: inherit;">Result:</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYKTxR0YlUzXBh9SQZRZ_GNQEvKG6TVQ1IwJNaCEI7f_XJeq7uYax4DIHYacBxlz84KMVXtzVpH7MZm74A6UW-9ChIx5l7ucZAIxi3B0lV1XDlVD_JjjMplSppLyGMeoHUjZKm8bkUQosq/s1600/pa+Network+Service.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="162" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYKTxR0YlUzXBh9SQZRZ_GNQEvKG6TVQ1IwJNaCEI7f_XJeq7uYax4DIHYacBxlz84KMVXtzVpH7MZm74A6UW-9ChIx5l7ucZAIxi3B0lV1XDlVD_JjjMplSppLyGMeoHUjZKm8bkUQosq/s320/pa+Network+Service.PNG" width="320" /></span></a></div>
<br />
<div>
<span style="font-size: x-small;"><span style="color: #0b5394; font-family: inherit; font-size: large;"><b>3. Encrypt web.config section</b></span></span><br />
<span style="font-size: x-small;"><span style="font-family: inherit; font-size: small;">One of the best things in section encryption is that we don't need to write any code to support it. We can do all that stuff only by command line. IIS will automatically decrypt on the fly (in memory) sections when it is necessary. </span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Command to encrypt section:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">aspnet_regiis -pe "<Path/to/section>" -app "/<YouWebApplication>"</span></blockquote>
<span style="font-family: inherit;">Path to section is a relative path (from root configuration element in web.config) to target section. We should skip root configuration element in this path. For example a path to identity section in web.config will look like "<span lang="EN-US" style="line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: major-latin;">system.web/identity</span>"</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Example:</span><br />
<span style="font-family: inherit;"><span style="font-size: x-small;"><span style="font-size: small;">We have TestWebsite virtual directory on our IIS web server. </span></span><span style="font-size: x-small;"><span style="font-size: small;">Here is web.config:</span></span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj38LQDN5Tjs8VFiet1VNEWt5wue-GYVq0377RLqVR_bUtWbtTsTibiPwJn033S0xgfG6bNC2najTZkHRxv12d8Fbhx16Y-EsbojOTHbHED3qiPuGLCpcLT9blQsXVbfOjA75rPlfE_qt8n/s1600/web+config.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="94" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj38LQDN5Tjs8VFiet1VNEWt5wue-GYVq0377RLqVR_bUtWbtTsTibiPwJn033S0xgfG6bNC2najTZkHRxv12d8Fbhx16Y-EsbojOTHbHED3qiPuGLCpcLT9blQsXVbfOjA75rPlfE_qt8n/s320/web+config.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Now we encrypt appSetting section:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pe "appSettings" -app "/TestWebsite"</span></blockquote>
<span style="font-family: inherit;">Result:</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixooc4f6XdsNHBLvNB-4ShWpiDcG682vocYnu3XgcfyvFUpq5BW6Bv-7_M3RSVB8f3GppEVShjTdu-dN_iZGgwL_K5V3cOD-WPPZ-FoNwDCEBX0JsPvQOvO5PfDi19wp8GKO8rxoRkGlP1/s1600/pe+app+settings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="162" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixooc4f6XdsNHBLvNB-4ShWpiDcG682vocYnu3XgcfyvFUpq5BW6Bv-7_M3RSVB8f3GppEVShjTdu-dN_iZGgwL_K5V3cOD-WPPZ-FoNwDCEBX0JsPvQOvO5PfDi19wp8GKO8rxoRkGlP1/s320/pe+app+settings.PNG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAvVQ13LlRxOmC5T9uYJwW5L5DlOk7e_ngGT5Dlzm2Az9gk8NXjfkwhi81wza-YRQSTBuftLxAD-HY7YKQOv3p9rZjgzvzyQfdBjBa05kGdIk6cskll-1W4LDyt3lGtktLpU5JHbFXG-VD/s1600/web+config+encrypted+appSettings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAvVQ13LlRxOmC5T9uYJwW5L5DlOk7e_ngGT5Dlzm2Az9gk8NXjfkwhi81wza-YRQSTBuftLxAD-HY7YKQOv3p9rZjgzvzyQfdBjBa05kGdIk6cskll-1W4LDyt3lGtktLpU5JHbFXG-VD/s320/web+config+encrypted+appSettings.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">And connectionStrings section:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pe "connectionStrings" -app "/TestWebsite"</span></blockquote>
<span style="font-family: inherit;">Result:</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimKBT6KlPZQzJ09bDcTGgHaFxecFGiZu5L4GsTCkFnKFsF8tB9nDZT14511-o2ssINkQQ4Uatojbe1g9jf3RvBYrP5erYGqyVCKqL7W50SEVUuuTUNxcrG9uYfaqnJE8NyMAPTAdyQ7xFw/s1600/pe+connectionStrings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="162" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimKBT6KlPZQzJ09bDcTGgHaFxecFGiZu5L4GsTCkFnKFsF8tB9nDZT14511-o2ssINkQQ4Uatojbe1g9jf3RvBYrP5erYGqyVCKqL7W50SEVUuuTUNxcrG9uYfaqnJE8NyMAPTAdyQ7xFw/s320/pe+connectionStrings.PNG" width="320" /></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVPARfDdugDgUeCpgok49ByV1gnnp-cWKZ5eleKAAoVP1ynRQ3Oye_LQAXFctXkc611x5A-4GgKgp9xDl72fpyfEnlh1G57TbSgttiyw_wXsWnSRyWT_HUppAyUNPEmGfBdJukqtudhMFS/s1600/encrypted+appSettings+%252B+connectionStrings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVPARfDdugDgUeCpgok49ByV1gnnp-cWKZ5eleKAAoVP1ynRQ3Oye_LQAXFctXkc611x5A-4GgKgp9xDl72fpyfEnlh1G57TbSgttiyw_wXsWnSRyWT_HUppAyUNPEmGfBdJukqtudhMFS/s320/encrypted+appSettings+%252B+connectionStrings.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<div>
</div>
<div>
<b><span style="color: #0b5394; font-family: inherit; font-size: large;">Decrypt web.config section</span></b></div>
<div>
<span style="font-family: inherit;">If you need to change manually some values in already encrypted sections you may decrypt section, change values and after that encrypt them one more time.</span></div>
<div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Command to decrypt section:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">aspnet_regiis -pd "<Path/to/section>" -app "/<YouWebApplication>"</span></blockquote>
<span style="font-family: inherit;">Example:</span></div>
<div>
<span style="font-family: inherit;">We need to decrypt appSettings section to change some values:</span><br />
<blockquote class="tr_bq">
<span style="font-family: inherit;">c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pd "appSettings" -app "/TestWebsite"</span></blockquote>
<span style="font-family: inherit;">Result:</span></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcA05yH2gOQdpwnxu425jHYjEpp5ea9J4tvTYix9yu1s-vJF-UVdvuR1nnKLA5BDb302c9BUGDTOZhQZRaAZTMej0WDZ8DpMljeoSbnb_BVcnSgboKXyWxaB64mYlr_UQBxfFImoJw3OF9/s1600/pd+app+settings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="162" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcA05yH2gOQdpwnxu425jHYjEpp5ea9J4tvTYix9yu1s-vJF-UVdvuR1nnKLA5BDb302c9BUGDTOZhQZRaAZTMej0WDZ8DpMljeoSbnb_BVcnSgboKXyWxaB64mYlr_UQBxfFImoJw3OF9/s320/pd+app+settings.PNG" width="320" /></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0pCs7niSCqVCvqApOc4YmCiyFKr63jnD17bHpUktKCRPztZulNzP7_CPS8XvL73QKRp6xlgO4wkp_11MW4V-YRyz0Csk3_A3xUOg8-3NeNltNmN-gCtKUAFtXtppBFagI3g6YJoOAPwe0/s1600/decrypted+appSettings.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="219" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0pCs7niSCqVCvqApOc4YmCiyFKr63jnD17bHpUktKCRPztZulNzP7_CPS8XvL73QKRp6xlgO4wkp_11MW4V-YRyz0Csk3_A3xUOg8-3NeNltNmN-gCtKUAFtXtppBFagI3g6YJoOAPwe0/s320/decrypted+appSettings.PNG" width="320" /></span></a></div>
</div>
<div>
</div>
<div>
<b><span style="color: #0b5394; font-family: inherit; font-size: large;">Summary</span></b></div>
<div>
<span style="font-family: inherit;">I hope this article will help you to make your web sites more secure. As you see it is very easy and doesn't require any additional code. Don't be afraid about IIS, it will automatically decrypt any section on the fly when it is needed. </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">And I want to summarize all information one more time in one simple example.</span><br />
<span style="font-family: inherit;">If our ApplicationPoolIdenity is Network Service, our website is named TestWebsite and we need to encrypt s<span style="line-height: 18px;">system.web/identity</span> section we should do next steps:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">1. Run cmd as administrator.</span><br />
<span style="font-family: inherit;">2. Go to c:\Windows\Microsoft.NET\Framework\v4.0.30319 folder</span><br />
<span style="font-family: inherit;">3. aspnet_regiis.exe -pa "NetFrameworkConfigurationKey" "Network Service"</span><br />
<span style="font-family: inherit;">4. aspnet_regiis.exe -pe "<span style="line-height: 18px;">system.web/identity</span>" -app "/TestWebsite"</span><br />
<br /></div>
Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com13tag:blogger.com,1999:blog-7244391747483605318.post-76990180984981223842012-11-01T12:36:00.000+02:002012-11-15T23:25:53.194+02:00Settings in .NET are easyAs I <a href="http://ryaremchuk.blogspot.com/2012/10/xml-serialization.html">said</a> 'few' days ago, today I`ll tell you about Settings files.<br />
Settings files are designed to make our life simpler during working with application and user configuration.<br />
<br />
<strong><span style="color: #0b5394; font-size: large;">A bit theory</span></strong><br />
<br />
Settings file consists of few parts in our project.<br />
<ol>
<li>Settings file (auto generated class derived from <span style="color: #2b91af; font-family: Consolas;"><span style="color: #2b91af; font-family: Consolas;"><span style="color: #2b91af; font-family: Consolas;">ApplicationSettingsBase</span></span></span>).</li>
<li>app.config (contains default values for all options).</li>
</ol>
Generated setting file derived from ApplcationSettingsBase already contains initialized static instances of all our options. We don't need to create this class manually and load any data, because it has already been created and loaded. And it is ready to work.<br />
<br />
All settings have next properties:<br />
<ul>
<li><strong>Name (</strong>this is the name of our setting): with this name we will have automatically generated property in Settings class.</li>
<li><strong>Type</strong> (the type of our setting): there are a lot of predefined types like string, int, etc., but you can also set your custom type.</li>
<li><strong>Value</strong> (this is default value for setting).</li>
<li><strong>Scope</strong>: all settings should be in one of two scopes (User or Application). It represents how our setting will be accessed at runtime.</li>
</ul>
<a name='more'></a><br />
User Scope - options in this scope belong to a user, we can read\write these options in runtime. Values for each property will be stored in user.config file in Local AppData folder for a current user.<br />
<br />
Application Scope - options in this scope belong to application and we can only read these options. Values are always read from app.config.<br />
<br />
<span style="color: #0b5394; font-size: large;"><strong>Getting started with Settings</strong></span><br />
<br />
How to add settings files to your project:<br />
<span style="font-size: x-small;">By default every WindowsForms project already contains an empty Settings file. </span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGEfVN_3kPKSme6KXm3h7aRxK-QY-b-rIAAPEpunR_H1ULpgHJi8entW7FmQHNSBqTo74BBbWM3YP5sHKa1tBT4g4fK4QY4jlGSIQ_CYPt4OEpB_n5amck3bV-vaZWSyyLJDF9rrow9PI7/s1600/Screenshot+-+10_31_2012+,+10_42_50+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGEfVN_3kPKSme6KXm3h7aRxK-QY-b-rIAAPEpunR_H1ULpgHJi8entW7FmQHNSBqTo74BBbWM3YP5sHKa1tBT4g4fK4QY4jlGSIQ_CYPt4OEpB_n5amck3bV-vaZWSyyLJDF9rrow9PI7/s400/Screenshot+-+10_31_2012+,+10_42_50+PM.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeSjZxMyBxxE5EVdmPmws2dM1H7fiq-LUw9fQkhV0xqtl4esGyQJjIWMT2a_0jQV6_SmDCmfk2a0mtBhuWdSzhT7YMquiaX8BeFgKvqNgvwTEaxeQjOkyp1gD13aS0AuR8OWVGkYBKuDnx/s1600/Screenshot+-+10_31_2012+,+10_49_26+PM_ver002.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="263" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeSjZxMyBxxE5EVdmPmws2dM1H7fiq-LUw9fQkhV0xqtl4esGyQJjIWMT2a_0jQV6_SmDCmfk2a0mtBhuWdSzhT7YMquiaX8BeFgKvqNgvwTEaxeQjOkyp1gD13aS0AuR8OWVGkYBKuDnx/s400/Screenshot+-+10_31_2012+,+10_49_26+PM_ver002.png" width="400" /></a></div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihF3pDXoQ8vzZ1-lgwhpmhS6EUUm1gSN-y698qpO_5Mw2mdXI2A86V1cFsXNAwiS4yUM5aWWrQ9gQJd8Yqm94de8dwAJQELyGLFDjM0rPu_6KybNmsxM9zClFv_053HZ29MinSKJHNm6J-/s1600/Screenshot+-+10_31_2012+,+10_49_26+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a><span id="goog_542446884"></span><span id="goog_542446885"></span><br />
<br />
Settings file has a very nice UI designer in Visual Studio. Designer allows us to add\edit\remove any options. After editing options appropriate settings class will be automatically regenerated with new changes. But I prefer clicking <strong>Synchronize </strong>button for sure that settings class is really regenerated.<br />
<br />
This is how designer looks like (I've already added few options):<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCb6jIuGO5j7VQRO4jVAOIBISQt8zxsj_3LcQJn6U7v7PRmFd0ffwlkiQKpddeuavxBKheuqoMD3PD5iSR1chmUConedlxcLqpOWtc1tjgJAVZUGalYoZB_jn9AFUU2ZeUOI5APtZJ9vFJ/s1600/Screenshot+-+10_31_2012+,+10_59_57+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="96" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCb6jIuGO5j7VQRO4jVAOIBISQt8zxsj_3LcQJn6U7v7PRmFd0ffwlkiQKpddeuavxBKheuqoMD3PD5iSR1chmUConedlxcLqpOWtc1tjgJAVZUGalYoZB_jn9AFUU2ZeUOI5APtZJ9vFJ/s400/Screenshot+-+10_31_2012+,+10_59_57+PM.png" width="400" /></a></div>
This is how generated settings class looks like:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4fZcoquH3N14S-RIEBEa_E8tGhvy31wn5C-7Qa-n0WYZEVpKJ1pSDcGIO1eVvqFVuGSG_arf5ApZcW3nYqAx0rxWiVSuMpT1P_n2wFSKeeXmmjV6ENMESKRd2nma1hlb2PDuqLfZXslcd/s1600/Screenshot+-+10_31_2012+,+11_00_49+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4fZcoquH3N14S-RIEBEa_E8tGhvy31wn5C-7Qa-n0WYZEVpKJ1pSDcGIO1eVvqFVuGSG_arf5ApZcW3nYqAx0rxWiVSuMpT1P_n2wFSKeeXmmjV6ENMESKRd2nma1hlb2PDuqLfZXslcd/s400/Screenshot+-+10_31_2012+,+11_00_49+PM.png" width="400" /></a></div>
Default values in app.config:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAh94WG9TlagfRXNIoZCU_Eo0lqWglAriUR3TvfkAIMa6OimIF67d8F0fzUTjJNuRIgBjKJUatr8f2vM2L7z_Y4gV7ZAZSrwGjIy3OPLw1XMwTJBaMCUqAKHBDSvFd5E08qPF1D-5nKJzd/s1600/Screenshot+-+10_31_2012+,+11_01_28+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAh94WG9TlagfRXNIoZCU_Eo0lqWglAriUR3TvfkAIMa6OimIF67d8F0fzUTjJNuRIgBjKJUatr8f2vM2L7z_Y4gV7ZAZSrwGjIy3OPLw1XMwTJBaMCUqAKHBDSvFd5E08qPF1D-5nKJzd/s400/Screenshot+-+10_31_2012+,+11_01_28+PM.png" width="400" /></a></div>
As you can see we have 3 options: UserName, UserPassword and PathToLib.<br />
PathToLib is in Application scope and is generated like read-only property in settings class.<br />
<br />
How we can use settings in code:<br />
<pre class="brush: csharp">public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(
"User name: {0}, password: {1}"
, CustomSettings.Default.UserName
, CustomSettings.Default.UserPassword);
//output: User name: defualtUser, password: 123
Console.WriteLine(
"Path to application library: {0}"
, CustomSettings.Default.PathToLib);
//output: Path to application library: D:\Lib
}
}
</pre>
<br />
<span style="color: #0b5394; font-size: large;"><strong>Managing options in code</strong></span><br />
There are few useful methods in settings class which allow us to load default\save changes only by one line of code:<br />
<ul>
<li>Save() - stores the current values of the application settings properties.</li>
<li>Reload() - refreshes the application settings property values from persistent storage.</li>
<li>Reset() - restore the application settings values to their corresponding default properties.</li>
</ul>
<br />
See the example of all these method usages:<br />
<pre class="brush: csharp">public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("User: {0}" , CustomSettings.Default.UserName);
//output: User: defualtUser
CustomSettings.Default.UserName = "newUser";
CustomSettings.Default.Save();
Console.WriteLine("User: {0}" , CustomSettings.Default.UserName);
//output: User: newUser
CustomSettings.Default.UserName = "badUser";
CustomSettings.Default.Reload();
Console.WriteLine("User: {0}", CustomSettings.Default.UserName);
//output: User: newUser
CustomSettings.Default.UserName = "badUser";
CustomSettings.Default.Reset();
Console.WriteLine("User: {0}", CustomSettings.Default.UserName);
//output: User: defaultUser
}
}
</pre>
<br />
When we change settings values and call Save() method, after some 'magic', .NET will create user.config file with new values. <br />
In my situation I find this file in:<br />
c:\Users\<current user>\AppData\Local\TestConsoleApplication\TestConsoleApplication.ex_Url_0pb1j4w12xaui1md5tuq0dev0d2olihj\1.0.0.0\user.config <br />
<span style="font-size: x-small;">*but in this file there are values that are only in user-scope. All application-scope values will get from app.config.</span><br />
<div>
</div>
<strong><span style="color: #0b5394; font-size: large;">Manual app.config editing</span></strong><br />
<span style="color: black; font-size: small;">As you already know that user-scope values are stored in user.config file (in user AppData folder) and aplication-scope values are stored in app.config.</span><br />
<span style="color: black;">If you will manually change default values in app.config (for example by ConfigurationManager class in code or by notepad) after we will get setting value we will get next results depends on setting scope:</span><br />
<ul><span style="color: black;">
</span>
<li><span style="color: black;">If settings is in user-scope then in code we don't get our new edited values (from app.config) because we already have values in user.config. But after we call Reset() method we will get new defaults from app.config.</span></li>
<span style="color: black;">
</span>
<li><span style="color: black;">If settings is in application-scope we will get new values from app.conig.</span></li>
<span style="color: black;">
</span></ul>
<span style="color: black;">The main idea of loading user-scope settigns is - if we have value in user.config we get it otherwise we get default value from app.config.</span><br />
<br />
<span style="color: #0b5394; font-size: large;"><strong>Summary</strong></span><br />
<span style="color: black;">We can use Settings file not only in Windows Forms. Settings file is one of the easiest ways to manage application and user options. Visual Studio has a very powerful UI designer that decreases development time to minimum during options management.</span><br />
<span style="color: black;">As for me, I use Settings files always when I need to use options in my application. It allows me to write less code and this is very good, because less code is always good :)</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCb6jIuGO5j7VQRO4jVAOIBISQt8zxsj_3LcQJn6U7v7PRmFd0ffwlkiQKpddeuavxBKheuqoMD3PD5iSR1chmUConedlxcLqpOWtc1tjgJAVZUGalYoZB_jn9AFUU2ZeUOI5APtZJ9vFJ/s1600/Screenshot+-+10_31_2012+,+10_59_57+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a> </div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4fZcoquH3N14S-RIEBEa_E8tGhvy31wn5C-7Qa-n0WYZEVpKJ1pSDcGIO1eVvqFVuGSG_arf5ApZcW3nYqAx0rxWiVSuMpT1P_n2wFSKeeXmmjV6ENMESKRd2nma1hlb2PDuqLfZXslcd/s1600/Screenshot+-+10_31_2012+,+11_00_49+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a> </div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAh94WG9TlagfRXNIoZCU_Eo0lqWglAriUR3TvfkAIMa6OimIF67d8F0fzUTjJNuRIgBjKJUatr8f2vM2L7z_Y4gV7ZAZSrwGjIy3OPLw1XMwTJBaMCUqAKHBDSvFd5E08qPF1D-5nKJzd/s1600/Screenshot+-+10_31_2012+,+11_01_28+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a> </div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeSjZxMyBxxE5EVdmPmws2dM1H7fiq-LUw9fQkhV0xqtl4esGyQJjIWMT2a_0jQV6_SmDCmfk2a0mtBhuWdSzhT7YMquiaX8BeFgKvqNgvwTEaxeQjOkyp1gD13aS0AuR8OWVGkYBKuDnx/s1600/Screenshot+-+10_31_2012+,+10_49_26+PM_ver002.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a> </div>
<br />Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com0tag:blogger.com,1999:blog-7244391747483605318.post-87256314867403680562012-10-22T23:50:00.000+03:002012-12-04T19:00:17.714+02:00XML Serialization<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Problem<o:p></o:p></strong></span></span></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">A long time ago I had to
implement some data storage between restartings of application in one of my
projects. I started looking for the best solution to do that and one of them using
.NET Settings files (about that I`ll write in the future), another – to use
serialization (I`ve chosen xml serialization because it is more human readable and flexible).<o:p></o:p></span></span></div>
<span style="font-family: Calibri;"><span lang="UK" style="font-size: 10pt; line-height: 115%;">Serialization
is the process of converting an object into a stream of bytes in order to store
</span><span style="font-size: 10pt; line-height: 115%; mso-ansi-language: EN-US;">or
</span><span lang="UK" style="font-size: 10pt; line-height: 115%;">transmit </span><span style="font-size: 10pt; line-height: 115%; mso-ansi-language: EN-US;">it.</span></span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-size: 10pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;"><span style="font-family: Calibri;">Deserialization is the opposite
process to serialization – converting data to object.<o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">The most used serialization types in .NET Framework:</span></span></div>
<ul>
<li>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Binary
serialization (is the serialization process with binary data as a result)<o:p></o:p></span></span></li>
<li><span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Xml serialization (is the serialization process with XML data as a result)<o:p></o:p></span></span></li>
</ul>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">XML serialization
approach is “cross platform”. We can serialize data in one application (for
example java-based) and deserialize it in .NET app.<o:p></o:p></span></span><br />
<br />
<span style="mso-ansi-language: EN-US;">
<span style="font-family: Calibri;">
Want to know more about XML Serialization? Welcome...</span></span><br />
<a name='more'></a><br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"></span></span></span></span><br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Which class supports xml
serialization?<o:p></o:p></strong></span></span></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">XML Serialization uses
Reflection to get data from class to generate XML.<o:p></o:p></span></span></div>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Therefore there are
next requirements for class that can be serialized to xml:</span></span><br />
<ul>
<li><div class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Class should contain public default constructor (without parameters). <span style="mso-spacerun: yes;"> </span></span></span></div>
</li>
<li><div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Only Public properties\fields are to be serialized.</span></span></div>
</li>
<li><div class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;">
<span style="font-family: Calibri;">If public property is read-only then it value cannot be restored.<o:p></o:p></span></div>
</li>
</ul>
</div>
<span style="font-family: Calibri;"><span style="font-size: 10pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">In Binary serialization we make a
‘dump’ of appropriate object memory. At the time we can store all data along
with private properties and fields.</span><span lang="UK" style="font-size: 10pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></span><br />
<br />
Now I`ll show to you how XML Serialization is easy to use .<br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;"><em>Let’s take a look at our
test class.</em></span></span></div>
<pre class="brush: csharp"> public enum Role
{
RegularUser,
Administrator
}
</pre>
<pre class="brush: csharp"> public class User
{
public string Name { get; set; }
public string EMail { get; set; }
public string Password { get; set; }
public int PasswordExpiresIn { get; set; }
public int Age { get; set; }
public Role UserRole { get; set; }
}
</pre>
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"></span></span></span></span><br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Generic serialization and deserialization<o:p></o:p></strong></span></span></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: Calibri;"><span style="mso-ansi-language: EN-US;">Detailed information
about XMLSerializer can be found here in msdn: </span><span lang="UK"><a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx"><span lang="EN-US" style="mso-ansi-language: EN-US;"><span style="color: blue;">http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx</span></span></a></span><span class="MsoHyperlink"><span style="mso-ansi-language: EN-US;"><o:p></o:p></span></span></span></div>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">I prefer developing some
abstract serializer which just gets some object (which will be serialized) and
returns xml result. It is very flexible because we separate serialization and
data saving. Thus if we need to store serialized object in database it cannot
be a problem and we shouldn’t improve serialization, we should only save it.<o:p></o:p></span></span><br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public static class XmlSerializationService<T> where T : class
{
private static XmlSerializer _serializer = new XmlSerializer(typeof(T));
public static T Deserialize(string serializedObject)
{
using (var sr = new StringReader(serializedObject))
{
return (T)_serializer.Deserialize(sr);
}
}
public static string Serialize(T objectToSerialize)
{
using (var sw = new StringWriter())
{
_serializer.Serialize(sw, objectToSerialize);
return sw.ToString();
}
}
}
]]></script>
<span style="font-family: Calibri; mso-ansi-language: EN-US;"><em></em></span>
<span style="font-family: Calibri; mso-ansi-language: EN-US;"><em>Example:</em></span><br />
<pre class="brush: csharp">public class Program
{
public static void Main(string[] args)
{
var userToSave = new User
{
Name = "Rostyslav",
Age = 24,
Password = "123",
EMail = "sample@email.com",
UserRole = Role.Administrator,
};
var xml = XmlSerializationService<User>.Serialize(userToSave);
Console.WriteLine(xml);
}
}
</pre>
<span style="font-family: Calibri;">
</span>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: Calibri;"><span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;"><em>Result:</em></span></span></span><br />
<pre class="brush: xml"><User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Rostyslav</Name>
<Email>sample@email.com</Email>
<Password>123</Password>
<PasswordExpiresIn>0</PasswordExpiresIn>
<Age>24</Age>
<UserRole>Administrator</UserRole>
</User>
</pre>
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong></strong></span></span></span></span><br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Manage 'class members to xml'
transformation.<o:p></o:p></strong></span></span></span></span></div>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">If you want to manage
serialization process: <o:p></o:p></span></span><br />
<ul>
<li>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">set the field that should be stored as xml-attribute;</span></span></li>
<li><span style="mso-ansi-language: EN-US;"></span><span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">setup some another xml nodes name (instead of defaults based on members names);<o:p></o:p></span></span></li>
</ul>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">You can use the attributes
on your target .NET classes.<o:p></o:p></span></span><br />
<span style="font-family: Calibri;"><span style="mso-ansi-language: EN-US;">Read more about
Attributes that control XML Serialization here in msdn: </span><span lang="UK"><a href="http://msdn.microsoft.com/en-us/library/83y7df3e(v=vs.71).aspx"><span lang="EN-US" style="mso-ansi-language: EN-US;"><span style="color: blue;">http://msdn.microsoft.com/en-us/library/83y7df3e(v=vs.71).aspx</span></span></a></span><span style="mso-ansi-language: EN-US;"><o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Now we can store and
restore our user object, but we want to change all nodes in result xml to
attributes, rename EMail attribute to UserMail and do not serialize PasswordExpiresIn property because these data will
be received from some another source. All that we need to do is only to set appropriate attributes to our target class.</span></span><br />
<em></em><br />
<em>Example:</em></div>
<pre class="brush: csharp">public class User
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute(AttributeName = "UserMail")]
public string EMail { get; set; }
[XmlAttribute]
public string Password { get; set; }
[XmlIgnore]
public int PasswordExpiresIn { get; set; }
[XmlAttribute]
public int Age { get; set; }
[XmlAttribute]
public Role UserRole { get; set; }
}
</pre>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: Calibri;"><em>Result:</em></span></div>
<div style="margin: 10pt 0in 0pt;">
</div>
<pre class="brush: xml"><User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Age="24" Name="Rostyslav" Password="123" UserMail="sample@email.com" UserRole="Administrator">
</User>
</pre>
<br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Serialization of complex/derived
classes and arrays<o:p></o:p></strong></span></span></span></span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">If we derive classes
or some class that contains another one XmlSerializer will work fine. Also
<strong>XmlSerializer can serialize List<T></strong> and arrays without any magic.<o:p></o:p></span></span></div>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<i style="mso-bidi-font-style: normal;"><span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Example:</span></span></i><br />
<script class="brush: c-sharp" type="syntaxhighlighter">
<![CDATA[
var users = new List<user>
{
new User
{
Name = "Rostyslav",
Age = 24,
Password = "123",
EMail = "sample@email.com",
UserRole = Role.Administrator,
},
new User
{
Name = "SomeAnotherGuy",
Age = 42,
Password = "qwerty",
EMail = "sample2@email.com",
UserRole = Role.RegularUser,
}
};
var xml = XmlSerializationService<List<User>>.Serialize(users);
Console.WriteLine(xml);
]]></script>
<em><span style="font-family: Calibri;">Result:</span></em><br />
<pre class="brush: xml"><?xml version="1.0" encoding="utf-16"?>
<ArrayOfUser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<User Name="Rostyslav" UserMail="sample@email.com" Password="123" Age="24" UserRole="Administrator" />
<User Name="SomeAnotherGuy" UserMail="sample2@email.com" Password="qwerty" Age="42" UserRole="RegularUser" />
</ArrayOfUser>
</pre>
</div>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Standard .NET class -
<strong>Dataset also supports xml serialization</strong>. In one of my projects I had to get
some data (without a strict structure) from one database and store these result
data in another one. I loaded all data into Dataset object and just serialized
to xml and saved it. <o:p></o:p></span></span></div>
<i style="mso-bidi-font-style: normal;"><span style="mso-ansi-language: EN-US;"><o:p><span style="font-family: Calibri;"></span></o:p></span></i><br />
<span style="mso-ansi-language: EN-US;"><span style="font-size: medium;"><span style="color: #4f81bd;"><span style="font-family: Cambria;"><strong>Summary<o:p></o:p></strong></span></span></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">I think Xml
serialization is a nice .NET feature and it is really easy to use and fast to
implement. And you don’t have to parse manually XmlDocument anymore. Your code
will be more elegant and readable.<o:p></o:p></span></span></div>
<span style="mso-ansi-language: EN-US;"><span style="font-family: Calibri;">Yes, using XmlSerialization
is a bad idea when you try to use it with a huge amount of data, it will load
all data in the memory, but in the most common cases it is justified.<o:p></o:p></span></span>Rostyslav Yaremchukhttp://www.blogger.com/profile/03554485540947531622noreply@blogger.com0