tag:blogger.com,1999:blog-90737885024538205752024-03-05T03:34:30.860-08:00How to.............Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-9073788502453820575.post-79796966566155249132013-04-04T02:03:00.002-07:002013-04-04T02:06:17.015-07:00How to Print particular portion of webpage?<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>
<br />
<div class="MsoNormal">
<b>Introduction</b><o:p></o:p></div>
<div class="MsoNormal">
Here I am going to demonstrate “<i>how to print particular portion of webpage?”</i>. Manier we use “<i>Window.Print()”</i>
javascript in order to print data displayed on web-page. But there may be a
case when I didn’t need to print whole page or just required to print one small
portion of page instead of whole page, in those scenario “<i>Window.print()</i>” won’t work. So what should we do in such situation?
Here is the answer.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Inside the code</b><o:p></o:p></div>
<div class="MsoNormal">
We can print web document through javascript, so definitely
we’ll write some javascript to print portion of web page. Here is the script:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
function
CallPrint(strid) {<o:p></o:p></div>
<div class="MsoNormal">
var
prtContent = document.getElementById(strid);<o:p></o:p></div>
<div class="MsoNormal">
var
WinPrint = window.open('', '',
'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');<o:p></o:p></div>
<div class="MsoNormal">
WinPrint.document.write(prtContent.innerHTML);<o:p></o:p></div>
<div class="MsoNormal">
WinPrint.document.close();<o:p></o:p></div>
<div class="MsoNormal">
WinPrint.focus();<o:p></o:p></div>
<div class="MsoNormal">
WinPrint.print();<o:p></o:p></div>
<div class="MsoNormal">
WinPrint.close();<o:p></o:p></div>
<div class="MsoNormal">
prtContent.innerHTML = strOldOne;<o:p></o:p></div>
<div class="MsoNormal">
}<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Now, how to use this javascript? For that create a <div
/> element and assign unique id to it (i.e. <div ID=”divPrint”). Now put
your html content between this <div />.<o:p></o:p></div>
<div class="MsoNormal">
For example, if I want to print Item information which are
displays on grid like:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<asp:DataGrid Id=”grdItem” runat=”server><o:p></o:p></div>
<div class="MsoNormal">
<Columns><o:p></o:p></div>
<div class="MsoNormal">
……..<o:p></o:p></div>
<div class="MsoNormal">
……..<o:p></o:p></div>
<div class="MsoNormal">
</Columns><o:p></o:p></div>
<div class="MsoNormal">
</asp:DataGrid><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Put this whole html content between <div /> tag. <o:p></o:p></div>
<div class="MsoNormal">
Now onClientClick event of button call javascript function
like:<o:p></o:p></div>
<div class="MsoNormal">
OnClientClick=”CallPrint(‘divPrint’)”<o:p></o:p></div>
<span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US;">This
all makes your printing done.</span>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-89086763561827129402012-10-31T01:50:00.000-07:002013-03-15T06:49:41.704-07:00How to export Encoded Data into CSV Files?<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>
<br />
<div class="MsoNormal">
<span style="font-size: 10.0pt; line-height: 115%; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;"><span style="font-family: Arial, Helvetica, sans-serif;">Exporting
data into various file format - i.e. .csv, .tsv is frequently required in our
day to day action. Many of us have been written code segment for this also. My
purpose to write this article is to demonstrate <i>How export data using various encoding into csv format?</i><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: 10.0pt; line-height: 115%; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;"><span style="font-family: Arial, Helvetica, sans-serif;"><i><br /></i></span></span></div>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;">Here I’ve developed a
sample application which export a Unicode data stored in database into csv
file. Note that if you export data with Unicode encoding delimited by coma (‘,’),
it won’t gives you effective result. Let’s start how to overcome this problem.</span></span><br />
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<b><span style="font-family: "Calibri","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-hansi-theme-font: minor-latin;">Inside the code</span></b><br />
<b><span style="font-family: "Calibri","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-hansi-theme-font: minor-latin;"><br /></span></b>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8p95b81CFCktBI7ciP0Mb5DiSIkxVDRbdIkmZjZhUV-_pKNChXVh0CUoMz4g3Uei8WUhnwmctStqitjmD541278kLZCE7xLjIpqxWdsFhXe2VzGjgbA6IvaVN7TsyIiaQv3yn-uL47S0/s1600/ExportToCsv.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8p95b81CFCktBI7ciP0Mb5DiSIkxVDRbdIkmZjZhUV-_pKNChXVh0CUoMz4g3Uei8WUhnwmctStqitjmD541278kLZCE7xLjIpqxWdsFhXe2VzGjgbA6IvaVN7TsyIiaQv3yn-uL47S0/s320/ExportToCsv.PNG" width="257" /></a></div>
<b><span style="font-family: "Calibri","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-hansi-theme-font: minor-latin;"><br /></span></b>
<b><span style="font-family: "Calibri","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Courier New"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-hansi-theme-font: minor-latin;"><br /></span></b>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;">In above image you can see that Datagridview is loaded with
data. This data contains data in unicode format – Last two row contains data in
chainese and russian language. Also we’ve an option for selecting various
encoding and delimeters while exporting data. Code for file export is shown as
below.</span></span><br />
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">private</span>
<span style="color: blue;">void</span> btnExport_Click(<span style="color: blue;">object</span>
sender, <span style="color: #2b91af;">EventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">DataTable</span>
dt = dg.DataSource <span style="color: blue;">as</span> <span style="color: #2b91af;">DataTable</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(<span style="color: blue;">string</span>.IsNullOrEmpty(txtPath.Text.Trim()))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> ShowMessage(<span style="color: #a31515;">"Invalid file path. Please enter valid file
path"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">StringBuilder</span>
builder = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(chkHeader.Checked)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">var</span>
count = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">foreach</span>
(<span style="color: #2b91af;">DataColumn</span> column <span style="color: blue;">in</span>
dt.Columns)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> count++;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">string</span>
content = column.ColumnName + <span style="color: #a31515;">""</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> content = content.Replace(<span style="color: #a31515;">"\""</span>, <span style="color: #a31515;">"\"\""</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> builder.Append(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0}{1}{0}"</span>,
<span style="color: #2b91af;">Convert</span>.ToChar(34), content));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(count < dt.Columns.Count)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;">
builder.Append(GetDelimeter(cmbDelimeter.Text.Trim()));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> builder.Append(<span style="color: #2b91af;">Environment</span>.NewLine);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">foreach</span>
(<span style="color: #2b91af;">DataRow</span> row <span style="color: blue;">in</span>
dt.Rows)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">for</span>
(<span style="color: blue;">int</span> i = 0; i < row.ItemArray.Length; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(!<span style="color: #2b91af;">Convert</span>.IsDBNull(row[i]))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">string</span> content = row[i].ToString() + <span style="color: #a31515;">""</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> content =
content.Replace(<span style="color: #a31515;">"\""</span>, <span style="color: #a31515;">"\"\""</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> builder.Append(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0}{1}{0}"</span>,
<span style="color: #2b91af;">Convert</span>.ToChar(34), content));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(i < row.ItemArray.Length - 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;">
builder.Append(GetDelimeter(cmbDelimeter.Text.Trim()));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> builder.Append(<span style="color: #2b91af;">Environment</span>.NewLine);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> streamWriter = <span style="color: blue;">new</span>
<span style="color: #2b91af;">StreamWriter</span>(txtPath.Text.Trim(),
chkAppend.Checked, GetEncoding(cmbEncoding.Text.Trim())))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;">
streamWriter.Write(builder.ToString());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> streamWriter.Flush();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> streamWriter.Close();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> ShowMessage(<span style="color: #a31515;">"Data successfully exported"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10.0pt; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: Consolas; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;"><span style="font-family: Arial, Helvetica, sans-serif;">In above code you can see that we are iterating over each row in data
table and appending each cell data into string builder. Delimiter that we
choose while exporting is appended after each cell data through <i>GetDelimeter()</i> function. Code of <i>GetDelimeter()</i> function is shown below:</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10.0pt; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: Consolas; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">private</span>
<span style="color: blue;">string</span> GetDelimeter(<span style="color: blue;">string</span>
delimeter)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">string</span>
retval = <span style="color: #a31515;">""</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">switch</span>
(delimeter.ToLower())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"coma"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> retval = <span style="color: #a31515;">","</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"tab"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> retval = <span style="color: #a31515;">"\t"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"space"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> retval = <span style="color: #a31515;">"\b"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">return</span>
retval;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"> }</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-family: Consolas;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;">After
appending all data into string builder, initialize <i>StreamWriter</i> object with argument – Destination file path, data
should appended or not and content Encoding. Encoding is also chosen by us
while exporting. Code for selecting encoding is given below:</span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">private</span>
<span style="color: #2b91af;">Encoding</span> GetEncoding(<span style="color: blue;">string</span> encoding)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Encoding</span>
encod = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">switch</span>
(encoding.ToLower())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"unicode"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> encod = <span style="color: #2b91af;">Encoding</span>.Unicode;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"utf8"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> encod = <span style="color: #2b91af;">Encoding</span>.UTF8;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">case</span>
<span style="color: #a31515;">"ansi"</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">default</span>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> encod = <span style="color: #2b91af;">Encoding</span>.Default;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">return</span>
encod;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> }</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10.0pt; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: Consolas; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;"><span style="font-family: Arial, Helvetica, sans-serif;">This way we can export data in csv file with encoding we choose.</span><o:p></o:p></span></div>
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 10pt; line-height: 115%;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span></div>
<br />
<br />Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-76318224498062352472012-07-31T21:19:00.000-07:002012-07-31T21:19:13.201-07:00Sample MEF Example<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsg9FLrnWtJ7g960owL2qRftE-0C43XT1CZp_vDOr4kn_8wwwlHfcHUHtzpug-0c426F-kw4p8eoAfv9IYliW-jT0yr6vKsGGBmOgAsZwjs-wchPGnGgVGouhH7Z0nEkPFyYLhQgDs8Qs/s1600/Article.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsg9FLrnWtJ7g960owL2qRftE-0C43XT1CZp_vDOr4kn_8wwwlHfcHUHtzpug-0c426F-kw4p8eoAfv9IYliW-jT0yr6vKsGGBmOgAsZwjs-wchPGnGgVGouhH7Z0nEkPFyYLhQgDs8Qs/s1600/Article.GIF" /></a></div>
<div class="MsoNormal">
<b><span style="font-size: 12.0pt; line-height: 115%;"><br /></span></b></div>
<div class="MsoNormal">
<b><span style="font-size: 12.0pt; line-height: 115%;">Introduction<o:p></o:p></span></b></div>
<div class="MsoNormal">
<b><span style="font-size: 12.0pt; line-height: 115%;"><br /></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
I am not going cover detail about
MEF or not going describe functionality of MEF. One can find good material over
the net for MEF, so I am not going to post redundant information. What I feel
is MEF is an extensibility framework provided by Microsoft. Many other
extensibility framework exists but MEf is easy to initiate. Today I am going
develop a sample application which demonstrate how to import/export components.
Kindly note that this is just a small effort so that beginner can start working
with MEF.</div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 12.0pt; line-height: 115%;">Using the code<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 12.0pt; line-height: 115%;"><br /></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
In this article we are going to
cover following topic:</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l1 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]-->1)<span style="font-size: 7pt;">
</span><!--[endif]-->Develop Component behavior: This will be an
interface which functionality exported by components and consume by our host
application.</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l1 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]-->2)<span style="font-size: 7pt;">
</span><!--[endif]-->Host Application: Application that seeks for
component to be compose.</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l1 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]-->3)<span style="font-size: 7pt;">
</span><!--[endif]-->ExportLib1, ExportLib2: These are components –
actual implementation of our component behavior</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l1 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]-->4)<span style="font-size: 7pt;">
</span><!--[endif]-->Main Application: This uses all these.</div>
<div class="MsoNormal" style="text-align: justify;">
So lets start with 1<sup>st</sup>
part of our application.</div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-fareast-font-family: Consolas;">1)<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">Create
Contract library which contains <i>IComponent</i>
interface as shown below. Through this interface our component will
communicate.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> Contracts<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">interface</span>
<span style="color: #2b91af;">IComponent</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">string</span> Description { <span style="color: blue;">get</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">string</span> ManipulateOperation(<span style="color: blue;">params</span> <span style="color: blue;">double</span>[]
args);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]-->2)<span style="font-size: 7pt;"> </span><!--[endif]-->Create library ImportingLib, this contains
a class that actually host and calls the components. Here one has to add <i>System.ComponentModel.Composition</i>
assembly. Code for <i>Importer</i> class
shown as below.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.ComponentModel.Composition;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.ComponentModel.Composition.Hosting;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Reflection;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.IO;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> Contracts;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> ImportingLib<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">Importer</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
[<span style="color: #2b91af;">ImportMany</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IComponent</span>))]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">private</span> <span style="color: #2b91af;">IEnumerable</span><<span style="color: #2b91af;">Lazy</span><<span style="color: #2b91af;">IComponent</span>>>
operations;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">void</span>
DoImport()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: green;">//An
aggregate catalog that combines multiple catalogs</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">var</span>
catalog = <span style="color: blue;">new</span> <span style="color: #2b91af;">AggregateCatalog</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: green;">//Add all
the parts found in all assemblies in</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: green;">//the
same directory as the executing program</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> catalog.Catalogs.Add(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">new</span>
<span style="color: #2b91af;">DirectoryCatalog</span>(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Path</span>.GetDirectoryName(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Assembly</span>.GetExecutingAssembly().Location<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> )<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> )<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: green;">//Create
the CompositionContainer with the parts in the catalog.</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">CompositionContainer</span>
container = <span style="color: blue;">new</span> <span style="color: #2b91af;">CompositionContainer</span>(catalog);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: green;">//Fill
the imports of this object</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> container.ComposeParts(<span style="color: blue;">this</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">int</span>
AvailableNumberOfOperation<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">get</span>
{ <span style="color: blue;">return</span> operations != <span style="color: blue;">null</span>
? operations.Count() : 0; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> CallAllComponents(<span style="color: blue;">params</span>
<span style="color: blue;">double</span>[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">var</span>
result = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">foreach</span>
(<span style="color: #2b91af;">Lazy</span><<span style="color: #2b91af;">IComponent</span>>
com <span style="color: blue;">in</span> operations)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Console</span>.WriteLine(com..Description);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
result.Add(com.Value.ManipulateOperation(args));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">return</span>
result;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
This host imports components implementing <i>IComponent</i> and lists them into private variable <i>operations</i>. The <i>DoImport</i> method initializes list. <i>Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)</i>
gives a path in which our executing assembly resides.<b>MEF will automatically start searching all assemblies in the directory where the calling program resides.</b>
This is done by creating DirectoryCatelog on that folder and then adding it to
the main AggregateCatelog. Property <i>AvailableNumberOfOperation</i>
returns the number of found operations and <i>CallAllComponents</i>
method calls all the exporting components and returns the result.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]-->3)<span style="font-size: 7pt;">
</span><!--[endif]-->As
our host is ready for import functionality now major task is to develop
component that exports desired functionality. For demonstration I developed two
class library which contains classes that implement <i>IComponent</i> interface. Their code section shown as below.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.ComponentModel.Composition;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> Contracts;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> ExportSumLib<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
[<span style="color: #2b91af;">Export</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IComponent</span>))]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">SumOfNumberComponent</span> : <span style="color: #2b91af;">IComponent</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">string</span>
Description<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">get</span>
{ <span style="color: blue;">return</span> <span style="color: #a31515;">"Summation
of components"</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">string</span>
ManipulateOperation(<span style="color: blue;">params</span> <span style="color: blue;">double</span>[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">string</span>
result = <span style="color: #a31515;">""</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">double</span>
count = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">bool</span>
first = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">foreach</span>
(<span style="color: blue;">double</span> d <span style="color: blue;">in</span>
args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(first)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> count = d;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> first = <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> count += d;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> result += d.ToString() + <span style="color: #a31515;">" + "</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">return</span>
result.Trim(<span style="color: #a31515;">'+'</span>, <span style="color: #a31515;">'
'</span>) + <span style="color: #a31515;">" = "</span> +
count.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Here you can see the code of <i>SumOfNumberComponent</i>
class. This class built in ExportingLib1.dll file.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
You’ve add reference for Contracts.dll and System.ComponentModel.Composition.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Same way develop another library called ExportingLib2.dll which
contains <i>SubstractOfNumberComponent</i>
class. Code of that class shown as below.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.ComponentModel.Composition;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> Contracts;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> ExportSubstractLib<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
[<span style="color: #2b91af;">Export</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IComponent</span>))]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">SubstractOfNumberComponent</span> : <span style="color: #2b91af;">IComponent</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">string</span>
Description<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">get</span>
{ <span style="color: blue;">return</span> <span style="color: #a31515;">"Subtraction
of components"</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">public</span> <span style="color: blue;">string</span>
ManipulateOperation(<span style="color: blue;">params</span> <span style="color: blue;">double</span>[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">string</span>
result = <span style="color: #a31515;">""</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">double</span>
count = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">bool</span>
first = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">foreach</span>
(<span style="color: blue;">double</span> d <span style="color: blue;">in</span>
args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">if</span>
(first)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> count = d;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> first = <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> count -= d;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> result += d.ToString() + <span style="color: #a31515;">" - "</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">return</span>
result.Trim(<span style="color: #a31515;">'-'</span>, <span style="color: #a31515;">'
'</span>) + <span style="color: #a31515;">" = "</span> +
count.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now let’s move to the final step of this exercise.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Create an application that uses all these stuffs.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]-->4)<span style="font-size: 7pt;"> </span><!--[endif]-->Develop a console application called <i>MEFApplication</i>. Add reference of
ImportingLib.dll. now write down code section shown below.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> MEFApplication<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">static</span> <span style="color: blue;">void</span>
Main(<span style="color: blue;">string</span>[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">var</span>
t = <span style="color: blue;">new</span> ImportingLib.<span style="color: #2b91af;">Importer</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> t.DoImport();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"{0} component(s) are imported successfully."</span>,
t.AvailableNumberOfOperation);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: blue;">var</span>
result = t.CallAllComponents(125, 5, 10, 27, 45, 19, 10);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
<span style="color: blue;">foreach</span>
(<span style="color: blue;">string</span> s <span style="color: blue;">in</span>
result)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Console</span>.WriteLine(s);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">Console</span>.Read();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; mso-bidi-font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Before executing application do one thing go to the Project
Properties->Build option and set building path of each application to <b>bin\Debug</b> folder of <i>MEFApplication</i> project. Now build whole
project and see the magic.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
You can when you execute <i>MEFApplication</i>
project it will show <b>2 component(s) are
imported successfully.</b> message and rest messages. If you remove any of
library from ExportingLib1.dll or ExportingLib2.dll message will change to <b>1 component(s) are imported successfully.</b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-36036111901001644242012-05-09T23:59:00.001-07:002012-05-09T23:59:43.539-07:00Inversion of Control : Overview and Example<div dir="ltr" style="text-align: left;" trbidi="on">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Calibri","sans-serif";}
</style>
<![endif]-->
<br />
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Introduction</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Definition: <i style="mso-bidi-font-style: normal;">“Inversion
of Control, is an abstract principal describing an aspect of some software
architecture design in which the flow of control of a system is inverted in
comparison to procedural programming.”</i> </div>
<div class="MsoNormal">
Let’s go in simple manner what happened in procedural
programming is chunk of code that uses or consumes another chunk of code, is in
control of process. It knows what piece of code, which method in which class it
uses and in this way it knows about some implementation detail in the code it
uses.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Example</b></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh60z32c6f2MtVODDbcJgCv0N1ER75IoF0AiIOvUSiaXi2ZjTb9IuqBD3KN9YzTno0sS99G60DwmCim-oIpuCL99yovlGNm6DHPdLZgVCXqPV2YwvOnyV-vNgYeginiU6YArYEc1wc_-8c/s1600/1.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="48" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh60z32c6f2MtVODDbcJgCv0N1ER75IoF0AiIOvUSiaXi2ZjTb9IuqBD3KN9YzTno0sS99G60DwmCim-oIpuCL99yovlGNm6DHPdLZgVCXqPV2YwvOnyV-vNgYeginiU6YArYEc1wc_-8c/s320/1.JPG" width="320" /></a></div>
<div class="MsoNormal">
<span style="mso-no-proof: yes;"><br /></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here is a example of such scenario when a class called X
uses another class called Y. The consumer class X, needs to consume the class
Y, to accomplish something. This is very obvious example, but waits and thinks
for a minute, does X really needs to know that it uses Y? Isn’t it enough that
X knows that it uses something that has the behavior, the methods, the
properties etc. of Y without knowing who actually implement the behavior? By
extracting abstract definition of the behavior used by X in Y and let the
consumer X to use an instance of that behavior instead of Y. This way X does
same thing without knowing the specific about Y</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-no-proof: yes;"></span></div>
<div class="MsoNormal">
<span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV2s8qL4SpCa16oV3ikFYOoRDKlsiIp7BWKcMiuImouojHYwc91jki5YQ_pK225iJV3L1Wu-krDo1Au4hdih8senGwAPVwRnn5PBd9kQYxmNwshUVRzIkEqUiTESrjyKpdHw1BI6gZ6z8/s1600/2.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="131" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV2s8qL4SpCa16oV3ikFYOoRDKlsiIp7BWKcMiuImouojHYwc91jki5YQ_pK225iJV3L1Wu-krDo1Au4hdih8senGwAPVwRnn5PBd9kQYxmNwshUVRzIkEqUiTESrjyKpdHw1BI6gZ6z8/s320/2.JPG" width="320" /></a></div>
<div class="MsoNormal">
<span><br /></span></div>
<div class="MsoNormal">
<span><br /></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As illustrated above class Y implement interface I and class
X uses I. Here interesting matter is though class X still uses Class Y through
interface I, but X doesn’t know that it uses Y, it just knows that it uses
something that implement I, right now it’s Y but it would be A, B or C that
implement interface I. this way we are reducing dependency of X over Y. Now you
might have question in your mind, what is dependency? And how it’s dangerous
for software?</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">What is Dependency in
terms of classes?</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Example mentioned in starting of this article is example of
dependent classes. When class X uses any method of class Y or class Y as whole
we can says that class X has some level of dependency over class Y. dependency
may extend to several levels. i.e. X uses Y, Y uses A and B, A uses C and more.
This way this chain go on and on. Problem with this is if we have any change in
these classes, it may spawn to multiple classes. Solution of this dependency is
define loose relation between classes. One way to achieve this is <b style="mso-bidi-font-weight: normal;">Inversion of Control</b> pattern. This
pattern uses <b style="mso-bidi-font-weight: normal;">Dependency Injection</b> to
eliminate tight coupling between objects. Let’s see dependency with some
practical example.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">A Sample Scenario</b></div>
<div class="MsoNormal">
We have a class called <i style="mso-bidi-font-style: normal;">LoggerEngine</i>,
which is used for logging messages. It had a method called Log which receives a
string object as an argument and logs it into file using another class named
FileLogger .</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9s-N3CCbgslZ-z-6kCJSDu2BnF0mreMTf699r-10v0ROcr9HjjxNBpzKy4j-j2aQ6n20zljVdgtX77L-VirDJ5Oux5FOKy4iQIWBGAUEmBvUjYu-GNtXrvJEltyhKc6xldhAr3kbs6wc/s1600/4.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="86" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9s-N3CCbgslZ-z-6kCJSDu2BnF0mreMTf699r-10v0ROcr9HjjxNBpzKy4j-j2aQ6n20zljVdgtX77L-VirDJ5Oux5FOKy4iQIWBGAUEmBvUjYu-GNtXrvJEltyhKc6xldhAr3kbs6wc/s400/4.JPG" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The code for LoggerEngine looks like this.</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0KiA138m8XteyQ2226Ah7BinU_Shz7T8tn9bx4satS4eH9Qnwhy-LgixQh6Hp-njM6m36i1m6SAAkmFHaXw8xcChQ0ofvbQwO9RoRxa41nbq7gqj34-uLjqF8Fwvd6pPIBp6riODQHeo/s1600/5.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="218" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0KiA138m8XteyQ2226Ah7BinU_Shz7T8tn9bx4satS4eH9Qnwhy-LgixQh6Hp-njM6m36i1m6SAAkmFHaXw8xcChQ0ofvbQwO9RoRxa41nbq7gqj34-uLjqF8Fwvd6pPIBp6riODQHeo/s400/5.JPG" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Notice above code, in which Log method creates instance of
FileLogger class and then logs the message by using its own Log method. Here
you may define all three method to static or there might be singleton instance
of FileLogger class. Ways doesn’t matter what is the matter, dependency of
LoggerEngine class over FileLogger class. We can solve this matter by creating
an abstract definition of the behavior in FileLogger that LoggerEngine needs in
the form of interface ILooger and making Log method use an instance of ILogger
instead of<span style="mso-spacerun: yes;"> </span>an instance of FileLogger</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUeYOSfn1Ft7FYTkvGDs3XD8o4tMjn7_LN8FnD0UQyL94mDqxnOLg1CEuKpItuYymdwbmH1s7AJ7TxIevEKeTDdsg53WhLibt7DVRkv9H2aa-o0MrQk_TVhgZ5un_x72v0G-tGU0-pQF0/s1600/6.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="156" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUeYOSfn1Ft7FYTkvGDs3XD8o4tMjn7_LN8FnD0UQyL94mDqxnOLg1CEuKpItuYymdwbmH1s7AJ7TxIevEKeTDdsg53WhLibt7DVRkv9H2aa-o0MrQk_TVhgZ5un_x72v0G-tGU0-pQF0/s400/6.JPG" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Now LoggerEngine no longer knows about the FileLogger class.
It just uses instance of ILogger interface of which FileLogger is one of many
possibly implementation. Now in fact we could easily change it so that
LoogerEngine logs message on console instead of file without changing single
line of code in LoggerEngine.</div>
<div class="MsoNormal">
This all sounds good, but main question is How does LoggerEngine
get instance of ILogger? Here we need a way for LoggerEngine to get an instance
of ILogger at runtime without knowing about any concrete implementation. There
are couple of ways to do that, one of which is through <b style="mso-bidi-font-weight: normal;">Dependency Injection</b>.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Dependency Injection</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This is basic concept for implementing <b style="mso-bidi-font-weight: normal;">inversion of control</b> (IoC) pattern.</div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l1 level1 lfo1;">It eliminate tight
coupling between objects.</li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo1;">Makes object and
application more flexible.</li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo1;">It facilitates to create
more loosely coupled objects and their dependencies.</li>
</ol>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTKeAYNk1h6E7bMmx0n_0YA-gI3Z-1UOPOqdFGnlXfkMu2XXLW26ySWvj04xJnq9XtBETsauz1O7ZjFq9W9sFkmqC11Jo0hrdV720kGkZRAldsJla-2om3xWFhwIlGgmWI190awoCDyaY/s1600/3.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="255" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTKeAYNk1h6E7bMmx0n_0YA-gI3Z-1UOPOqdFGnlXfkMu2XXLW26ySWvj04xJnq9XtBETsauz1O7ZjFq9W9sFkmqC11Jo0hrdV720kGkZRAldsJla-2om3xWFhwIlGgmWI190awoCDyaY/s400/3.JPG" width="400" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Constructor Injection</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here object reference would get pass to constructor of
business class LoggerEngine. In this case since LoggerEngine class depends on
FileLogger class, so reference of FileLogger class will pass steps to implement
Constructor Injection.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-1</b> Create an
Interface</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAR1tNzeurM6Z_Ejgw8Xw4LGA_v7pNoFRSUKl7w-cNqcL619zsCq7OIFcUfiluHujVBVNPNuEioJwXMPKDd-pt8KIf1ZTO1st9P0DMy0fw4GjQo3U_y3RdZJiDtQraa6IzG1Ydzl2hQ88/s1600/7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAR1tNzeurM6Z_Ejgw8Xw4LGA_v7pNoFRSUKl7w-cNqcL619zsCq7OIFcUfiluHujVBVNPNuEioJwXMPKDd-pt8KIf1ZTO1st9P0DMy0fw4GjQo3U_y3RdZJiDtQraa6IzG1Ydzl2hQ88/s640/7.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Only method of interface will expose in business class.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-2</b> Implement
interface to FileLogger class</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZRc5Ne2fpnn9jGPO5rQAgvDpFHquyCJewtFsO-IDNX3FXW39de8X2gXoq2D6QqGlkgRxeY4rbMvjvzTlq2QSQuKIoLWMILWlI3aXlul1JzCZzePMI0J5h_aqN2tNh2TxsXg06hHM5K_I/s1600/8.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZRc5Ne2fpnn9jGPO5rQAgvDpFHquyCJewtFsO-IDNX3FXW39de8X2gXoq2D6QqGlkgRxeY4rbMvjvzTlq2QSQuKIoLWMILWlI3aXlul1JzCZzePMI0J5h_aqN2tNh2TxsXg06hHM5K_I/s640/8.JPG" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnxTufJQOpUBL_ZNgTb0CtlOYwCMPJaBQ9rqr4yhsUwTqpQQXx1BSHUEW7J0SCsXAIPxSW2YiP1zTGaz-nh3POSTwQ49c9iGRfx6Cj0JVQRotj9QO9UnXvpkSlS4otKtAiezqbz7FlHoA/s1600/9.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnxTufJQOpUBL_ZNgTb0CtlOYwCMPJaBQ9rqr4yhsUwTqpQQXx1BSHUEW7J0SCsXAIPxSW2YiP1zTGaz-nh3POSTwQ49c9iGRfx6Cj0JVQRotj9QO9UnXvpkSlS4otKtAiezqbz7FlHoA/s640/9.JPG" width="640" /><span id="goog_856178998"></span><span id="goog_856178999"></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhU7LPbQ23OJ-fqzCMVhAkPeCY7TtR91imPA2vBCOzK4UzaauwEYZfHfLoZg-x9IP37Sg6XANLyt9Pf8Scek7gbiLSFCaVXDZh1zPgvrCjcv-c1Jvl-XdwAKGXukGs_v297ZXP3AS99WkE/s1600/10.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhU7LPbQ23OJ-fqzCMVhAkPeCY7TtR91imPA2vBCOzK4UzaauwEYZfHfLoZg-x9IP37Sg6XANLyt9Pf8Scek7gbiLSFCaVXDZh1zPgvrCjcv-c1Jvl-XdwAKGXukGs_v297ZXP3AS99WkE/s640/10.JPG" width="640" /></a></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Object of FileLogger class is going to be referenced by
LoggerEngine class. So this class needs to be implement interface.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-3</b> Make
reference of interface in LoggerEngine class</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYJh5pIPvQg6Z1U_fw8GMv3nAWkNe5b4ELOCTCdzgrCMz63UPEWiI6D2UTHTf14zBp-omkM9Y4qd2R5JYTHayYIHg23PF-Q77XKEwmkWqKMQ-UncPLqo86mhZypsW1Xr_hz7nYZVIyNwc/s1600/11.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYJh5pIPvQg6Z1U_fw8GMv3nAWkNe5b4ELOCTCdzgrCMz63UPEWiI6D2UTHTf14zBp-omkM9Y4qd2R5JYTHayYIHg23PF-Q77XKEwmkWqKMQ-UncPLqo86mhZypsW1Xr_hz7nYZVIyNwc/s640/11.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiErz5rexXOA2S_i5G4lJQqPZ3eO4LlehJA6IhaknVkC4LUHUQrCCjYKinMX7_bvLfsBOGSL_7d12ZWuYXZJB5c7F1VIlBp7Au0IYt_WicPUrT5W4qWi7nO63CqJnVbT8s40Dz1Y_yCklU/s1600/12.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="132" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiErz5rexXOA2S_i5G4lJQqPZ3eO4LlehJA6IhaknVkC4LUHUQrCCjYKinMX7_bvLfsBOGSL_7d12ZWuYXZJB5c7F1VIlBp7Au0IYt_WicPUrT5W4qWi7nO63CqJnVbT8s40Dz1Y_yCklU/s640/12.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-4</b> Create a
third party class, who creates instance of all this objects. </div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1tVXP4nw6GZvWGmvrO5fJMzfLqgBSYtSrSjw0z2pn4cBYVNaZ0PboMGpwieauR5kDxN6NyT5MGXbvS9kSnT3Br7iFE-ikjgOqi6w_efl6liOeh91l-ufFV9RnvyiLH7KbFwslKBIrO6Y/s1600/13.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1tVXP4nw6GZvWGmvrO5fJMzfLqgBSYtSrSjw0z2pn4cBYVNaZ0PboMGpwieauR5kDxN6NyT5MGXbvS9kSnT3Br7iFE-ikjgOqi6w_efl6liOeh91l-ufFV9RnvyiLH7KbFwslKBIrO6Y/s640/13.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qJNee2vRJKXwmRiasyRAcEohpmpjktbFi2FgvVwCrICMVcAtKK8248JzpWlSc27dB6iASCOegEN41WbTB8JWN6OVlyzMC2fP4l2PHQdQpOUd3PdFUeZFZOQPiMdIY7Gr17NfYRHjsCE/s1600/14.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="142" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qJNee2vRJKXwmRiasyRAcEohpmpjktbFi2FgvVwCrICMVcAtKK8248JzpWlSc27dB6iASCOegEN41WbTB8JWN6OVlyzMC2fP4l2PHQdQpOUd3PdFUeZFZOQPiMdIY7Gr17NfYRHjsCE/s640/14.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step 5 </b>Use this
third party class at client side.</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhonOK-oQn1RmVok0q1-AkdPWJdLZ4R3bUuM-3CzWxAMNDeydR0Rk1gSunxIptOtP5g_rfCnjWTX22M6YP5MY81252Gz-bobJknTofjy6VMf0ctlkx1yLMQ21ak41J3bt-q7-y8ycKszSY/s1600/15.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="272" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhonOK-oQn1RmVok0q1-AkdPWJdLZ4R3bUuM-3CzWxAMNDeydR0Rk1gSunxIptOtP5g_rfCnjWTX22M6YP5MY81252Gz-bobJknTofjy6VMf0ctlkx1yLMQ21ak41J3bt-q7-y8ycKszSY/s640/15.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZUjQ7C5hK9UqB3uZeLDYH6rMpiEIw4r3HL0HZe2H4AxgnckxEVNE8VL1yNg37N0l1b_gFD1vUKnoJqjEoq3FSgKHkk0YsR_VasbucoXBMLNhUaByStHZbAhpDSWs2iISWk-S5Sf-6Kkg/s1600/16.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="78" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZUjQ7C5hK9UqB3uZeLDYH6rMpiEIw4r3HL0HZe2H4AxgnckxEVNE8VL1yNg37N0l1b_gFD1vUKnoJqjEoq3FSgKHkk0YsR_VasbucoXBMLNhUaByStHZbAhpDSWs2iISWk-S5Sf-6Kkg/s640/16.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Disadvantage of
constructor injection</b></div>
<ul style="margin-top: 0in;" type="disc">
<li class="MsoNormal" style="mso-list: l0 level1 lfo2;">In constructor injection
business logic class doesn’t have default constructor.</li>
<li class="MsoNormal" style="mso-list: l0 level1 lfo2;">Once the class is
instantiated, object dependency cannot be<span style="mso-spacerun: yes;">
</span>changed.</li>
</ul>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Setter Injection</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This uses the Properties to inject dependency. Here rather
than creating a reference and assign them in constructor, it has been done in
properties. By this way LoggingEngine class could have default constructor.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-1</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-2</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-3</b> LoggerEngine.cs</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuj01uEGeIkSgQ3uC4_nc1jZC-CA_rUcAbsC1tj_LjekzSuuKKCseGNf3bfOJdPATiISBJo1YpyiIk6cXzhfrKzv5EhtQHqQtqX5hlDE7jYAFo1aOl2lT0nehSSVurwACvS0L2jXo6Zuk/s1600/17.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuj01uEGeIkSgQ3uC4_nc1jZC-CA_rUcAbsC1tj_LjekzSuuKKCseGNf3bfOJdPATiISBJo1YpyiIk6cXzhfrKzv5EhtQHqQtqX5hlDE7jYAFo1aOl2lT0nehSSVurwACvS0L2jXo6Zuk/s640/17.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT8SuhAQo_FzaWN-O9lxj0QSNpDZjcsTghaKcTmF8VPzPTuZChYvnOkOmGo2QqiC6OuQYlb9tGp00PhKFq_AcLg3O-pvs28lTa2AV8epxJ2oeyYQ0jdHtckXZeXgvtktlYmVONo-VXdGA/s1600/18.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="144" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT8SuhAQo_FzaWN-O9lxj0QSNpDZjcsTghaKcTmF8VPzPTuZChYvnOkOmGo2QqiC6OuQYlb9tGp00PhKFq_AcLg3O-pvs28lTa2AV8epxJ2oeyYQ0jdHtckXZeXgvtktlYmVONo-VXdGA/s640/18.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In LoggerEngine class, there is property Logger, which is
setting and getting value of reference of interface.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-4</b> There is
some changes in third party class</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbEevDBTUReaL4sWPgNjkXyn1yHeVascQfAwBAviZ-b_mJO0FDOYfqQJ7zAV3Rc2CrkN9J4oo3PBzuVGEcvhxWNNJI2q2oZEwOp3Ck3TkscZckXC_LqyJNUrKizUjILTfTl98NgQYU-Zg/s1600/19.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbEevDBTUReaL4sWPgNjkXyn1yHeVascQfAwBAviZ-b_mJO0FDOYfqQJ7zAV3Rc2CrkN9J4oo3PBzuVGEcvhxWNNJI2q2oZEwOp3Ck3TkscZckXC_LqyJNUrKizUjILTfTl98NgQYU-Zg/s640/19.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtfhMXUQoHssQ7uaTv966WcUdzNBLxuZYT9j2CoGzX33tpw2oWUM8K5PeLREnO2SsHT4z0IWaC64uoBR3B7B4dr1eq_EcAewfTsTkasspyFLNvHImWIWO5-SmXDaa_oHxETpxAbbbuqVI/s1600/20.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="206" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtfhMXUQoHssQ7uaTv966WcUdzNBLxuZYT9j2CoGzX33tpw2oWUM8K5PeLREnO2SsHT4z0IWaC64uoBR3B7B4dr1eq_EcAewfTsTkasspyFLNvHImWIWO5-SmXDaa_oHxETpxAbbbuqVI/s640/20.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-5</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Advantages of Setter
Injection</b></div>
<div class="MsoNormal">
<br /></div>
<ul style="margin-top: 0in;" type="disc">
<li class="MsoNormal" style="mso-list: l2 level1 lfo3;">It’s more flexible than
Constructor Injection.</li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo3;">Dependency of object can
be change without creating any instance.</li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo3;">Dependency of object can
be change without changing constructor.</li>
<li class="MsoNormal">Setters has constructive
and self descriptive meaningful name that simplify understanding and using
them </li>
</ul>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Interface Injection</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this instance creation is transferred to an additional
interface. This interface passes as an argument in calling method.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-1</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-2</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-3</b> Create an
interface and its implemented class, which creates instance.</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGR7fuSc7EvBD-H0vGuz8WlvTiawQfwwoBVnHU-NZQX9FGLAzVkeY_o83EvYfcuvq312ndyICtYEH6nh9DDWuN3LL8FQ568639EuseeRqT_nK77OKEh95noH6fYOdBkt2A56pp_cNhVPk/s1600/21.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGR7fuSc7EvBD-H0vGuz8WlvTiawQfwwoBVnHU-NZQX9FGLAzVkeY_o83EvYfcuvq312ndyICtYEH6nh9DDWuN3LL8FQ568639EuseeRqT_nK77OKEh95noH6fYOdBkt2A56pp_cNhVPk/s640/21.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkwxZEHgTLBgEq6kvsUBHsY1uf-uP7VfzaNPj45L2aGRmBuQHVxmumq-IjB82h-su32HBYThMIu8N9xTSJBSaps9P5DBoM6L1zaQrinYDGQj3pis12VoGn3YiCIHSu7xVRq0VUrohxKHc/s1600/22.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="78" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkwxZEHgTLBgEq6kvsUBHsY1uf-uP7VfzaNPj45L2aGRmBuQHVxmumq-IjB82h-su32HBYThMIu8N9xTSJBSaps9P5DBoM6L1zaQrinYDGQj3pis12VoGn3YiCIHSu7xVRq0VUrohxKHc/s640/22.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here one more interface and its implemented class has
defined which are responsible for instance creation.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-4</b>
LoggerEngine.cs</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkGM2a8ir0YRIB9oSXPKx-uXTLoB2JOvvzZ60m4nEUh3L86K1jKm5bwk73SfVl35f4Dfilo9auEp7ZxGfSE43D2cbTOHmsGNEPc-ZquLw-UpPrUER6yvR_QhrLj2n7JkIelBREr0yp_XM/s1600/23.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkGM2a8ir0YRIB9oSXPKx-uXTLoB2JOvvzZ60m4nEUh3L86K1jKm5bwk73SfVl35f4Dfilo9auEp7ZxGfSE43D2cbTOHmsGNEPc-ZquLw-UpPrUER6yvR_QhrLj2n7JkIelBREr0yp_XM/s640/23.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0n24DcFiCicS9asmxLZbNNtA0OhEnywEL0kyA5ZRHNxT8Iman8LHR6t76-HRYLhD4tA7gDVmXRRqmiOr7kpSz4IIoqbmTUSkEOxIM2AU1hhcc2GjYAf9Ob8dxPwYd4EBsP75NjucqJV8/s1600/24.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="66" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0n24DcFiCicS9asmxLZbNNtA0OhEnywEL0kyA5ZRHNxT8Iman8LHR6t76-HRYLhD4tA7gDVmXRRqmiOr7kpSz4IIoqbmTUSkEOxIM2AU1hhcc2GjYAf9Ob8dxPwYd4EBsP75NjucqJV8/s640/24.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-5</b> Our third
party class will be</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwh5Y06mVCWEQXlfSsKL1iQk0ysOuQAQ0IkDoaH20Nu-c8AKDQaMVyOmzPX3k1EBlJTsFlsCIu1KmTJCAk_rWmBFUbP9c3m4QmotcH4TErh78GJzTwf1WsmguWBeRQ8AH7AL8_CKYGGIw/s1600/25.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwh5Y06mVCWEQXlfSsKL1iQk0ysOuQAQ0IkDoaH20Nu-c8AKDQaMVyOmzPX3k1EBlJTsFlsCIu1KmTJCAk_rWmBFUbP9c3m4QmotcH4TErh78GJzTwf1WsmguWBeRQ8AH7AL8_CKYGGIw/s640/25.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZQ_SAbgR5yuqZ1Y3QWwFSRQvbCt5ImnR4KHJrr7RlyrUQdly74osVvIjBi4bEqKjubpnQLhg5Lwg1baXpxJaU1F857d2vJMH-i5hGsrhmc8Iorf0WwOi154BjwNFjXChT1Lcsk2DzMRs/s1600/26.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZQ_SAbgR5yuqZ1Y3QWwFSRQvbCt5ImnR4KHJrr7RlyrUQdly74osVvIjBi4bEqKjubpnQLhg5Lwg1baXpxJaU1F857d2vJMH-i5hGsrhmc8Iorf0WwOi154BjwNFjXChT1Lcsk2DzMRs/s640/26.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-6</b> Same as
Step-5 in Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Service Locator
Injection</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here one Service class introduced which contains either
static method or Service class may be uses singleton pattern.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-1</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-2</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-3</b> Define
service class as follow</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUg-GaySxkkKeIbMjn1MhbxxhME3y_oQcYUjN-ijaleDP_6QU41gOkV4HJOuAtRvjOayDeAhXKw3DZJH29tQdIJCkyqCNokYHY8RpL3Q727FRkNdeyTO8-d79glfdGRCAbBFQPijhMCzs/s1600/27.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUg-GaySxkkKeIbMjn1MhbxxhME3y_oQcYUjN-ijaleDP_6QU41gOkV4HJOuAtRvjOayDeAhXKw3DZJH29tQdIJCkyqCNokYHY8RpL3Q727FRkNdeyTO8-d79glfdGRCAbBFQPijhMCzs/s640/27.JPG" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJADGRVoD4kHWeYjKJ926pEMH0wykXgoyzG8m1vrw716yoBzUtnTH_Pyi_IGZlA3nUnjTCCsCLOF2OrHLhvQuVrb8TILATR3IREXYo708OMuA6Mnw_q8sQF8kID27Zz1tSx3u1IvhVARI/s1600/28.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="46" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJADGRVoD4kHWeYjKJ926pEMH0wykXgoyzG8m1vrw716yoBzUtnTH_Pyi_IGZlA3nUnjTCCsCLOF2OrHLhvQuVrb8TILATR3IREXYo708OMuA6Mnw_q8sQF8kID27Zz1tSx3u1IvhVARI/s640/28.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here we have static writeonly property Logger which is uses
for setting value of reference of ILogger instance and a static mettod which is
uses for getting value of reference of ILogger instance</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-4</b>
LoggerEngine.cs</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdysir55wWf5zCguKwkaQUPSt1ye5kAqUN2sOgqqgPv7aWajNNE4NyPv7zIsN_bHjmQSB6DNFgQeIKYR3eNS_kgO9PvqTts1NqB5QZapDuSFQJ5B33Wwzjjzbnw67GBVXZl5MZaiZ4Qvw/s1600/29.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="272" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdysir55wWf5zCguKwkaQUPSt1ye5kAqUN2sOgqqgPv7aWajNNE4NyPv7zIsN_bHjmQSB6DNFgQeIKYR3eNS_kgO9PvqTts1NqB5QZapDuSFQJ5B33Wwzjjzbnw67GBVXZl5MZaiZ4Qvw/s640/29.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0a8rt0fr_pV8OrXlhyphenhyphencQTWEC_7dGBJER8a2TkkU1zzUVCg3vHdGq7QpUsvkiPUjh4fS-3yP-iAepi3jcWhym5JUNdh4YM5a3cigGnYcPiqiLIsvUarUgGURV_dJYzogwGoHkn1ydmmlk/s1600/30.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="48" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0a8rt0fr_pV8OrXlhyphenhyphencQTWEC_7dGBJER8a2TkkU1zzUVCg3vHdGq7QpUsvkiPUjh4fS-3yP-iAepi3jcWhym5JUNdh4YM5a3cigGnYcPiqiLIsvUarUgGURV_dJYzogwGoHkn1ydmmlk/s640/30.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-5</b>
IoCClass.cs</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjATsI3qxMQkNZcVWAmq7wXvLckBqulF2d2poIT0pPSdslARbMXzLtEgkKP0XhBwAPwFO9tejArDxlbPicoaZxyR-IEDlPrTvSX3HpjHynQiOzzvTWkBHtUQPzH2nAEfaUY4SgKHCfkJZc/s1600/31.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjATsI3qxMQkNZcVWAmq7wXvLckBqulF2d2poIT0pPSdslARbMXzLtEgkKP0XhBwAPwFO9tejArDxlbPicoaZxyR-IEDlPrTvSX3HpjHynQiOzzvTWkBHtUQPzH2nAEfaUY4SgKHCfkJZc/s640/31.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigKiemsPqeBcBF_z0IthwHgKOjGQYF7P3yRBQNGcmfO-P6dbdqvMx4zdhIhuyG4xwF5bzBMk-g-w14YrjKMYJNCY3f7QU5bW0RhHktG0hq7JjgkWDpNwROY9Q5w3XRBYJiLjsbmeJaFvQ/s1600/32.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="238" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigKiemsPqeBcBF_z0IthwHgKOjGQYF7P3yRBQNGcmfO-P6dbdqvMx4zdhIhuyG4xwF5bzBMk-g-w14YrjKMYJNCY3f7QU5bW0RhHktG0hq7JjgkWDpNwROY9Q5w3XRBYJiLjsbmeJaFvQ/s640/32.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-6</b> Same as
Step-5 in Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Generic-Type
Injectiom</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this method either we can define Generic LoggerEngine
class or Generic method in LoggerEngine clas. Generic method would be more
appropriate because if we use Generic class then it will cause same problems
which are identified in Constructor Injection.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-1</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-2</b> Same as
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-3</b></div>
<div class="MsoNormal">
<b> </b> </div>
<div class="MsoNormal">
Let’s define LoggerEngine class which contains generic
method for writing log</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjNnCG4VlPucrcYFffLjBMgD-xVL-WGTf-V9uM_ulW-CWJ_PXDtYQQLVqOkbbD_6HcbO_nxsRzelxOFKQNQskSXPf_ZZWe47bUWkD-pJBzAr_6D72zO9U5qpqa196VaqSuBOpGm2PJLNM/s1600/33.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjNnCG4VlPucrcYFffLjBMgD-xVL-WGTf-V9uM_ulW-CWJ_PXDtYQQLVqOkbbD_6HcbO_nxsRzelxOFKQNQskSXPf_ZZWe47bUWkD-pJBzAr_6D72zO9U5qpqa196VaqSuBOpGm2PJLNM/s640/33.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl61uxzJh3JSStzeqCEnOKOnAWex4x8KQlU4W2IuYI2LOEfQ2Xx2YRXBDMXhDRjtmekqIUJE8ZYG5M4Jfuq60bpFGqOOwANsRZ_nLbwYrsVujZs6ig0RW4Gl_1HGOyF3kkaV1hnMht0CU/s1600/34.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="28" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl61uxzJh3JSStzeqCEnOKOnAWex4x8KQlU4W2IuYI2LOEfQ2Xx2YRXBDMXhDRjtmekqIUJE8ZYG5M4Jfuq60bpFGqOOwANsRZ_nLbwYrsVujZs6ig0RW4Gl_1HGOyF3kkaV1hnMht0CU/s640/34.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here we explicitly mentioned that our TLogger would be any
reference implementation of ILogger.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Step-4</b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Our third party code would be as per shown below.</div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghFi7M8zp63E_iCCjaQy5yY40WHWS-QNTspBtWuKXUiiWrh6tfNN3_C54Zod5u5Rbj9A2u1DL-omcNtEGD1HXEp9YcZYSlC637ulfb1JqS1H3ujUdWXX3yiyMH9FpxdVtVwlL4J2zXXxw/s1600/35.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghFi7M8zp63E_iCCjaQy5yY40WHWS-QNTspBtWuKXUiiWrh6tfNN3_C54Zod5u5Rbj9A2u1DL-omcNtEGD1HXEp9YcZYSlC637ulfb1JqS1H3ujUdWXX3yiyMH9FpxdVtVwlL4J2zXXxw/s640/35.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZCG9aPQT1BjE5p4amqQWhU3KMxX5zwFkUty4UUtx5dLiJy8FgLsSxI0EC2dn_OSP6Eq9yHq5OLBmIZVj6rVzXBegJt2XKNNjYwfQHBYI6Tf28Ax8dRIcNuv2MpVVCwPyvkHZ1B-gGtzo/s1600/36.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZCG9aPQT1BjE5p4amqQWhU3KMxX5zwFkUty4UUtx5dLiJy8FgLsSxI0EC2dn_OSP6Eq9yHq5OLBmIZVj6rVzXBegJt2XKNNjYwfQHBYI6Tf28Ax8dRIcNuv2MpVVCwPyvkHZ1B-gGtzo/s640/36.JPG" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Step-5</b> Same as in
Constructor Injection</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Benefits of Inversion
of Controls</b></div>
<ul style="margin-top: 0in;" type="disc">
<li class="MsoNormal" style="mso-list: l3 level1 lfo4;">We can reduce dependency
between objects and can design more flexible systems.</li>
<li class="MsoNormal" style="mso-list: l3 level1 lfo4;">We can isolate our code at
a time of unit testing.</li>
</ul>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div align="right" class="MsoNormal" style="text-align: right;">
<br /></div>
</div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-5785867464293964682012-04-21T02:56:00.000-07:002012-04-24T21:03:34.942-07:00Asynchronous methods in C#: Part II<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Introduction</b></div>
<div class="MsoNormal">
In this article we demonstrate how to report progress
information and intermediate results from asynchronous method. This article
builds on previous article, <a href="http://himanshumbri.blogspot.in/2012/04/asynchronous-method-in-c-part-i.html" target="_blank">Asynchronousmethod in C#: Part I.</a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Using Code</b></div>
<div class="MsoNormal">
In this article we do following changes in our previous
example.</div>
<div class="MsoNormal">
<br /></div>
<ul style="text-align: left;">
<li><span style="font-family: Symbol;">·<span style="font: 7pt "Times New Roman";">
</span></span><b style="mso-bidi-font-weight: normal;">Modify
Perform</b> method for reporting progress.</li>
<li><span style="font-family: Symbol;">·<span style="font: 7pt "Times New Roman";">
</span></span><b style="mso-bidi-font-weight: normal;">Implement
ProgressChanged</b> event.</li>
<li><span style="font-family: Symbol;">·<span style="font: 7pt "Times New Roman";">
</span></span><b style="mso-bidi-font-weight: normal;">Define
method ReportProgrss</b> that synchronizes <b style="mso-bidi-font-weight: normal;">Perform</b>
method and <b style="mso-bidi-font-weight: normal;">ProgressChanged</b> event </li>
</ul>
<div class="MsoNormal">
<br />
<b>Perform</b> Method:</div>
<div class="MsoNormal">
Here is a perform method that we define in our previous
example.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span>
<span style="color: blue;">void</span> Perform(<span style="color: blue;">string</span>[]
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span>
(<span style="color: blue;">string</span> file <span style="color: blue;">in</span>
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">//do
something......</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Thread</span>.Sleep(100);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal">
Now we modify it such way, that it reports progress of task
to client code and processed files.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span>
<span style="color: blue;">void</span> Perform(<span style="color: blue;">string</span>[]
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>
<b style="mso-bidi-font-weight: normal;">counter</b> = 0;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span>
(<span style="color: blue;">string</span> file <span style="color: blue;">in</span>
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">//do
something......</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Thread</span>.Sleep(100);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> counter++;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>
<b style="mso-bidi-font-weight: normal;">percentage</b> = (100 * counter) /
files.Length;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <b style="mso-bidi-font-weight: normal;">ReportProgress</b>(percentage, file);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
After processing file, we compute percentage in <b style="mso-bidi-font-weight: normal;">percentage</b> and paases it as an argument in <b style="mso-bidi-font-weight: normal;">ReportProgress</b>() method, which is an example of how to pass
intermediate result to client code.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">ProgressChanged</b> event:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
This event raised whenever asynchronous method wants to report
progress to client code.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">ProgressChangedEventHandler</span>
ProgressChanged</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">add</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.progressChangedEventHandler
+= <span style="color: blue;">value</span>;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">remove</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.progressChangedEventHandler
-= <span style="color: blue;">value</span>;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span>
<span style="color: #2b91af;">ProgressChangedEventHandler</span>
progressChangedEventHandler;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span>
<span style="color: blue;">virtual</span> <span style="color: blue;">void</span>
OnProgressChanged(<span style="color: #2b91af;">ProgressChangedEventArgs</span>
e)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(progressChangedEventHandler != <span style="color: blue;">null</span>)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> progressChangedEventHandler(<span style="color: blue;">this</span>, e);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">Handling ProgressChanged</b> event in client code:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
Register and implement handler of ProgressChanged event.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> t.ProgressChanged += <span style="color: blue;">new</span> <span style="color: #2b91af;">ProgressChangedEventHandler</span>(task_ProgressChange);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">void</span> task_ProgressChange(<span style="color: blue;">object</span>
sender, <span style="color: #2b91af;">ProgressChangedEventArgs</span> e)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> System.Diagnostics.<span style="color: #2b91af;">Debug</span>.Print(<span style="color: #a31515;">"[Task]
Progress: {0} %, Current File: {1}"</span>, e.ProgressPercentage,
e.UserState);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">ReportProgress</b> method:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
This method calls ProgressChanged event through AsyncOperation
object.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
ReportProgress(<span style="color: blue;">int</span> percentProgress, <span style="color: blue;">object</span> userState)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">// FIXME:
verify the expected behavior</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(!IsBusy)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span>;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> async.Post(<span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> o)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">ProgressChangedEventArgs</span>
e = o <span style="color: blue;">as</span> <span style="color: #2b91af;">ProgressChangedEventArgs</span>;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> OnProgressChanged(e);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> },</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">new</span>
<span style="color: #2b91af;">ProgressChangedEventArgs</span>(percentProgress,
userState));</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<b>In this series</b></div>
<ol style="text-align: left;">
<li><b> </b><a href="http://himanshumbri.blogspot.in/2012/04/asynchronous-method-in-c-part-i.html" target="_blank">Asynchronous method in C#: Part - I</a> </li>
<li>Asynchronous method in C#: Part - II. </li>
</ol>
<div class="MsoNormal">
<br /></div>
</div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-72088896965943555532012-04-21T02:38:00.001-07:002012-04-21T02:59:53.926-07:00Asynchronous method in C# - Part I<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>Introduction</b><br />
<div class="MsoNormal">
In this article, we’ll demonstrate how to write asynchronous
method. For better understanding first we’ll perform task conventionally and
after that we’ll create its Async version.<br />
</div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Objective</b></div>
<div class="MsoNormal">
To write Asynchronous method.<br />
</div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Using the code</b></div>
<div class="MsoNormal">
Let first design class say <i style="mso-bidi-font-style: normal;">Task</i> and write a Perform() method that iterate loops over some
collection.<br />
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Task</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span>
<span style="color: blue;">void</span> Perform(<span style="color: blue;">string</span>[]
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span>
(<span style="color: blue;">string</span> file <span style="color: blue;">in</span>
files)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">//do
something......</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Thread</span>.Sleep(100);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span><br />
</div>
<div class="MsoNormal">
See above example in which we are iterating loop for array
of string. This is convention method which we all normaly write.</div>
<div class="MsoNormal">
Now, let’s try to perform same task asynchronously using delegates.
For this we need following member: </div>
<div class="MsoNormal">
<ul style="text-align: left;">
<li><b style="mso-bidi-font-weight: normal;">Perform</b>() method
– does actual work.</li>
<li><b style="mso-bidi-font-weight: normal;">PerformAsync</b>()
method – invokes the asynchronous operation.</li>
<li><b style="mso-bidi-font-weight: normal;">IsBusy</b> property –
indicates that asynchronous operation is going on.</li>
<li><b style="mso-bidi-font-weight: normal;">CompleteTask</b>()
method – called when perform method completed operation.</li>
<li><b style="mso-bidi-font-weight: normal;">TaskCompleted</b>
event – notifies of the asynchronous operation completion.</li>
</ul>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Perform()</b> Method:</div>
<div class="MsoNormal">
This method executes in background and does actual task. In
our example it iterates loop over array of string.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">private</span> <span style="color: blue;">void</span> Perform(<span style="color: blue;">string</span>[] files)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> file <span style="color: blue;">in</span> files)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: green;">//do something......</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af;">Thread</span>.Sleep(100);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">delegate</span> <span style="color: blue;">void</span> <span style="color: #2b91af;">TaskEventHandler</span>(<span style="color: blue;">string[]</span>
files, <span style="color: #2b91af;">AsyncOperation</span> async, <span style="color: #2b91af;">SendOrPostCallback</span> callback);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">PerformAsync()</b> Method:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
This method invokes asynchronous operation and returns immediately.
If asynchronous operation is running then throw an exception.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">public</span> <span style="color: blue;">void</span> PerformAsync(<span style="color: blue;">string</span>[] files)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">if</span>
(IsBusy)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"The
background worker is busy."</span>);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
async = <span style="color: #2b91af;">AsyncOperationManager</span>.CreateOperation(<span style="color: blue;">this</span>);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af;">TaskEventHandler</span> handler =</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">new</span> <span style="color: #2b91af;">TaskEventHandler</span>(ProcessWorker);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
handler.BeginInvoke(files, async, CompleteWorker, <span style="color: blue;">null</span>, <span style="color: blue;">null</span>);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
First it checks for busy status of operation, if it’s so then throw
InvalidCastOperationException.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
Next an <b style="mso-bidi-font-weight: normal;">AsyncOperation</b> is
created. This object is used by worker thread to invoke client’s event handler
on proper thread.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
Next <b style="mso-bidi-font-weight: normal;">TaskEventHandler</b> is
registered with proper handler method. Through <b style="mso-bidi-font-weight: normal;">TaskEventHandler</b>, we can invoke our Task asynchronously.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
Now, the asynchronous operation is started in separate thread using <b style="mso-bidi-font-weight: normal;">handler.BeginInvoker</b> method. <b style="mso-bidi-font-weight: normal;">BeginInvoke</b> method contains two more
parameter along with string array.</div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; text-autospace: none;">A delegate of type <b style="mso-bidi-font-weight: normal;">SendOrPostCallback</b> to a callback
method which called when operation finishes.</li>
<li class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; text-autospace: none;">A custom object that is
stored in the <b style="mso-bidi-font-weight: normal;">AsyncState</b>
property of an <b style="mso-bidi-font-weight: normal;">IAsyncResult</b> instance return by <b style="mso-bidi-font-weight: normal;">BeginInvoke</b> method.</li>
</ol>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">CompleteTask</b> Method:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
This method calls when worker invoker has finishes it’s processes.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">void</span> CompleteTask(<span style="color: blue;">object</span> state)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">object</span>[] args = (<span style="color: blue;">object</span>[])state;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af;">RunWorkerCompletedEventArgs</span> e =</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
args[0] <span style="color: blue;">as</span> <span style="color: #2b91af;">RunWorkerCompletedEventArgs</span>;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af;">AsyncOperation</span> async = args[1] <span style="color: blue;">as</span> <span style="color: #2b91af;">AsyncOperation</span>;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af;">SendOrPostCallback</span> callback = <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> darg)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
OnRunWorkerCompleted(darg
<span style="color: blue;">as</span> <span style="color: #2b91af;">RunWorkerCompletedEventArgs</span>);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
};</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
async.PostOperationCompleted(callback, e);</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">this</span>.async = <span style="color: blue;">null</span>;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
First the <b style="mso-bidi-font-weight: normal;">AsyncOperation</b>
object is obtained and at last <b style="mso-bidi-font-weight: normal;">TaskCompleted</b>
event fired through <b style="mso-bidi-font-weight: normal;">AsyncOperation</b>
object</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<b style="mso-bidi-font-weight: normal;">TaskCompleted</b> Event:</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
This event fired when the asynchronous operation completed.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">RunWorkerCompletedEventHandler</span> TaskCompleted</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">add</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">this</span>.taskCompletedEventHandler += <span style="color: blue;">value</span>;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">remove</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">this</span>.taskCompletedEventHandler -= <span style="color: blue;">value</span>;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
}</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">private</span> <span style="color: #2b91af;">RunWorkerCompletedEventHandler</span>
taskCompletedEventHandler;</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">protected</span> <span style="color: blue;">virtual</span> <span style="color: blue;">void</span> OnTaskCompleted(<span style="color: #2b91af;">RunWorkerCompletedEventArgs</span>
e)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
{</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue;">if</span> (taskCompletedEventHandler != <span style="color: blue;">null</span>)</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
taskCompletedEventHandler(<span style="color: blue;">this</span>, e);</div>
<span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%;"> }</span><br />
<br />
<span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%;"><b>In this Series</b></span><br />
<ol style="text-align: left;">
<li><span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%;">Asynchronous method in C# - Part I</span></li>
<li><span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%;"><a href="http://himanshumbri.blogspot.in/2012/04/asynchronous-methods-in-c-part-ii.html" target="_blank">Asynchronous method in C# - Part II</a> </span></li>
</ol>
</div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-27788568602881847682012-04-13T02:19:00.001-07:002012-04-18T21:09:53.146-07:00Singleton using C#<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<div class="MsoNormal"><b style="mso-bidi-font-weight: normal;">Inreoduction</b></div><div class="MsoNormal">This article intended to give brief overview of singleton pattern. Main intent to define single tone pattern is to ensure a class has only one instance and provide global point of access to it. One may think, this is not big deal, we can achieve same thing by declaring static class. Global static class solves one portion of global accessibility, but does nothing to ensure that single instance of a class running at a time. In singleton, responsibility of having single instance at a time should in a class itself not on the user class. User class should free from this responsibility.</div><div class="MsoNormal"><span style="height: 31px; margin-left: 103px; margin-top: 29px; position: absolute; width: 82px; z-index: -3;"> </span></div><table cellpadding="0" cellspacing="0"><tbody>
<tr> <td bgcolor="white" height="31" style="background: white; border: .75pt solid white; vertical-align: top;" width="82"><span style="position: absolute; z-index: -3;"> </span><br />
<table cellpadding="0" cellspacing="0"><tbody>
<tr> <td><div class="shape" style="padding: 4.35pt 7.95pt 4.35pt 7.95pt;"><div align="center" class="MsoNormal" style="text-align: center;"><b style="mso-bidi-font-weight: normal;">User</b></div></div></td> </tr>
</tbody></table></td> </tr>
</tbody></table><b style="mso-bidi-font-weight: normal;">Logical Model</b><br />
<br />
<div class="MsoNormal" style="tab-stops: center 3.25in;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqjvQOK6TJZb2YKJobzg_7vo7y4rA37JntinHtNa4_5OCqf2lo0CbLa8yt-F7e0fxyROzOZHrMOaEB8IebKjedVAKI87MVfPLNELOn5mD8VdKLFzMRSR-DPGPLRPw05SwDj8RzubQjAuM/s1600/fig1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqjvQOK6TJZb2YKJobzg_7vo7y4rA37JntinHtNa4_5OCqf2lo0CbLa8yt-F7e0fxyROzOZHrMOaEB8IebKjedVAKI87MVfPLNELOn5mD8VdKLFzMRSR-DPGPLRPw05SwDj8RzubQjAuM/s1600/fig1.JPG" /></a></div> </div><div class="MsoNormal">As shown above, Singleton model is straight forward for understand. There is only one singleton instance. Client can access that instance through single well-known access point.</div><div class="MsoNormal" style="tab-stops: center 3.25in;"><br />
</div><div class="MsoNormal"><b>Physical Model</b></div><div class="MsoNormal"><br />
</div><div class="MsoNormal" style="tab-stops: center 3.25in;">Physical model of singleton is also as simple as logical one. There are many different ways that singleton have implemented over time. Simple UML of GoF singleton is shown below</div><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ZRwiVPkszDt3Cro5_QJWKRgPCNPgyvmUmVlnNe1_DGLWq54sMj7S-foVuiM4jl69vKcC1PkiFbHARRMwjZ4vbFwE7O_un1mX7BX8yYIm5rkdW3OWzelrtaBtRLMTaqCp3qVTvdqu6Dw/s1600/fig2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="171" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ZRwiVPkszDt3Cro5_QJWKRgPCNPgyvmUmVlnNe1_DGLWq54sMj7S-foVuiM4jl69vKcC1PkiFbHARRMwjZ4vbFwE7O_un1mX7BX8yYIm5rkdW3OWzelrtaBtRLMTaqCp3qVTvdqu6Dw/s640/fig2.JPG" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikxpEHJivQg9XMkZEMqHqkQX9IB1syCfvkVaBDWX3jJtPNMkeurf0q2UzhN5vCid0_BNC42VFBMFO1eTgx6n85jEoBOfj9x0uZu3Mevqd_9V02U4Lr9Rk5P1rD1xQ_Bq4kj6WlQo_0QvQ/s1600/fig2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br />
</a></div><br />
<br />
<div class="MsoNormal"><b>Using Code</b></div><div class="MsoNormal" style="tab-stops: center 3.25in;"><br />
</div><div class="MsoNormal">Let’s understand singleton by defining a simple class as below:</div><div class="MsoNormal"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">MySingleton</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">private</span> MySingleton() { }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">MySingleton</span> CreateInstance()</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">lock</span> (<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">MySingleton</span>))</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> singleton = <span style="color: blue;">new</span> <span style="color: #2b91af;">MySingleton</span>();</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">return</span> singleton;</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue;">static</span> <span style="color: #2b91af;">MySingleton</span> singleton = <span style="color: blue;">null</span>;</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }<span style="font-family: Consolas; font-size: 9.5pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="tab-stops: center 3.25in;">As shown above we’ve defined singleton class <i style="mso-bidi-font-style: normal;">MySingleton</i>. We can see here we define constructor as private constructor that means no one can create instance of this class explicitly. Only way for creating instance is static method <i style="mso-bidi-font-style: normal;">CreateInstance().</i> While creating instance it check’s wether instance has been created before, if not then will create new instance else use previous one. Thus our class will handle responsibility of having single instance at a time. It seems good but may cause a problem in multi-threaded environment. If two threads manage to enter in control block at a same time, two instance of member variable could be created. To solve this one may put control block into crtical section to ensure thread safety, but by doing this each call for instance method will be serialized and cause negative impact on performance. To solve this we’ve to make little change in <i style="mso-bidi-font-style: normal;">CreateInstance()</i> method.</div><div class="MsoNormal"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">MySingleton</span> CreateInstance()</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">lock</span> (<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">MySingleton</span>))</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> singleton = <span style="color: blue;">new</span> <span style="color: #2b91af;">MySingleton</span>();</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">return</span> singleton;</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }<span style="font-family: Consolas; font-size: 9.5pt;"></span></div><div class="MsoNormal" style="tab-stops: center 3.25in;"><br />
</div><div class="MsoNormal" style="tab-stops: center 3.25in;">Technique we used above called <i style="mso-bidi-font-style: normal;">“Double Check Lock”</i>. In this technique we didn’t introduce any new declaration or use any new variable, just we tack advantage of critical section here. The double check occurs at the first IF block. If the member variable is null, then the execution enters a critical section block where the member variable is double checked again. Only after passing this last test is the member variable instantiated. The general thinking is that there is no way that two threads can create two instances of the class using this technique. Also, since there is no thread blocking at the first check, most calls to this method would not get the performance hit of having to enter the lock.</div><div class="MsoNormal" style="tab-stops: center 3.25in;">Another way to attempt to fix this problem could be using the <code><span style="font-size: 10pt; line-height: 115%;">volatile</span></code> keyword on the member variable declaration. This should tell the compiler to not reorder the code and forgo optimization. So our final code will be:</div><div class="MsoNormal"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">MySingleton</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">private</span> MySingleton() { }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">MySingleton</span> CreateInstance()</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">lock</span> (<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">MySingleton</span>))</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> {</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">if</span> (singleton == <span style="color: blue;">null</span>)</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> singleton = <span style="color: blue;">new</span> <span style="color: #2b91af;">MySingleton</span>();</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">return</span> singleton;</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">volatile</span> <span style="color: #2b91af;">MySingleton</span> singleton = <span style="color: blue;">null</span>;</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"> }</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><b style="mso-bidi-font-weight: normal;">Conclusion</b></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">The Singleton design pattern is a very useful mechanism for providing a single point of object access in an object-oriented application. Regardless of the implementation used, the pattern provides a commonly understood concept that can be easily shared among design and development teams. However, as we have discovered, it is also important to note how different these implementations can be and their potential side effects. The .NET Framework goes a long way to help an implementer of a pattern design the type of functionality desired without having to deal with many of the side effects discussed in this article. The validity of the pattern's original intent is proven when implemented properly.<br />
<br />
<b>References</b><br />
To know more about this pattern kindly follow following URLs<br />
<br />
<div style="font-weight: normal; text-align: left;"><b>1) </b><a href="http://www.yoda.arachsys.com/csharp/singleton.html" target="_blank">Implementing the Singleton Pattern in C#</a></div><div class="title" style="font-weight: normal; text-align: left;"><b>2)</b> <a href="http://msdn.microsoft.com/en-us/library/ff650316.aspx" target="_blank">Implementing Singleton in C#</a></div><div style="font-weight: normal; text-align: left;"><b>3)</b> <a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/19/c-system.lazylttgt-and-the-singleton-design-pattern.aspx" target="_blank">C#: System.Lazy<T> and the Singleton Design Pattern</a></div></div><div class="MsoNormal" style="tab-stops: center 3.25in;"><br />
</div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-56803444668322480432012-04-09T02:41:00.000-07:002012-04-09T02:41:27.465-07:00ABC of C# Iterator Pattern<div dir="ltr" style="text-align: left;" trbidi="on"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style> <![endif]--> <br />
<div class="MsoNormal"><b style="mso-bidi-font-weight: normal;">Introduction</b></div><div class="MsoNormal">The aim of this alternative tip is to give more relevant information to the beginner as well as why the heck one should bother about iterators at all.</div><div class="MsoNormal">Lets start with that: why to bother what the iterator pattern is? You use the iterator pattern most likely in your every day work maybe without being aware of:</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">IList</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><<span style="color: blue;">string</span>> names = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>() { <span style="color: #a31515;">"Himanshu"</span>, <span style="color: #a31515;">"Hetal"</span>, <span style="color: #a31515;">"Viral"</span> };</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (<span style="color: blue;">string</span> name <span style="color: blue;">in</span> names)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">Console</span>.Write(<span style="color: #a31515;">"Name : {0}"</span>, name);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">T</span>he iterator tells the <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">foreach </span></code>loop in what sequence you get the elements.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><b style="mso-bidi-font-weight: normal;">Using<span style="mso-spacerun: yes;"> </span>Code</b></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">A class that can be used in a <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">foreach</span></code> loop must provide a <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator<T> GetEnumerator() { ... }</span></code> method. The method name is reserved for that purpose. This function defines in what sequence the elements are returned.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">Some classes may also provide the non-generic <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator GetEnumerator() { ... }</span></code> method. This is from the older days where there were no generics yet, e.g. all non-generic collections like Array, etc. provide only that "old-fashioned" iterator function.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">Behind the scenes, the foreach loop</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (<span style="color: blue;">string</span> name <span style="color: blue;">in</span> names) { ... }</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">translates into:</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none; text-indent: .5in;"><b style="mso-bidi-font-weight: normal;">Explicit Generic Version<span style="mso-tab-count: 5;"> </span>Explicit non-generic version</b></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (<span style="color: blue;">var</span> it = names.GetEnumerator())<span style="mso-tab-count: 2;"> </span><span style="color: blue;">var</span> it = names.GetEnumerator()</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">while</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (it.MoveNext())<span style="mso-tab-count: 4;"> </span><span style="color: blue;">while</span> (it.MoveNext())</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{<span style="mso-tab-count: 7;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">string</span> name = it.Current;<span style="mso-tab-count: 3;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)it.Current;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>....<span style="mso-tab-count: 6;"> </span><span style="mso-spacerun: yes;"> </span>....</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}<span style="mso-tab-count: 7;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">the two explicit iterator calls can be combined into one:</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">var</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> it = names.GetEnumerator()</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (it <span style="color: blue;">as</span> <span style="color: #2b91af;">IDisposable</span>)<span style="color: #2b91af;"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">while</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (it.MoveNext())</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">string</span> name = it.Current;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>....</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">So, the core of the C# implementation of the Iterator Pattern is the <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">GetEnumerator()</span></code> method. What are now these <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator</span></code>/<code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator<T></span></code> interfaces?</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><b style="mso-bidi-font-weight: normal;">What’s an iterator?</b></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">An iterator provides a means to iterate (i.e. loop) over some items. The sequence of elements is given by the implementations of the <code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator</span></code>/<code><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">IEnumerator<T></span></code> interfaces:</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Collections</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IEnumerator</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">object</span> Current { <span style="color: blue;">get</span>; }</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">bool</span> MoveNext();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">void</span> Reset();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Collections.Generic</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IEnumerator</span><<span style="color: blue;">out</span> T> : <span style="color: #2b91af;">IDisposable</span>, <span style="color: #2b91af;">IEnumerator</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>T Current { <span style="color: blue;">get</span>; }</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">The pattern is basically given by </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">MoveNext()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> and </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">Current</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">. The semantics is that one has to first call </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">MoveNext()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> to get to the first element. If </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">MoveNext()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> returns </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">false</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">, then there is no more element. </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">Current</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> returns the current element. You are not supposed to call </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">Current</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> if the preceeding </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">MoveNext()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> returned false. </span><br />
<span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">The </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">MoveNext()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> gives the next element in the sequence of elements - what ever that sequence is, e.g. from first to last, or sorted by some criteria, or random, etc. </span><br />
<span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">You know now how to apply the iterator pattern (e.g. in a </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">foreach</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> loop) and that this is possible for all classes that provide the above mentioned </span><code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">GetEnumerator()</span></code><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> method (the iterator).</span> <br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">What is IEnumerable/IEnumerable<> for?</span></b><br />
<div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">These interfaces are quite simple: </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"> </span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">System.Collections</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IEnumerable</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">IEnumerator</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"> </span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">GetEnumerator();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Collections.Generic</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IEnumerable</span><<span style="color: blue;">out</span> T> : <span style="color: #2b91af;">IEnumerable</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">IEnumerator</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><T> GetEnumerator();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">So, easy answer: they provide an iterator (one "old-fashioned", one with generics). </span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">A class that implements one of these interfaces provides an iterator implementation. Furthermore, such an instance can be used wherever one of these interface is needed.</span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>Note: it is <b>not</b> required to implement this interface to have an iterator: one can provide its </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">GetEnumerator()</span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> method without implementing this interface. But in such a case, one can not pass the class to a method where </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">IEnumerable<T></span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> is to be passed. </span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">E.g. there is a </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">List<T></span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> constructor that takes an </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">IEnumerable<T></span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> to initialize its content from that iterator.</span><span style="font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Collections.Generic</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">List</span><T> : <span style="color: #2b91af;">IList</span><T>, <span style="color: #2b91af;">ICollection</span><T>, <span style="color: #2b91af;">IEnumerable</span><T>, <span style="color: #2b91af;">IList</span>, <span style="color: #2b91af;">ICollection</span>, <span style="color: #2b91af;">IEnumerable</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> List(<span style="color: #2b91af;">IEnumerable</span><T> collection);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">If you look now at the LINQ extension methods: many of these base on </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">IEnumerable<T></span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">, thus, extending any iterator class by some new function that often return yet another iterator. E.g</span><span style="font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">. </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Linq</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Enumerable</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span><TResult> Select<TSource, TResult>(</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: 3.5in; margin-right: 0in; margin-top: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">this</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">IEnumerable</span><TSource> source, <span style="color: #2b91af;">Func</span><TSource, TResult> selector);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">public</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span><TSource> Where<TSource>(</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: 3.5in; margin-right: 0in; margin-top: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">this</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> <span style="color: #2b91af;">IEnumerable</span><TSource> source, <span style="color: #2b91af;">Func</span><TSource, <span style="color: blue;">bool</span>> predicate);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">This is used as: </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">List</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><<span style="color: blue;">string</span>> list =</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"> ...;</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">var</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> query = <span style="color: blue;">from</span> s <span style="color: blue;">in</span> list <span style="color: blue;">where</span> s.Length > 2 <span style="color: blue;">select</span> s;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> (<span style="color: blue;">string</span> s <span style="color: blue;">in</span> query)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">And again, the C# language provides an alterantive way to express this (one could say simpler): </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">List</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><<span style="color: blue;">string</span>> list =</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"> ...;</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">var</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> query = <span style="color: blue;">from</span> s <span style="color: blue;">in</span> list <span style="color: blue;">where</span> s.Length > 2 <span style="color: blue;">select</span> s;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> s <span style="color: blue;">in</span> query)</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>...</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span><span lang="CS" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: CS; mso-fareast-font-family: "Times New Roman";"></span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">This is LINQ - Language Integrated Queries: Extension methods that can be expressed in the form </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">from ... in ... where ... select</span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> (to show some of the LINQ keywords). Please note that you can always write a LINQ expression as a chain of extension methods as shown above. </span></div><div class="MsoNormal" style="line-height: normal; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">So, now you know the benefits of the </span><span style="mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">IEnumerable<T></span><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> interfaces and where and how they are used.</span><span style="font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal"><br />
</div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-78091221296397304232012-04-09T01:30:00.000-07:002012-04-09T01:30:15.082-07:00How to encrypt/decrypt Query string<div dir="ltr" style="text-align: left;" trbidi="on"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
</style> <![endif]--> <br />
<div class="MsoNormal"><b style="mso-bidi-font-weight: normal;">Introduction</b></div><div class="MsoNormal">In our development routine many time we need to pass information from one page to another. Most popular way to do this is to pass <i style="mso-bidi-font-style: normal;">Query String </i>along with url. But as we all know, it’s not safe as it’s visible to user. Here is a solution.</div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><b style="mso-bidi-font-weight: normal;">Objective</b></div><div class="MsoNormal">Pass information through <i style="mso-bidi-font-style: normal;">Query String</i> after encode them</div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><b style="mso-bidi-font-weight: normal;">Using the Code</b></div><div class="MsoNormal">In this example I developed a class, that contains method for encrypting and decrypting <i style="mso-bidi-font-style: normal;">Query string</i>.</div><div class="MsoNormal">I’m using <b style="mso-bidi-font-weight: normal;">TripleDES</b> algorithm which using <b style="mso-bidi-font-weight: normal;">MD5</b> generated hash as a sault. Code for same is given below.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// The Querystring to encrypt.</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">string</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> Msg = Request.QueryString;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">string</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> Password = <span style="color: #a31515;">"Pa5sw0rd"</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">string</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> EncryptedString = <span style="color: #2b91af;">MySample</span>.EncryptString(Msg, Password);<span style="color: #2b91af;"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="mso-bidi-font-family: Consolas;">In the EncryptString function we apply the TripleDES algorithm with a 128 bit key. But first we need to turn the above passphrase (‘Pa5sw0rd’) into a 128 bit key. One useful coincidence is that the MD5 hash algorithm accepts a set of bytes of any length and turns them into a 128 bit hash. So by running the password through the MD5 hashing algorithm we create our key.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// Step 1. We hash the passphrase using MD5</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// We use the MD5 hash generator as the result is a 128 bit byte array</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// which is a valid length for the TripleDES encoder we use below</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">MD5CryptoServiceProvider</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> HashProvider = <span style="color: blue;">new</span> <span style="color: #2b91af;">MD5CryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">byte</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="mso-bidi-font-family: Consolas;">The TripleDES algorithm itself turns a byte array into an encrypted byte array. So we first need to convert our C# message string (which is Unicode encoded) into a byte array through the System.Text.UTF8Encoding encoder.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="mso-bidi-font-family: Consolas;">The key is used to initialize the TripleDES algorithm. In addition we need to specify that we will only encode something once (CipherMode.ECB) and because its unlikely that our source string fits into a single TripleDES block we need to specify how we want to pad any remaining bytes (PaddingMode.PKCS7).</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// Step 2. Create a new TripleDESCryptoServiceProvider object</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">TripleDESCryptoServiceProvider</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> TDESAlgorithm = <span style="color: blue;">new</span> <span style="color: #2b91af;">TripleDESCryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">// Step 3. Setup the encoder</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">TDESAlgorithm.Key = TDESKey;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">TDESAlgorithm.Mode = <span style="color: #2b91af;">CipherMode</span>.ECB;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">TDESAlgorithm.Padding = <span style="color: #2b91af;">PaddingMode</span>.PKCS7;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">The encrypted byte array is finally converted into a Base64 encoded string for easy storage. The <em><span style="font-family: "Calibri","sans-serif";">DecryptString</span></em> function is very similar to the encryption function, except that it turns the Base64 encoded encrypted message back into the original UTF8 string.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">Complete code is given below.</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Text;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">using</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> System.Security.Cryptography;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> EncryptStringSample</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">class</span> <span style="color: #2b91af;">MySample</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> EncryptString(<span style="color: blue;">string</span> Message, <span style="color: blue;">string</span> Passphrase)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] Results;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>System.Text.<span style="color: #2b91af;">UTF8Encoding</span> UTF8 = <span style="color: blue;">new</span> System.Text.<span style="color: #2b91af;">UTF8Encoding</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 1. We hash the passphrase using MD5</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// We use the MD5 hash generator as the result is a 128 bit byte array</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// which is a valid length for the TripleDES encoder we use below</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">MD5CryptoServiceProvider</span> HashProvider = <span style="color: blue;">new</span> <span style="color: #2b91af;">MD5CryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 2. Create a new TripleDESCryptoServiceProvider object</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">TripleDESCryptoServiceProvider</span> TDESAlgorithm = <span style="color: blue;">new</span> <span style="color: #2b91af;">TripleDESCryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 3. Setup the encoder</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Key = TDESKey;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Mode = <span style="color: #2b91af;">CipherMode</span>.ECB;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Padding = <span style="color: #2b91af;">PaddingMode</span>.PKCS7;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 4. Convert the input string to a byte[]</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] DataToEncrypt = UTF8.GetBytes(Message);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 5. Attempt to encrypt the string</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">try</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">ICryptoTransform</span> Encryptor = TDESAlgorithm.CreateEncryptor();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">finally</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Clear the TripleDes and Hashprovider services of any sensitive information</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Clear();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>HashProvider.Clear();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 6. Return the encrypted string as a base64 encoded string</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: #2b91af;">Convert</span>.ToBase64String(Results);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> DecryptString(<span style="color: blue;">string</span> Message, <span style="color: blue;">string</span> Passphrase)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] Results;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>System.Text.<span style="color: #2b91af;">UTF8Encoding</span> UTF8 = <span style="color: blue;">new</span> System.Text.<span style="color: #2b91af;">UTF8Encoding</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 1. We hash the passphrase using MD5</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// We use the MD5 hash generator as the result is a 128 bit byte array</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// which is a valid length for the TripleDES encoder we use below</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">MD5CryptoServiceProvider</span> HashProvider = <span style="color: blue;">new</span> <span style="color: #2b91af;">MD5CryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 2. Create a new TripleDESCryptoServiceProvider object</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">TripleDESCryptoServiceProvider</span> TDESAlgorithm = <span style="color: blue;">new</span> <span style="color: #2b91af;">TripleDESCryptoServiceProvider</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 3. Setup the decoder</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Key = TDESKey;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Mode = <span style="color: #2b91af;">CipherMode</span>.ECB;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Padding = <span style="color: #2b91af;">PaddingMode</span>.PKCS7;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 4. Convert the input string to a byte[]</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">byte</span>[] DataToDecrypt = <span style="color: #2b91af;">Convert</span>.FromBase64String(Message);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 5. Attempt to decrypt the string</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">try</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: #2b91af;">ICryptoTransform</span> Decryptor = TDESAlgorithm.CreateDecryptor();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">finally</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Clear the TripleDes and Hashprovider services of any sensitive information</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>TDESAlgorithm.Clear();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>HashProvider.Clear();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: green;">// Step 6. Return the decrypted string in UTF8 format</span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> UTF8.GetString(Results);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">}</span></div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-38395065930235793222012-01-11T21:59:00.000-08:002012-01-11T21:59:42.705-08:00Asynchronous Processing<div dir="ltr" style="text-align: left;" trbidi="on"><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><b style="mso-bidi-font-weight: normal;"><span lang="EN-US" style="font-size: 9pt; line-height: 115%;"><span style="font-family: Calibri;">Introduction</span></span></b></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: Calibri;"><span lang="EN-US" style="font-size: 8pt; line-height: 115%;"><span style="mso-tab-count: 1;"> </span></span><span lang="EN-US">Hi, today we will demonstrate asynchronous processing using delegates and events. One might think what the need of doing work<span style="mso-spacerun: yes;"> </span>asynchronously in windows environment. Yes, it is required. By sample example we will prove this.</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><b style="mso-bidi-font-weight: normal;"><span lang="EN-US" style="font-size: 9pt; line-height: 115%;"><span style="font-family: Calibri;">The Program</span></span></b></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: Calibri;"><span lang="EN-US" style="font-size: 8pt; line-height: 115%;"><span style="mso-tab-count: 1;"> </span></span><span lang="EN-US">In this program we will develop a class will have certain events and delegates to handle those events. Our class will have a function to perform task and continuously changes the status through raising events.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Task</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">delegate</span> <span style="color: blue;">void</span> <span style="color: #2b91af;">UpdateStatusEventHandler</span>(<span style="color: blue;">string</span> text, <span style="color: blue;">int</span> total, <span style="color: blue;">int</span> current);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">delegate</span> <span style="color: blue;">void</span> <span style="color: #2b91af;">UpdateTextEventHandler</span>(<span style="color: blue;">string</span> text);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">delegate</span> <span style="color: blue;">void</span> <span style="color: #2b91af;">UpdateProgressBarEventHandler</span>(<span style="color: blue;">int</span> total, <span style="color: blue;">int</span> value);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">UpdateStatusEventHandler</span> UpdateStatus;</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">UpdateTextEventHandler</span> UpdateText;</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">UpdateProgressBarEventHandler</span> UpdateProgressBar;</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>……….</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-tab-count: 1;"> </span>As shown in code we define 3 events for updating status, text and progress bar value as per task perform. For handling these events we define 3 delegates. As we all knows delegate works asynchronously, so whenever we raise these events, they will execute asynchronously.</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;">This event handled from Windows Form. Code to handle these events are as follows.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: #2b91af; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>Task</span><span lang="EN-US" style="mso-no-proof: yes;"> task = <span style="color: blue;">new</span> <span style="color: #2b91af;">Task</span>();</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>task.UpdateProgressBar += <span style="color: blue;">new</span> <span style="color: #2b91af;">Task</span>.<span style="color: #2b91af;">UpdateProgressBarEventHandler</span>(task_UpdateProgressBar);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>task.UpdateStatus += <span style="color: blue;">new</span> <span style="color: #2b91af;">Task</span>.<span style="color: #2b91af;">UpdateStatusEventHandler</span>(task_UpdateStatus);</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>task.UpdateText += <span style="color: blue;">new</span> <span style="color: #2b91af;">Task</span>.<span style="color: #2b91af;">UpdateTextEventHandler</span>(task_UpdateText);</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;">in order to handle these events we’ve to register them first. To register them first create instance of class which owns those events. As you can see delegates which we defined in that class are working as event handler and each event handler has reference of a function having code for handling this events.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>private</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">void</span> task_UpdateStatus(<span style="color: blue;">string</span> text, <span style="color: blue;">int</span> total, <span style="color: blue;">int</span> current)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>eUpdateStatus(lblStatus, text, total, current);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> task_UpdateText(<span style="color: blue;">string</span> text)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>eUpdateText(lblText, text);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> task_UpdateProgressBar(<span style="color: blue;">int</span> total, <span style="color: blue;">int</span> value)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>eUpdateProgressBar(pbTask, total, value);</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;">Now let us see how to update windows contron state asynchronously. For that you have to define delegates. As we knows delegates are special typed class which can point function having same signature. We’ll define delegates and functions that work asynchronouly for us.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>private</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">delegate</span> <span style="color: blue;">void</span> <span style="color: #2b91af;">delUpdateStatus</span>(<span style="color: #2b91af;">Label</span> lbl, <span style="color: blue;">string</span> text, <span style="color: blue;">int</span> total, <span style="color: blue;">int</span> current);<span style="mso-spacerun: yes;"> </span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> mUpdateStatus(<span style="color: #2b91af;">Label</span> lbl, <span style="color: blue;">string</span> text, <span style="color: blue;">int</span> total, <span style="color: blue;">int</span> current)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span> (lbl.InvokeRequired)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>lbl.BeginInvoke(<span style="color: blue;">new</span> <span style="color: #2b91af;">delUpdateStatus</span>(mUpdateStatus1), lbl, text, total, current);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> mUpdateStatus1(<span style="color: #2b91af;">Label</span> lbl, <span style="color: blue;">string</span> text, <span style="color: blue;">int</span> total, <span style="color: blue;">int</span> current)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>{</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>lbl.Text = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0}/{1}\n {2}"</span>, current, total, text);</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>lbl.Update();</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>lbl.Parent.Update();</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">this</span>.Update();</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>}</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;">As we shown above we defined delegate and its function for updating label status. Here you notice BeginInvoke method which execute asynchronously for updating control status</span></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFLvyw9J6Yahsf7-ki-0MEV3_IdWXGyddbEz8UCneJIqYqcAac1y5vrY0pjWK5ljodTYd6gOlJ2BsbzuzPBL3HbJDq6RCeYdgOwt6A2QSuF1b6tl4qQIO-BrhuPU1Uwc4j3Xtbi5wtIUE/s1600/Hmanjarawala_8746_a1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="232" kba="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFLvyw9J6Yahsf7-ki-0MEV3_IdWXGyddbEz8UCneJIqYqcAac1y5vrY0pjWK5ljodTYd6gOlJ2BsbzuzPBL3HbJDq6RCeYdgOwt6A2QSuF1b6tl4qQIO-BrhuPU1Uwc4j3Xtbi5wtIUE/s320/Hmanjarawala_8746_a1.jpg" width="320" /></a></div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVPmd6Ge_je2Y5Hy05aYavZu1uHGM8By6NinzeZd8MxwojxDFefvrKsu_BkhLPlEZa1ufHTCuTsPc4SfgnWdfm5XtTplgBdRpsXVzJZC-yW1UrOWd8uTcNaczF3l7xie8qRiWKtF6rSwo/s1600/Hmanjarawala_4488_a2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="197" kba="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVPmd6Ge_je2Y5Hy05aYavZu1uHGM8By6NinzeZd8MxwojxDFefvrKsu_BkhLPlEZa1ufHTCuTsPc4SfgnWdfm5XtTplgBdRpsXVzJZC-yW1UrOWd8uTcNaczF3l7xie8qRiWKtF6rSwo/s320/Hmanjarawala_4488_a2.jpg" width="320" /></a></div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil6SNz4m8hVTpQger9lS01KQjbwPTMaRFUEIektqbVoYVIRQ5ogWqDclr7ZEwAWMd9h9W0u5rUz8ZmFtqPe2L3syR65_3zKpvyYFKWjMi8kOXe1rSSVi-cw4_vinfn-cJXrVFWwJjVJ1E/s1600/Hmanjarawala_3414_a3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" kba="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil6SNz4m8hVTpQger9lS01KQjbwPTMaRFUEIektqbVoYVIRQ5ogWqDclr7ZEwAWMd9h9W0u5rUz8ZmFtqPe2L3syR65_3zKpvyYFKWjMi8kOXe1rSSVi-cw4_vinfn-cJXrVFWwJjVJ1E/s1600/Hmanjarawala_3414_a3.jpg" /></a></div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-11249047045002584192011-10-18T22:48:00.001-07:002011-10-19T07:38:27.016-07:00What is Mock Objects?<div dir="ltr" style="text-align: left;" trbidi="on"><sup><span style="font-family: "Calibri", "sans-serif"; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><span style="font-size: large;">Mock objects are objects that are isolated from there dependencies for testing purpose. In other words mock object is a simulated </span><span style="font-size: large;">object</span><span style="font-size: large;"> that mimics the behavior of a real object in controlled ways. Mock objects are often used in </span><span style="font-size: large;"><span style="color: black;">unit</span> testing</span><span style="font-size: large;"> to analyze the performance of actual objects. In this context, an object is the smallest testable part of an </span><span style="font-size: large;">application</span><span style="font-size: large;">. A mock object makes use of the same </span><span style="font-size: large;">interface</span><span style="font-size: large;"> as the element of code it is intended to imitate. </span></span></sup><br />
<span style="font-size: large;"><sup><span style="font-family: "Calibri", "sans-serif"; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">Mock</span></sup><span style="font-family: "Calibri", "sans-serif"; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"> <sup>objects have two roles during a test case: actor and critic.</sup></span></span><br />
<sup><span style="font-family: "Calibri", "sans-serif"; font-size: large; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">The actor behavior is to simulate objects that are difficult to set up or time consuming to set up for a test i.e. <span style="mso-spacerun: yes;"> </span>database connection. Setting up a test database at the start of each test would slow testing to a crawl and would require the installation of the database engine and test data on the test machine. If we can simulate the connection and return data of our choosing we not only win on the pragmatics of testing, but can also feed our code spurious data to see how it responds. We can simulate databases being down or other extremes without having to create a broken database for real. In other words, we get greater control of the test environment. </span></sup><br />
<sup><span style="font-family: "Calibri", "sans-serif"; font-size: large; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">If mock objects only behaved as actors they would simply be known as "server stubs". This was originally a pattern named by Robert Binder (<span style="color: black;"><span style="mso-field-code: " HYPERLINK """;"><span class="MsoHyperlink">Testing object-oriented systems</span></span>:</span> models, patterns, and tools, Addison-Wesley) in 1999. </span></sup><br />
<sup><span style="font-family: "Calibri", "sans-serif"; font-size: large; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">A server stub is a simulation of an object or component. It should exactly replace a component in a system for test or prototyping purposes, but remain lightweight. This allows tests to run more quickly, or if the simulated class has not been written, to run at all. </span></sup><br />
<sup><span style="font-family: "Calibri", "sans-serif"; font-size: large; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">However, the mock objects not only play a part (by supplying chosen return values on demand) they are also sensitive to the messages sent to them (via expectations). By setting expected parameters for a method call they act as a guard that the calls upon them are made correctly. If expectations are not met they save us the effort of writing a failed test assertion by performing that duty on our behalf. </span></sup><br />
<sup><span style="font-family: "Calibri", "sans-serif"; font-size: large; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">In the case of an imaginary database connection they can test that the query, say SQL, was correctly formed by the object that is using the connection. Set them up with fairly tight expectations and you will hardly need manual assertions at all. </span></sup><br />
<span style="font-size: large;"><br />
</span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">A mock object can be useful in place of a real object that: </span></span></sup></div><ul type="disc"><li class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-list: l0 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">Runs slowly or inefficiently in practical situations</span></span></sup></li>
<li class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-list: l0 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">Occurs rarely and is difficult to produce artificially</span></span></sup></li>
<li class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-list: l0 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">Produces non-deterministic results</span></span></sup></li>
<li class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-list: l0 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">Does not yet exist in a practical sense</span></span></sup></li>
<li class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt; mso-list: l0 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"><sup><span style="mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><span style="font-family: Calibri; font-size: large;">Is intended mainly or exclusively for conducting tests</span></span></sup><span style="font-family: "Cambria", "serif"; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman"; mso-hansi-theme-font: major-latin;"></span></li>
</ul></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-84494252070276408242011-10-18T01:38:00.001-07:002011-10-18T01:38:33.334-07:00Query Execution in LinQ<div dir="ltr" style="text-align: left;" trbidi="on"><!-- .entry-meta --><br />
<div class="entry-content">In LINQ, queries have two different behaviors of execution: immediate and deferred. In this article, we will take a quick overview of how Deferred query execution and Immediate Query Execution works in LINQ<br />
<strong>Deferred Query Execution</strong><br />
To understand Deferred Query Execution, let’s take the following example which declares some Employees and then queries all employees with Age > 28:<br />
<br />
<span style="color: blue;">class</span> <span style="color: #2b91af;">Employee</span><br />
<br />
{<br />
<br />
<span style="color: blue;">public</span> <span style="color: blue;">int</span> ID { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<br />
<br />
<span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<br />
<br />
<span style="color: blue;">public</span> <span style="color: blue;">int</span> Age { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<br />
<br />
}<br />
<br />
<span style="color: blue;">static</span> <span style="color: blue;">void</span> main(<span style="color: blue;">string</span>[] args)<br />
{<br />
<span style="color: blue;">var</span> empList = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>(<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>[]<br />
{<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=1, Name=<span style="color: #a31515;">“Himanshu”</span>, Age=<span style="color: #a31515;">“30″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=2, Name=<span style="color: #a31515;">“Rahul”</span>, Age=<span style="color: #a31515;">“35″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=3, Name=<span style="color: #a31515;">“Hetal”</span>, Age=<span style="color: #a31515;">“26″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=4, Name=<span style="color: #a31515;">“Varsha”</span>, Age=<span style="color: #a31515;">“28″</span>},<br />
});<br />
<span style="color: blue;">var</span> lst = <span style="color: blue;">from</span> e <span style="color: blue;">in</span> empList<br />
<span style="color: blue;">where</span> e.Age > 28 <span style="color: green;">//<= query seems to be executed here</span><br />
<span style="color: blue;">select</span> <span style="color: blue;">new</span> { e.Name };<br />
<span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> emp <span style="color: blue;">in</span> lst)<br />
<span style="color: #2b91af;">Console</span>.WriteLine(emp.Name);<br />
<span style="color: #2b91af;">Console</span>.ReadLine();<br />
}<br />
OUTPUT: Himanshu, Rahul<br />
Looking at the query shown above, it appears that the query is executed at the point where the arrow is pointing towards. However that’s not true. The query is actually executed when the query variable is <em>iterated</em> over, not when the query variable is created. This is called <em>deferred execution</em>.<br />
Now how do we prove that the query was not executed when the query variable was created? It’s simple. Just create another Employee instance <strong><em>after</em></strong><em> the query variable is created</em><br />
<span style="color: blue;">static</span> <span style="color: blue;">void</span> main(<span style="color: blue;">string</span>[] args)<br />
{<br />
<span style="color: blue;">var</span> empList = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>(<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>[]<br />
{<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=1, Name=<span style="color: #a31515;">“Himanshu”</span>, Age=<span style="color: #a31515;">“30″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=2, Name=<span style="color: #a31515;">“Rahul”</span>, Age=<span style="color: #a31515;">“35″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=3, Name=<span style="color: #a31515;">“Hetal”</span>, Age=<span style="color: #a31515;">“26″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=4, Name=<span style="color: #a31515;">“Varsha”</span>, Age=<span style="color: #a31515;">“28″</span>},<br />
});<br />
<span style="color: blue;">var</span> lst = <span style="color: blue;">from</span> e <span style="color: blue;">in</span> empList<br />
<span style="color: blue;">where</span> e.Age > 28 <span style="color: green;">//<= query Variable</span><br />
<span style="color: blue;">select</span> <span style="color: blue;">new</span> { e.Name };<br />
empList.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span> { ID = 5, Name = <span style="color: #a31515;">“Tarun”</span>, Age = <span style="color: #a31515;">“39″</span> }); <span style="color: green;">//<= New employee initialization</span><br />
<span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> emp <span style="color: blue;">in</span> lst)<br />
<span style="color: #2b91af;">Console</span>.WriteLine(emp.Name);<br />
<span style="color: #2b91af;">Console</span>.ReadLine();<br />
}<br />
Notice we are creating a new Employee instance <em>after</em> the query variable is created. Now had the query been executed when the query variable is created, the results would be the same as the one we got earlier, i.e. only two employees would meet the criteria of Age > 28. However the output is not the same<br />
OUTPUT: Himanshu, Rahul, Tarun.<br />
What just happened is that the execution of the query was <em>deferred</em> until the query variable was iterated over in a foreach loop. This allows you to execute a query as frequently as you want to, like fetching the latest information from a database that is being updated frequently by other applications. You will always get the latest information from the database in this case.<br />
<br />
<strong>Immediate Query Execution</strong><strong></strong><br />
You can also force a query to execute immediately, which is useful for caching query results. Let us say we want to display a count of the number of employees that match a criteria.<br />
<span style="color: blue;">static</span> <span style="color: blue;">void</span> main(<span style="color: blue;">string</span>[] args)<br />
{<br />
<span style="color: blue;">var</span> empList = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>(<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>[]<br />
{<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=1, Name=<span style="color: #a31515;">“Himanshu”</span>, Age=<span style="color: #a31515;">“30″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=2, Name=<span style="color: #a31515;">“Rahul”</span>, Age=<span style="color: #a31515;">“35″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=3, Name=<span style="color: #a31515;">“Hetal”</span>, Age=<span style="color: #a31515;">“26″</span>},<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>{ID=4, Name=<span style="color: #a31515;">“Varsha”</span>, Age=<span style="color: #a31515;">“28″</span>},<br />
});<br />
<span style="color: blue;">var</span> lst = (<span style="color: blue;">from</span> e <span style="color: blue;">in</span> empList<br />
<span style="color: blue;">where</span> e.Age > 28<br />
<span style="color: blue;">select</span> e).Count(); <span style="color: green;">//<= Immediate Execution</span><br />
empList.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span> { ID = 5, Name = <span style="color: #a31515;">“Tarun”</span>, Age = <span style="color: #a31515;">“39″</span> });<br />
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">“Total employees whose age is > 28 are {0}”</span>, lst);<br />
<span style="color: #2b91af;">Console</span>.ReadLine();<br />
}<br />
In the query shown above, it order to count the elements that match the condition, the query must be executed, and this is done automatically when Count( ) is called. So adding a new employee instance <em>after</em> the query variable declaration does not have any effect here, as the query is already executed. The output will be 2, instead of 3.<br />
The basic difference between a Deferred execution vs Immediate execution is that Deferred execution of queries produce a sequence of values, whereas Immediate execution of queries return a singleton value and is executed immediately. Examples are using Count(), Average(), Max() etc.</div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-83236639312211926542011-10-15T09:23:00.001-07:002011-10-17T03:35:08.451-07:00Initiate with .Net Remoting<div dir="ltr" style="text-align: left;" trbidi="on"><!-- .entry-meta --><br />
<div class="entry-content"><strong><span style="font-size: large;">Introduction</span></strong><br />
.Net Remoting provides mechanism to communicate with object beyond the application context boundary. This application context known as AppDomain. It is a generic system for different applications to communicate with one another. Object exposes their processes called Remote methods for interprocess communication. Application might be hosted in same network or some remote location, either through intranet or Internet.<br />
.Net remoting provides framework that allow objects to interact with each other across application domain. Remoting was designed in a way that it hides the most difficult aspects like managing connections, marshaling data, and reading and writing XML and SOAP. The framework provides a number of services, including object activation and object lifetime support, as well as communication channels which are responsible for transporting messages to and from remote applications.<br />
<br />
<strong><span style="font-size: large;">Remote Object</span></strong><br />
Any object, outside the application domain of caller application called remote object. Local object that cannot be serialized cannot be passed to different application domain, hence cannot be remote object.<br />
Any object can be changed into a remote object by deriving it from <code><strong><em><span style="font-family: Consolas; font-size: x-small;">MarshalByRefObject</span></em></strong></code>, or by making it serializable either by adding the <code><strong><em><span style="font-family: Consolas; font-size: x-small;">[Serializable]</span></em></strong></code> tag or by implementing the <code><strong><em><span style="font-family: Consolas; font-size: x-small;">ISerializable</span></em></strong></code> interface. When a client activates a remote object, it receives a proxy to the remote object. All operations on this proxy are appropriately indirected to enable the Remoting infrastructure to intercept and forward the calls appropriately. In cases where the proxy and remote objects are in different application domains, all method call parameters on the stack are converted into messages and transported to the remote application domain, where the messages are turned back into a stack frame and the method call is invoked. The same procedure is used for returning results from the method call.<br />
Here is a sample aplication for that. Each remoting application contains three parts:<br />
<ol><li>Client</li>
<li>Remote Object</li>
<li>Server.</li>
</ol><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Interface</span> IObserver</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Function</span> InvokeMethod() <span style="color: blue;">As</span> <span style="color: blue;">String</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Function</span> InvokeMethod(<span style="color: blue;">ByVal</span> number1 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>, <span style="color: blue;">ByVal</span> number2 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>, <span style="color: blue;">ByVal</span> [operator] <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>) <span style="color: blue;">As</span> <span style="color: blue;">String</span></span></span></div><span lang="EN-US" style="color: blue; font-family: "Calibri", "sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-no-proof: yes;">End</span><span lang="EN-US" style="font-family: "Calibri", "sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-no-proof: yes;"> <span style="color: blue;">Interface</span></span><br />
<br />
<span lang="EN-US" style="font-family: "Calibri", "sans-serif"; font-size: 8pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-no-proof: yes;"><span style="color: blue;"></span></span>Lets start with Remote object first. Here is an interface Iobserver that contains methods that’s are shared between the client and the server.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Class</span> RemoteMyObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Inherits</span> MarshalByRefObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Implements</span> IDisposable</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> <span style="color: blue;">New</span>()</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Function</span> Method1() <span style="color: blue;">As</span> <span style="color: blue;">String</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Method1 = Cache.CreateInstance.MyMethod1</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Function</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Function</span> Method2(<span style="color: blue;">ByVal</span> number1 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>, <span style="color: blue;">ByVal</span> number2 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>, <span style="color: blue;">ByVal</span> [operator] <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>) <span style="color: blue;">As</span> <span style="color: blue;">String</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Method2 = Cache.CreateInstance.MyMethod2(number1, number2, [operator])</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Function</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Function</span> Method3(<span style="color: blue;">ByVal</span> clientId <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>) <span style="color: blue;">As</span> <span style="color: blue;">String</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Method3 = Cache.CreateInstance.MyMethod3(clientId)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Function</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> disposedValue <span style="color: blue;">As</span> <span style="color: blue;">Boolean</span> = <span style="color: blue;">False</span><span style="mso-spacerun: yes;"> </span><span style="color: green;">' To detect redundant calls</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' IDisposable</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Protected</span> <span style="color: blue;">Overridable</span> <span style="color: blue;">Sub</span> Dispose(<span style="color: blue;">ByVal</span> disposing <span style="color: blue;">As</span> <span style="color: blue;">Boolean</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">If</span> <span style="color: blue;">Not</span> <span style="color: blue;">Me</span>.disposedValue <span style="color: blue;">Then</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">If</span> disposing <span style="color: blue;">Then</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' TODO: free other state (managed objects).</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Console.WriteLine(<span style="color: #a31515;">"RemoteObject is Distroying"</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">If</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' TODO: free your own state (unmanaged objects).</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' TODO: set large fields to null.</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">If</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Me</span>.disposedValue = <span style="color: blue;">True</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">#Region</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: #a31515;">" IDisposable Support "</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' This code added by Visual Basic to correctly implement the disposable pattern.</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> Dispose() <span style="color: blue;">Implements</span> IDisposable.Dispose</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' Do not change this code.<span style="mso-spacerun: yes;"> </span>Put cleanup code in Dispose(ByVal disposing As Boolean) above.</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Dispose(<span style="color: blue;">True</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>GC.SuppressFinalize(<span style="color: blue;">Me</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">#End</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Region</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">End</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Class</span></span></span></div><br />
<br />
<br />
Here is a sample Remote class, as we already discussed local objects that cannot be serialized cannot be remote object, so to make this class serializable we derived this from <strong><em>MarshelByRefObject. </em></strong>You also noticed what is <strong><em>Cached</em></strong>? It’s a single tone class that is define as below. Singletone class is a class that cannot be instanciate directly through constructor.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Class</span> Cache</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Shared</span> _myInstance <span style="color: blue;">As</span> Cache</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Shared</span> _Observer <span style="color: blue;">As</span> Iobserver</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> <span style="color: blue;">New</span>()</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Shared</span> <span style="color: blue;">Sub</span> Attach(<span style="color: blue;">ByVal</span> observer <span style="color: blue;">As</span> IObserver)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>_Observer = observer</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="line-height: 115%; mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><span lang="EN-US" style="line-height: 115%; mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="color: blue;"><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>Public</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Shared</span><span style="color: black;"> </span><span style="color: blue;">Function</span><span style="color: black;"> CreateInstance() </span><span style="color: blue;">As</span><span style="color: black;"> Cache</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">If</span><span style="color: black;"> _myInstance </span><span style="color: blue;">Is</span><span style="color: black;"> </span><span style="color: blue;">Nothing</span><span style="color: black;"> </span><span style="color: blue;">Then</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>_myInstance = </span><span style="color: blue;">New</span><span style="color: black;"> Cache</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">If</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Return</span><span style="color: black;"> _myInstance</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Function</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Public</span><span style="color: black;"> </span><span style="color: blue;">Function</span><span style="color: black;"> MyMethod1() </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">String</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>MyMethod1 = _Observer.InvokeMethod</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Function</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Public</span><span style="color: black;"> </span><span style="color: blue;">Function</span><span style="color: black;"> MyMethod2(</span><span style="color: blue;">ByVal</span><span style="color: black;"> number1 </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">, </span><span style="color: blue;">ByVal</span><span style="color: black;"> number2 </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">, </span><span style="color: blue;">ByVal</span><span style="color: black;"> [operator] </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">) </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">String</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>MyMethod2 = _Observer.InvokeMethod(number1, number2, [operator])</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Function</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">End</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Class</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"> As per discussed instace of this class can be generated from <strong><em>CreateInstance()</em></strong> method. So this is first and most important part of our application. You might think why important? Because both client and server application use this objects for communication. You might think why don’t common class library project developed and distributed between client and server. Because it’s not a good programing practice to shared business logic to client side, that’s why we developed interface to hide bussiness intelegence.</div><br />
Now lets design server side object. Remote server is console application that register Remote object as some well known service. Lets see how to do that. For this first develop remote class that implement <strong><em>Iobserver</em></strong> interface.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.IO</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.InteropServices</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> RemotableObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Class</span> MyObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Implements</span> IObserver</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Implements</span> Idisposable</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Function</span> InvokeMethod() <span style="color: blue;">As</span> <span style="color: blue;">String</span> <span style="color: blue;">Implements</span> RemotableObject.IObserver.InvokeMethod</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Console.WriteLine(<span style="color: #a31515;">"Invoke 1 Method Called...."</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>InvokeMethod = <span style="color: #a31515;">"This is Invoke 1 Method"</span></span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="line-height: 115%; mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Function</span></span></span></div><span lang="EN-US" style="line-height: 115%; mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="color: blue;"><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Function</span><span style="color: black;"> InvokeMethod(</span><span style="color: blue;">ByVal</span><span style="color: black;"> number1 </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">, </span><span style="color: blue;">ByVal</span><span style="color: black;"> number2 </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">, </span><span style="color: blue;">ByVal</span><span style="color: black;"> [operator] </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;">) </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">String</span><span style="color: black;"> </span><span style="color: blue;">Implements</span><span style="color: black;"> RemotableObject.IObserver.InvokeMethod</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Dim</span><span style="color: black;"> output </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">String</span><span style="color: black;"> = </span><span style="color: #a31515;">"Output: {0} {1} {2} = {3}"</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: blue;">Select</span><span style="color: black;"> </span><span style="color: blue;">Case</span><span style="color: black;"> [operator]</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Case</span><span style="color: black;"> 1</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>output = </span><span style="color: blue;">String</span><span style="color: black;">.Format(output, number1, </span><span style="color: #a31515;">"+"</span><span style="color: black;">, number2, (number1 + number2))</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Case</span><span style="color: black;"> 2</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>output = </span><span style="color: blue;">String</span><span style="color: black;">.Format(output, number1, </span><span style="color: #a31515;">"-"</span><span style="color: black;">, number2, (number1 - number2))</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: blue;">Case</span><span style="color: black;"> 3</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>output = </span><span style="color: blue;">String</span><span style="color: black;">.Format(output, number1, </span><span style="color: #a31515;">"X"</span><span style="color: black;">, number2, (number1 * number2))</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Case</span><span style="color: black;"> </span><span style="color: blue;">Else</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>output = </span><span style="color: blue;">String</span><span style="color: black;">.Format(output, number1, </span><span style="color: #a31515;">"/"</span><span style="color: black;">, number2, (number1 / number2))</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Select</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>Console.WriteLine(output)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>InvokeMethod = output</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Function</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Private</span><span style="color: black;"> disposedValue </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Boolean</span><span style="color: black;"> = </span><span style="color: blue;">False</span><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' To detect redundant calls</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' IDisposable</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Protected</span><span style="color: black;"> </span><span style="color: blue;">Overridable</span><span style="color: black;"> </span><span style="color: blue;">Sub</span><span style="color: black;"> Dispose(</span><span style="color: blue;">ByVal</span><span style="color: black;"> disposing </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Boolean</span><span style="color: black;">)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">If</span><span style="color: black;"> </span><span style="color: blue;">Not</span><span style="color: black;"> </span><span style="color: blue;">Me</span><span style="color: black;">.disposedValue </span><span style="color: blue;">Then</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">If</span><span style="color: black;"> disposing </span><span style="color: blue;">Then</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' TODO: free other state (managed objects).</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>Console.WriteLine(</span><span style="color: #a31515;">"MyObject is Distroying"</span><span style="color: black;">)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">If</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' TODO: free your own state (unmanaged objects).</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' TODO: set large fields to null.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">If</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Me</span><span style="color: black;">.disposedValue = </span><span style="color: blue;">True</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Sub</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">#Region</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: #a31515;">" IDisposable Support "</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' This code added by Visual Basic to correctly implement the disposable pattern.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Public</span><span style="color: black;"> </span><span style="color: blue;">Sub</span><span style="color: black;"> Dispose() </span><span style="color: blue;">Implements</span><span style="color: black;"> IDisposable.Dispose</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">' Do not change this code.<span style="mso-spacerun: yes;"> </span>Put cleanup code in Dispose(ByVal disposing As Boolean) above.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>Dispose(</span><span style="color: blue;">True</span><span style="color: black;">)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>GC.SuppressFinalize(</span><span style="color: blue;">Me</span><span style="color: black;">)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Sub</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">#End</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Region</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">End</span><span lang="EN-US" style="mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Class</span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"></div>As you see, we defined a class that implement <strong><em>Iobserver</em></strong> interface. Now develop console application that register this class as service. To register a service you have to define <strong>channel.</strong><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting.Channels</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting.Channels.Tcp</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> RemotableObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Module</span><span lang="EN-US" style="mso-no-proof: yes;"> Module1</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><STAThread()> _</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Sub</span> Main()</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> RemoteMyObject <span style="color: blue;">As</span> <span style="color: blue;">New</span> RemoteMyObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'************************************* TCP *************************************</span></span></span></div><span lang="EN-US" style="font-family: "Calibri", "sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' using TCP protocol</span></span><br />
<span lang="EN-US" style="font-family: "Calibri", "sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-no-proof: yes;"><span style="color: green;"></span></span><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> channel <span style="color: blue;">As</span> <span style="color: blue;">New</span> TcpChannel(8080)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>ChannelServices.RegisterChannel(channel, <span style="color: blue;">False</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>RemotingConfiguration.RegisterWellKnownServiceType(<span style="color: blue;">GetType</span>(RemoteMyObject), <span style="color: #a31515;">"RemoteObject.rem"</span>, WellKnownObjectMode.Singleton)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'************************************* TCP *************************************</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Cache.Attach(<span style="color: blue;">New</span> MyObject)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Cache.Attach(<span style="color: blue;">New</span> MyClient)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>Console.Out.WriteLine(<span style="color: #a31515;">"Server is Ready to Accept Request"</span>)</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">While</span> Console.Read()</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Exit</span> <span style="color: blue;">While</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">While</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">End</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Module</span></span></span></div>So here is our remote server is ready. When you run this application it will look like this.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkD-KwHpdMfYq2NebpbHxLbtOThJVluqSz2sFEl4RuEKNdRbp-RHFXzC7h19J2osxSlBMx2xhpZ7TyOJY94T_Wmu2UihCv1_iSC4qyS-Mo8Q8bPEzEsJKmY5FOf6-RmzJY2mxkL5pQoCI/s1600/server1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" oda="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkD-KwHpdMfYq2NebpbHxLbtOThJVluqSz2sFEl4RuEKNdRbp-RHFXzC7h19J2osxSlBMx2xhpZ7TyOJY94T_Wmu2UihCv1_iSC4qyS-Mo8Q8bPEzEsJKmY5FOf6-RmzJY2mxkL5pQoCI/s1600/server1.png" /></a></div><br />
Now last but not least start developing the client.<br />
<br />
<div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> RemotableObject</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting.Channels</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Imports</span><span lang="EN-US" style="mso-no-proof: yes;"> System.Runtime.Remoting.Channels.Tcp</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">Public</span><span lang="EN-US" style="mso-no-proof: yes;"> <span style="color: blue;">Class</span> Form1</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> Form1_Load(<span style="color: blue;">ByVal</span> sender <span style="color: blue;">As</span> System.Object, <span style="color: blue;">ByVal</span> e <span style="color: blue;">As</span> System.EventArgs) <span style="color: blue;">Handles</span> <span style="color: blue;">MyBase</span>.Load</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> ExportButton1_Click(<span style="color: blue;">ByVal</span> sender <span style="color: blue;">As</span> System.Object, <span style="color: blue;">ByVal</span> e <span style="color: blue;">As</span> System.EventArgs) <span style="color: blue;">Handles</span> ExportButton1.Click</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'************************************* TCP *************************************</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' using TCP protocol</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' running both client and server on same machines</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> chan <span style="color: blue;">As</span> <span style="color: blue;">New</span> TcpChannel()</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><span style="mso-spacerun: yes;"> </span>ChannelServices.RegisterChannel(chan, <span style="color: blue;">False</span>)</span></span></div><span lang="EN-US" style="mso-no-proof: yes;"><span style="font-family: Calibri;"><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: green;">' Create an instance of the remote object</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> remoteOITExport <span style="color: blue;">As</span> RemoteMyObject</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Try</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>remoteOITExport = <span style="color: blue;">TryCast</span>(Activator.GetObject(<span style="color: blue;">GetType</span>(RemoteMyObject), <span style="color: #a31515;">"tcp://dell6:8080/RemoteObject.rem"</span>), RemoteMyObject)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>OutputMessage.Text = OutputMessage.Text & vbCrLf & remoteOITExport.Method1</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex2 <span style="color: blue;">As</span> Net.Sockets.SocketException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex2.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex1 <span style="color: blue;">As</span> RemotingTimeoutException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex1.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex <span style="color: blue;">As</span> RemotingException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Finally</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>remoteOITExport = <span style="color: blue;">Nothing</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>ChannelServices.UnregisterChannel(chan)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>chan = <span style="color: blue;">Nothing</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Try</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> ExportButton_Click(<span style="color: blue;">ByVal</span> sender <span style="color: blue;">As</span> System.Object, <span style="color: blue;">ByVal</span> e <span style="color: blue;">As</span> System.EventArgs) <span style="color: blue;">Handles</span> ExportButton.Click</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'************************************* TCP *************************************</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' using TCP protocol</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' running both client and server on same machines</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> chan <span style="color: blue;">As</span> <span style="color: blue;">New</span> TcpChannel()</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>ChannelServices.RegisterChannel(chan, <span style="color: blue;">False</span>)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">' Create an instance of the remote object</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> remoteOITExport <span style="color: blue;">As</span> RemoteMyObject</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> number1 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = <span style="color: blue;">New</span> Random().Next(50, 99)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> number2 <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = <span style="color: blue;">New</span> Random().Next(1, 49)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> [operator] <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = <span style="color: blue;">New</span> Random().Next(1, 10)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Try</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>remoteOITExport = <span style="color: blue;">TryCast</span>(Activator.GetObject(<span style="color: blue;">GetType</span>(RemoteMyObject), <span style="color: #a31515;">"tcp://dell6:8080/RemoteObject.rem"</span>), RemoteMyObject)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>OutputMessage.Text = OutputMessage.Text & vbCrLf & remoteOITExport.Method2(number1, number2, [operator])</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex2 <span style="color: blue;">As</span> Net.Sockets.SocketException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex2.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex1 <span style="color: blue;">As</span> RemotingTimeoutException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex1.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Catch</span> ex <span style="color: blue;">As</span> RemotingException</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MessageBox.Show(ex.Message)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Finally</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>remoteOITExport = <span style="color: blue;">Nothing</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>ChannelServices.UnregisterChannel(chan)</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>chan = <span style="color: blue;">Nothing</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Try</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="color: blue; mso-no-proof: yes;">End Class</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"></div>As you can see it’s a simple windows project having couple of buttons. On each button click channel is registered and client side proxy of remote object is generated through <strong><em>Activator.GetObject()</em></strong> method. Our user interface will look like this.<br />
<br />
<div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMk_5N4OE3D6OpTORZ4mBzwsYrqCtUFMexU7imFNHVlw_Sb2wnqysZMoi1U4cqMecbvpm1LiJOUEhezICSXAPxypK3_xfNsTgDEUTiSJxnM7j-AvNphyphenhyphenknT16I_W0hrvqwd2ZkcWqDdc8/s1600/client.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" oda="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMk_5N4OE3D6OpTORZ4mBzwsYrqCtUFMexU7imFNHVlw_Sb2wnqysZMoi1U4cqMecbvpm1LiJOUEhezICSXAPxypK3_xfNsTgDEUTiSJxnM7j-AvNphyphenhyphenknT16I_W0hrvqwd2ZkcWqDdc8/s1600/client.png" /></a></div></span></span> </div>After pressing buttons for Remote Method 1 and Remote Method 2, output is generated on server side.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFsOBJ8u2dTKtAYZrIMfccG9hQlKXOJF5vVsEkd88psBkjCN8C5cwhvw-zxxenSyeQ44kL8sv2ciB5BGGPgTjPpUsg5T50JFQClkomWmNpnTE4ltNl9vcAUkAvCxTt3b53ifMSNE0vixg/s1600/server2.png" imageanchor="1" style="clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" oda="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFsOBJ8u2dTKtAYZrIMfccG9hQlKXOJF5vVsEkd88psBkjCN8C5cwhvw-zxxenSyeQ44kL8sv2ciB5BGGPgTjPpUsg5T50JFQClkomWmNpnTE4ltNl9vcAUkAvCxTt3b53ifMSNE0vixg/s1600/server2.png" /></a></div></span></span></span> </span></span></span> </div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-35308302750657699272011-09-12T22:55:00.000-07:002011-09-19T08:10:34.105-07:00MyService Demo<div dir="ltr" style="text-align: left;" trbidi="on"><span style="color: blue;"> using</span> System;<br />
<span style="color: blue;"> <span style="color: blue;">using</span></span><span style="color: blue;"> System.Collections.Generic;</span><br />
<span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span><span style="color: blue;"><span style="color: blue;"> System.ComponentModel;</span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Data;</span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Diagnostics;</span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Linq;</span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.ServiceProcess;</span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Text;</span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">namespace</span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span></span></span></span></span></span></span></span></span> ServiceDemo</span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> {<br />
<span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">MyFirstWindowService</span> : <span style="color: #2b91af;">ServiceBase</span>{<br />
<span style="color: blue;">public</span> MyFirstWindowService()<br />
{<br />
InitializeComponent();<br />
}<br />
<span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnStart(<span style="color: blue;">string</span>[] args)<br />
{<br />
<span style="color: green;">// TODO: Add code here to start your service.</span><span style="color: blue;">using</span> (<span style="color: #2b91af;">Process</span> process = <span style="color: blue;">new</span> <span style="color: #2b91af;">Process</span>())<br />
{<br />
process.StartInfo = <span style="color: blue;">new</span> <span style="color: #2b91af;">ProcessStartInfo</span>(<span style="color: #a31515;">"calc.exe"</span>);<br />
process.StartInfo.CreateNoWindow = <span style="color: blue;">true</span>;<br />
process.StartInfo.ErrorDialog = <span style="color: blue;">false</span>;<br />
process.StartInfo.RedirectStandardError = <span style="color: blue;">true</span>;<br />
process.StartInfo.RedirectStandardInput = <span style="color: blue;">true</span>;<br />
process.StartInfo.RedirectStandardOutput = <span style="color: blue;">true</span>;<br />
process.StartInfo.UseShellExecute = <span style="color: blue;">false</span>;<br />
process.StartInfo.WindowStyle = <span style="color: #2b91af;">ProcessWindowStyle</span>.Normal;<br />
process.Start();<br />
System.Threading.<span style="color: #2b91af;">Thread</span>.Sleep(10000);<br />
<span style="color: blue;">if</span> (!process.HasExited)<br />
{<br />
process.WaitForExit(120000);<br />
<span style="color: blue;">if</span> (!process.HasExited)<br />
process.Kill();<br />
}<br />
}<br />
}<br />
<span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnStop()<br />
{<br />
<span style="color: green;">// TODO: Add code here to perform any tear-down necessary to stop your service.</span>}<br />
}<br />
}<br />
<br />
<span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System;</span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Collections;</span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using </span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;">System.Collections.Generic;</span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.ComponentModel;</span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Configuration.Install;</span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.ServiceProcess;</span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Linq;</span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <br />
<span style="color: blue;">namespace </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span> ServiceDemo</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> {<br />
[<span style="color: #2b91af;">RunInstaller</span>(<span style="color: blue;">true</span>)]<br />
<span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">MyWindowServiceInstaller</span> : <span style="color: #2b91af;">Installer</span>{<br />
<span style="color: blue;">public</span> MyWindowServiceInstaller()<br />
{<br />
InitializeComponent();<br />
<span style="color: #2b91af;">ServiceProcessInstaller</span> serviceProcessInstaller =<br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">ServiceProcessInstaller</span>();<br />
<span style="color: #2b91af;">ServiceInstaller</span> serviceInstaller = <span style="color: blue;">new</span> <span style="color: #2b91af;">ServiceInstaller</span>();<br />
<span style="color: green;">//# Service Account Information</span>serviceProcessInstaller.Account = <span style="color: #2b91af;">ServiceAccount</span>.LocalSystem;<br />
serviceProcessInstaller.Username = <span style="color: blue;">null</span>;<br />
serviceProcessInstaller.Password = <span style="color: blue;">null</span>;<br />
<span style="color: green;">//# Service Information</span>serviceInstaller.DisplayName = <span style="color: #a31515;">"My New C# Windows Service"</span>;<br />
serviceInstaller.StartType = <span style="color: #2b91af;">ServiceStartMode</span>.Automatic;<br />
<span style="color: green;">//# This must be identical to the WindowsService.ServiceBase name</span><span style="color: green;">//# set in the constructor of WindowsService.cs</span>serviceInstaller.ServiceName = <span style="color: #a31515;">"My Windows Service"</span>;<br />
<span style="color: blue;">this</span>.Installers.Add(serviceProcessInstaller);<br />
<span style="color: blue;">this</span>.Installers.Add(serviceInstaller);<br />
}<br />
}<br />
}<br />
<br />
<span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> System.Collections.Generic;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;">System.Linq;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;">System.ServiceProcess;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">using</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> </span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;">System.Text;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> <span style="color: blue;">namespace </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;">ServiceDemo</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"><span style="color: blue;"> {<br />
<span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span>{<br />
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><summary></span><span style="color: grey;">///</span><span style="color: green;"> The main entry point for the application.</span><span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"></summary></span><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()<br />
{<br />
<span style="color: #2b91af;">ServiceBase</span>[] ServicesToRun;<br />
ServicesToRun = <span style="color: blue;">new</span> <span style="color: #2b91af;">ServiceBase</span>[] <br />
{ <br />
<span style="color: blue;">new</span> <span style="color: #2b91af;">MyFirstWindowService</span>()<br />
};<br />
<span style="color: #2b91af;">ServiceBase</span>.Run(ServicesToRun);<br />
}<br />
}<br />
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-4241216125258249792011-08-08T03:45:00.000-07:002011-09-16T11:28:06.370-07:00Ajax Rating Control with Database<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"><b><u>Introduction:</u></b></span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"><br />
<br />
Here I will explain how to use Ajax rating control with database to display average rating for particular article using asp.net<br />
<br />
<b><u>Description:</u></b><br />
<br />
Previously I explained </span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">how to use <a href="http://aspdotnet-suresh.blogspot.com/2011/04/ajax-collapsible-panel-control-sample.html" target="_blank"><span style="color: #d1580d;">Ajax Collapsible panel</span></a> . Now I will explain how to use Ajax rating control with Database in asp.net. In many sites we will see rating options for books, articles and movies etc by giving rating option to user we have chance to know about particular thing how much users are feeling comfort with particular thing. In Ajax we have a rating control by using that we can display the rating option easily. Here I am storing each user rating details into database and displaying the average rating based on number of users rating. To achieve this first design one table in your database and give name like <b>RatingDetails </b>if you want to give another name you can but you need to change table name in code also.</span><br />
<a href="http://www.blogger.com/post-edit.g?blogID=9073788502453820575&postID=424121612525824979&from=pencil" name="more"></a><br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: medium none;"><tbody>
<tr><td style="background: none repeat scroll 0% 0% rgb(227, 108, 10); border: 1pt solid windowtext; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: white; font-family: "Verdana","sans-serif"; font-size: 10pt;">Column Name</span></b></div></td><td style="background: none repeat scroll 0% 0% rgb(227, 108, 10); border: 1pt solid windowtext; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: white; font-family: "Verdana","sans-serif"; font-size: 10pt;">Data Type</span></b></div></td><td style="background: none repeat scroll 0% 0% rgb(227, 108, 10); border: 1pt solid windowtext; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: white; font-family: "Verdana","sans-serif"; font-size: 10pt;">Allow Nulls</span></b></div></td></tr>
<tr><td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Id</span></div></td><td style="border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Int(Set Identity=true)</span></div></td><td style="border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">No</span></div></td></tr>
<tr><td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Rate</span></div></td><td style="border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">int</span></div></td><td style="border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 159.6pt;" valign="top" width="213"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Yes</span></div></td></tr>
</tbody></table><div class="MsoNormal" style="line-height: normal; margin-bottom: 12pt;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">After completion of design table in database add <b>AjaxControlToolkit</b> reference to your application after that add </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="background: none repeat scroll 0% 0% yellow; font-family: "Verdana","sans-serif"; font-size: 10pt;"><%</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">@</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: #a31515;">Register</span> <span style="color: red;">Namespace</span><span style="color: blue;">="AjaxControlToolkit"</span> <span style="color: red;">Assembly</span><span style="color: blue;">="AjaxControlToolkit"</span> <span style="color: red;">tagPrefix</span><span style="color: blue;">="ajax"</span> <span style="background: none repeat scroll 0% 0% yellow;">%></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 12pt;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">To your aspx page and design your page likes this</span></div><table border="1" cellpadding="0" class="MsoNormalTable" style="border: 1pt dashed brown;"><tbody>
<tr><td style="border: medium none; padding: 0.75pt;"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="background: none repeat scroll 0% 0% yellow; font-family: "Verdana","sans-serif"; font-size: 10pt;"><%</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">@</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: #a31515;">Register</span> <span style="color: red;">Assembly</span><span style="color: blue;">="AjaxControlToolkit"</span> <span style="color: red;">Namespace</span><span style="color: blue;">="AjaxControlToolkit"</span> <span style="color: red;">TagPrefix</span><span style="color: blue;">="ajax"</span> <span style="background: none repeat scroll 0% 0% yellow;">%></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><!</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">DOCTYPE</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">html</span> <span style="color: red;">PUBLIC</span> <span style="color: blue;">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color: blue;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">html</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">xmlns</span><span style="color: blue;">="http://www.w3.org/1999/xhtml"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">head</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">runat</span><span style="color: blue;">="server"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">title</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Ajax Rating Sample<span style="color: blue;"></</span><span style="color: #a31515;">title</span><span style="color: blue;">></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">style</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">type</span><span style="color: blue;">="text/css"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">.ratingEmpty</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">background-image</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">: <span style="color: blue;">url(ratingStarEmpty.gif)</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">width</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">height</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">.ratingFilled</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">background-image</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">: <span style="color: blue;">url(ratingStarFilled.gif)</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">width</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">height</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">.ratingSaved</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">background-image</span>: <span style="color: blue;">url(ratingStarSaved.gif)</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">width</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">height</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">:<span style="color: blue;">18px</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">style</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">head</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">body</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">form</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">id</span><span style="color: blue;">="form1"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ajax</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ToolkitScriptManager</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">ID</span><span style="color: blue;">="ScripManager1"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"/></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">div</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">asp</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">UpdatePanel</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">ID</span><span style="color: blue;">="pnlRating"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ContentTemplate</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">table</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">width</span><span style="color: blue;">="35%"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">tr</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">width</span><span style="color: blue;">="27%"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">b</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Average Rating:<span style="color: blue;"></</span><span style="color: #a31515;">b</span><span style="color: blue;">></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ajax</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">Rating</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">ID</span><span style="color: blue;">="ratingControl"</span> <span style="color: red;">AutoPostBack</span><span style="color: blue;">="true"</span> <span style="color: red;">OnChanged</span><span style="color: blue;">="RatingControlChanged"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span> <span style="color: red;">StarCssClass</span><span style="color: blue;">="ratingEmpty"</span> <span style="color: red;">WaitingStarCssClass</span><span style="color: blue;">="ratingSaved"</span> <span style="color: red;">EmptyStarCssClass</span><span style="color: blue;">="ratingEmpty"</span> <span style="color: red;">FilledStarCssClass</span><span style="color: blue;">="ratingFilled"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ajax</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">Rating</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">b</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: blue;"><</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">label</span> <span style="color: red;">ID</span><span style="color: blue;">="lbltxt"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"/></span> <span style="color: blue;"></</span><span style="color: #a31515;">b</span><span style="color: blue;">></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">tr</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">tr</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: red;">colspan</span><span style="color: blue;">="2"></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">td</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">tr</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">table</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">ContentTemplate</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">asp</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">UpdatePanel</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">div</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">form</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">body</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">html</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div></td></tr>
</tbody></table><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">If you observe above code I define lot of properties to</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;"> ajax</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">:</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">Rating</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">now I will explain all the properties of Ajax rating control</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">AutoPostBack</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This property should be true because we are storing rating details during rating item click. </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">OnChanged</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This event is used to get how many stars user has selected.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">StarCssClass</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This cssclass is used for to display starts.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">WaitingStarCssClass</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This cssclass is used to show the starts color during saving the rating value. </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">EmptyStarCssClass</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This cssclass is used to display empty starts color.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span></b><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">FilledStarCssClass</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This cssclass is used to display filled stars color.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">CurrentRating</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">– This property is used to display Initial rating value (Number of starts to be filled initially). </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">MaxRating</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">– This property is used to display maximum rating value (No. of starts here I am displaying only 5 if you want to increase starts value give property like this <b>MaxRating=10</b>). </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">ReadOnly</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This property is used to make rating control read only. </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">RatingAlign</span></b><b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">– This property is used to set starts vertical or horizontal.</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><b><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">RatingDirection</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> – This property is used to set the direction of stars</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">(LeftToRight or TopToBottom or RightToLeft or BottomToTop</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">). </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Now in code behind add following namespaces</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><table border="1" cellpadding="0" class="MsoNormalTable" style="border: 1pt dashed brown;"><tbody>
<tr><td style="border: medium none; padding: 0.75pt;"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">using</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> System.Configuration;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">using</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> System.Data;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">using</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> System.Data.SqlClient;</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"></span></div></td></tr>
</tbody></table><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">After completion of adding namespaces in code behind add following code </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><table border="1" cellpadding="0" class="MsoNormalTable" style="border: 1pt dashed brown;"><tbody>
<tr><td style="border: medium none; padding: 0.75pt;"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlConnection</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> con = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(<span style="color: #2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color: #a31515;">"dbconnection"</span>].ConnectionString);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">protected</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: blue;">void</span> Page_Load(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">if</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">(!IsPostBack)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">BindRatingControl();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">protected</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: blue;">void</span> RatingControlChanged(<span style="color: blue;">object</span> sender, AjaxControlToolkit.<span style="color: #2b91af;">RatingEventArgs</span> e)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">con.Open();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlCommand</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> cmd = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlCommand</span>(<span style="color: #a31515;">"insert into RatingDetails(Rate)values(@Rating)"</span>,con);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">cmd.Parameters.AddWithValue(<span style="color: #a31515;">"@Rating"</span>, ratingControl.CurrentRating);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">cmd.ExecuteNonQuery();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">con.Close();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">BindRatingControl(); </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">protected</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> <span style="color: blue;">void</span> BindRatingControl()</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">int</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> total = 0;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">DataTable</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> dt = <span style="color: blue;">new</span> <span style="color: #2b91af;">DataTable</span>();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">con.Open();</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlCommand</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> cmd = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlCommand</span>(<span style="color: #a31515;">"Select Rate from RatingDetails"</span>, con);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlDataAdapter</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> da = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlDataAdapter</span>(cmd);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">da.Fill(dt);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">if</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">(dt.Rows.Count>0)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">for</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">(<span style="color: blue;">int</span> i=0;i<dt.Rows.Count;i++)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">{</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">total += <span style="color: #2b91af;">Convert</span>.ToInt32(dt.Rows[i][0].ToString());</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">int</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"> average = total/(dt.Rows.Count);</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">ratingControl.CurrentRating = average;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">lbltxt.Text = dt.Rows.Count+<span style="color: #a31515;">"user(s) have rated this article"</span>;</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">}</span></div></td></tr>
</tbody></table><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Now in web.config file set your database connection because in above code I am getting connection from web.config</span></div><table border="1" cellpadding="0" class="MsoNormalTable" style="border: 1pt dashed brown;"><tbody>
<tr><td style="border: medium none; padding: 0.75pt;"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">connectionStrings</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"><</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">add</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"> </span><span style="color: red; font-family: "Verdana","sans-serif"; font-size: 10pt;">name</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">=</span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">"<span style="color: blue;">dbConnection</span>"<span style="color: blue;"> </span><span style="color: red;">connectionString</span><span style="color: blue;">=</span>"<span style="color: blue;">Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true</span>"<span style="color: blue;">/></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;"></</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">connectionStrings</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10pt;">></span><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"></span></div></td></tr>
</tbody></table><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: 12pt;"><b><u><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;">Demo</span></u></b><span style="font-family: "Verdana","sans-serif"; font-size: 10pt;"><br />
</span></div><div closure_uid_yq3t1w="119"></div><table border="1" cellpadding="0" class="MsoNormalTable" style="border: 1pt dashed brown;"><tbody>
<tr><td style="border: medium none; padding: 0.75pt;"><div class="separator" style="clear: both; text-align: center;"><a href="http://imgboot.com/images/sureshdasari/ajaxratingcontrol.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://imgboot.com/images/sureshdasari/ajaxratingcontrol.gif" /></a></div></td></tr>
</tbody></table><br />
<div closure_uid_yq3t1w="119"><br />
</div><div closure_uid_yq3t1w="119"><a href="http://www.box.net/shared/2sx0jkp73d">Download Demo</a></div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com2tag:blogger.com,1999:blog-9073788502453820575.post-89755705947523050502011-08-06T05:39:00.000-07:002011-09-16T11:32:05.034-07:00URL Re-Writing in ASP.Net<div dir="ltr" style="text-align: left;" trbidi="on"><h2>Introduction</h2>One of the most popular extensions to the Apache webserver has been <code>mod_rewrite</code> - a filter which rewrites URLs. For example, instead of a URL such as <br />
<div class="pre-action-link" closure_uid_jovsab="166" id="premain0" style="display: block;" width="100%"></div><pre id="pre0" lang="text" style="margin-top: 0px;">http://www.apache.org/BookDetails.pl?id=5</pre><div class="separator" closure_uid_jovsab="239" style="clear: both; text-align: center;"><br />
</div><div closure_uid_jovsab="190">you could provide a filter which accepts URLs such as </div><div class="pre-action-link" closure_uid_jovsab="186" id="premain1" style="display: block;" width="100%"></div><pre id="pre1" lang="text" style="margin-top: 0px;">http://www.apache.org/Book/5.html</pre>and it will silently perform a server-side redirect to the first URL. In this way, the real URL could be hidden, providing an obfuscated facade to the web page. The benefits are easier to remember URLs and increasing the difficulty of hacking a website. <br />
<code>Mod_rewrite</code> became very popular and grew to encompass a couple of other features not related to URL Rewriting, such as caching. This article demonstrates URL Rewriting with ASP.NET, whereby the requested URL is matched based on a regular expression and the URL mappings are stored in the standard ASP.NET <code>web.config</code> configuration file. ASP.NET includes great caching facilities, so there's no need to duplicate <code>mod_rewrite</code>'s caching functionality. <br />
As more and more websites are being rewritten with ASP.NET, the old sites which had been indexed by google and linked from other sites are lost, inevitably culminating in the dreaded 404 error. I will show how legacy ASP sites can be upgraded to ASP.NET, while maintaining links from search engines. <br />
<h2>ASP.NET support for URL Rewriting</h2>ASP.NET provides very limited support out of the box. In fact, it's support is down to a single method: <br />
<div class="pre-action-link" closure_uid_jovsab="146" id="premain2" style="display: block;" width="100%"></div><pre id="pre2" style="margin-top: 0px;"><span class="code-keyword">void</span> HttpContext.RewritePath(<span class="code-SDKkeyword">string</span> path)</pre>which should be called during the <code>Application_BeginRequest()</code> event in the <code>Global.asax</code> file. This is fine as long as the number of URLs to rewrite is a small, finite, managable number. However most ASP sites are in some way dynamic, passing parameters in the Query String, so we require a much more configurable approach. <br />
The storage location for all ASP.NET Configuration information is the <i>web.config</i> file, so we'd really like to specify the rewrites in there. Additionally, .Net has a fast regular expression processor, giving free and fast search and replace of URLs. Let's define a section in the <i>web.config</i> file which specifies those rewrites: <br />
<div class="pre-action-link" closure_uid_jovsab="141" id="premain3" style="display: block;" width="100%"></div><pre closure_uid_jovsab="145" id="pre3" lang="xml" style="margin-top: 0px;"><span class="code-keyword"><</span><span class="code-leadattribute">configuration</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">system.web</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">urlrewrites</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>/urlrewriter/show\.asp<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>show.aspx<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>/urlrewriter/wohs\.asp<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>show.aspx<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>/urlrewriter/show(.*)\.asp<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>show.aspx?$1<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>/urlrewriter/(.*)show\.html<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>show.aspx?id=$1<span class="code-keyword">&amp;</span>cat=2<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>/urlrewriter/s/h/o/w/(.*)\.html<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">url</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>/urlrewriter/show.aspx?id=$1<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rewrite</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">rule</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">urlrewrites</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">system.web</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">configuration</span><span class="code-keyword">></span>
</pre>Notice how we have to escape the period in the url element such as 'show\.asp'. This is a Regular Expression escape and it's a small price to pay for the flexibility of regular expressions. These also show how we set-up a capturing expression using (.*) in the <code><span class="code-keyword"><</span>url<span class="code-keyword">></span></code> element and refer to that capture in the <code><span class="code-keyword"><</span>rewrite<span class="code-keyword">></span></code> element with $1 <br />
<h2>Configuration Section Handlers</h2><div closure_uid_jovsab="140">.Net's configuration mechanism requires us to write code as a "handler" for this section. Here's the code for that: </div><div class="pre-action-link" closure_uid_jovsab="136" id="premain4" style="display: block;" width="100%"></div><pre id="pre4" lang="xml" style="margin-top: 0px;"><span class="code-keyword"><</span><span class="code-leadattribute">configuration</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">configSections</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">sectionGroup</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">system.web"</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-leadattribute">section</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">urlrewrites"</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">ThunderMain.URLRewriter.Rewriter,
ThunderMain.URLRewriter, Version=1.0.783.30976,
Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"</span><span class="code-keyword">/</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">sectionGroup</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">configSections</span><span class="code-keyword">></span>
<span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">configuration</span><span class="code-keyword">></span>
</pre>This section handler specifies that for every section called "urlrewrites", there is a class called <code>ThunderMain.URLRewriter.Rewriter</code> which can be found in the ThunderMain.URLRewriter.dll assembly with the given public key token. The public key token is required because this assembly has to be placed into the GAC and therefore given a strong name. <br />
<div closure_uid_jovsab="135">A section handler is defined as a class which implements the <code>IConfigurationSectionHandler </code>interface. This has one method, <code>Create()</code>, which should be implemented, and in our code that is very simple. It merely stores the urlrewrites element for later use: </div><div closure_uid_jovsab="135"><br />
</div><pre id="pre5" lang="cs" style="margin-top: 0px;"><span class="code-keyword">public</span> <span class="code-keyword">object</span> Create(<span class="code-keyword">object</span> parent, <span class="code-keyword">object</span> configContext, XmlNode section)
{
_oRules=section;
<span class="code-keyword">return</span> <span class="code-keyword">this</span>;
}
</pre><h2>Initiating the rewrite process</h2>Coming back to actually rewriting the URL, as I said earlier, we need to do something in the <code>Application_BeginRequest()</code> event in Global.asax - we just delegate this to another class: <br />
<div class="pre-action-link" closure_uid_jovsab="117" id="premain6" style="display: block;" width="100%"></div><pre id="pre6" lang="cs" style="margin-top: 0px;"><span class="code-keyword">protected</span> <span class="code-keyword">void</span> Application_BeginRequest(<span class="code-SDKkeyword">Object</span> sender, EventArgs e)
{
ThunderMain.URLRewriter.Rewriter.Process();
}
</pre>which calls the static method <code>Process() </code>on the Rewriter class. <code>Process() </code>first obtains a reference to the configuration section handler (which happens to be an instance of the current class) and then delegates most of the work to <code>GetSubstitution()</code> - an instance method of this class. <br />
<div class="pre-action-link" closure_uid_jovsab="108" id="premain7" style="display: block;" width="100%"></div><pre closure_uid_jovsab="116" id="pre7" lang="cs" style="margin-top: 0px;"><span class="code-keyword">public</span> <span class="code-keyword">static</span> <span class="code-keyword">void</span> Process()
{
Rewriter oRewriter=
(Rewriter)ConfigurationSettings.GetConfig(<span class="code-string">"</span><span class="code-string">system.web/urlrewrites"</span>);
<span class="code-keyword">string</span> zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path);
<span class="code-keyword">if</span>(zSubst.Length>0) {
HttpContext.Current.RewritePath(zSubst);
}
}
</pre><code>GetSubstitution()</code> is just as simple - iterating through all possible URL Rewrites to see if one matches. If it does, it returns the new URL, otherwise it just returns the original URL: <br />
<div class="pre-action-link" closure_uid_jovsab="107" id="premain8" style="display: block;" width="100%"></div><pre id="pre8" lang="cs" style="margin-top: 0px;"><span class="code-keyword">public</span> <span class="code-keyword">string</span> GetSubstitution(<span class="code-keyword">string</span> zPath)
{
Regex oReg;
<span class="code-keyword">foreach</span>(XmlNode oNode <span class="code-keyword">in</span> _oRules.SelectNodes(<span class="code-string">"</span><span class="code-string">rule"</span>)) {
oReg=new Regex(oNode.SelectSingleNode(<span class="code-string">"</span><span class="code-string">url/text()"</span>).Value);
Match oMatch=oReg.Match(zPath);
<span class="code-keyword">if</span>(oMatch.Success) {
<span class="code-keyword">return</span> oReg.Replace(zPath,
oNode.SelectSingleNode(<span class="code-string">"</span><span class="code-string">rewrite/text()"</span>).Value);
}
}
<span class="code-keyword">return</span> zPath;
}
</pre><h2>Installing the sample code</h2>Extract the code into a URLRewriter folder, then turn this into a virtual directory using the Internet Information Services MMC control panel applet. Compile the code use the 'Make Rewriter.bat' batch script into the bin sub-folder. Then add <i>bin/ThunderMain.URLRewriter.dll</i> to the Global Assembly Cache by copying and pasting the dll into %WINDIR%\assembly using Windows Explorer. Finally, navigate to <i>http://localhost/URLRewriter/default.aspx</i> and try the demo URLs listed. <br />
None will actually work because there's one last thing we have to be aware of... <br />
<h2>Finally</h2>There's one major caveat with all this. If you want to process a request with a file extension other than .aspx such as .asp or .html, then you need to change IIS to pass all requests through to the ASP.NET ISAPI extension. Unfortunately, you will need physical access to the server to perform this, which prevents you from simply XCOPY deploying your code to an ISP. <br />
<div closure_uid_jovsab="241"><br />
</div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFVj9QvepjUtkSv6d5Cl0iVdpo9MMPXZF3FH0dGVfMNg4ohxSmgAa0lOH5ep5oVTHgczDb6G4IZQqyL4wY07mBrW3T7gjueusCfpRtPEcnlVCS8RCUVDeDUyqox5bc5H73PM0hxoVUyOY/s1600/IIS.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFVj9QvepjUtkSv6d5Cl0iVdpo9MMPXZF3FH0dGVfMNg4ohxSmgAa0lOH5ep5oVTHgczDb6G4IZQqyL4wY07mBrW3T7gjueusCfpRtPEcnlVCS8RCUVDeDUyqox5bc5H73PM0hxoVUyOY/s320/IIS.png" t$="true" width="320" /></a></div>We've added the HEAD, GET and POST verbs to all files with .* file extension (ie all files) and mapped those to the ASP.NET ISAPI extension - <i>aspnet_isapi.dll</i>. <br />
<div closure_uid_jovsab="240"><br />
</div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhL2AvWJPq8fOlWstw8xWCfotiSvhOaQq35ibJbS-5CSlbGt0vDQIJZj9OFEui1tvWDx4Y7eWttnxsRvYyB_JAyGAiR_hXqTaNKcFyQuYs7NzvrZG2uPJAVoR84HfGHcVS0hwgPjYMmKT0/s1600/IIS2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhL2AvWJPq8fOlWstw8xWCfotiSvhOaQq35ibJbS-5CSlbGt0vDQIJZj9OFEui1tvWDx4Y7eWttnxsRvYyB_JAyGAiR_hXqTaNKcFyQuYs7NzvrZG2uPJAVoR84HfGHcVS0hwgPjYMmKT0/s320/IIS2.png" t$="true" width="285" /></a></div>The complete range of mappings, including the new .* mapping. </div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com5tag:blogger.com,1999:blog-9073788502453820575.post-54398543155342444392011-08-05T04:20:00.000-07:002011-09-16T11:33:31.063-07:00How to Attach Holidays Detail with Calendar Control?<div dir="ltr" style="text-align: left;" trbidi="on">HTML Code :<br />
<br />
<span style="color: blue;"><</span><span style="color: maroon;">body</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="111" style="color: blue;"><</span><span style="color: maroon;">form</span> <span style="color: red;">id</span>="<span style="color: blue;">form1</span>" <span style="color: red;">runat</span>="<span style="color: blue;">server</span>"<span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="112" style="color: blue;"><</span><span style="color: maroon;">div</span><span closure_uid_bwndv8="113" style="color: blue;">></span><br />
<span closure_uid_bwndv8="113" style="color: blue;"></span><span style="color: blue;"><</span><span style="color: maroon;">div</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="114" style="color: blue;"><</span>p <span style="color: red;">style</span>="<span style="color: blue;">text-align: center</span>"<span style="color: blue;">></span><span closure_uid_bwndv8="115" style="color: blue;"><</span>b<span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">asp:Label</span> <span style="color: red;">ID</span>="<span style="color: blue;">Label1</span>" <span style="color: red;">runat</span>="<span style="color: blue;">server</span>" <span style="color: red;">Font-Bold</span>="<span style="color: blue;">True</span>" <span style="color: red;">Font-Names</span>="<span style="color: blue;">Arial Black</span>" <span style="color: red;">Font-Size</span>="<span style="color: blue;">Medium</span>"<span style="color: red;">ForeColor</span>="<span style="color: blue;">#0066FF</span>"<span style="color: blue;">></span>Indian List of Holidays 2011<span style="color: blue;"><</span>/<span style="color: maroon;">asp:Label</span><span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">br</span> /<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">b</span><span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">p</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="116" style="color: blue;"><</span><span style="color: maroon;">asp:Calendar</span> <span style="color: red;">ID</span>="<span style="color: blue;">Calendar1</span>" <span style="color: red;">runat</span>="<span style="color: blue;">server</span>" <span style="color: red;">BackColor</span>="<span style="color: blue;">#FFFFCC</span>" <span style="color: red;">BorderColor</span>="<span style="color: blue;">#FFCC66</span>"<span style="color: red;">BorderWidth</span>="<span style="color: blue;">1px</span>" <span style="color: red;">DayNameFormat</span>="<span style="color: blue;">Shortest</span>" <span style="color: red;">Font-Names</span>="<span style="color: blue;">Verdana</span>" <span style="color: red;">Font-Size</span>="<span style="color: blue;">8pt</span>"<span style="color: red;">ForeColor</span>="<span style="color: blue;">#663399</span>" <span style="color: red;">ShowGridLines</span>="<span style="color: blue;">True</span>" <span style="color: red;">OnDayRender</span>="<span style="color: blue;">Calendar1_DayRender</span>" <span style="color: red;">OnSelectionChanged</span>="<span style="color: blue;">Calendar1_SelectionChanged</span>"<span style="color: red;">OnVisibleMonthChanged</span>="<span style="color: blue;">Calendar1_VisibleMonthChanged</span>"<span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">SelectedDayStyle</span> <span style="color: red;">BackColor</span>="<span style="color: blue;">#CCCCFF</span>" <span style="color: red;">Font-Bold</span>="<span style="color: blue;">True</span>" /<span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="117" style="color: blue;"><</span><span style="color: maroon;">SelectorStyle</span> <span style="color: red;">BackColor</span>="<span style="color: blue;">#FFCC66</span>" /<span closure_uid_bwndv8="118" style="color: blue;">></span><br />
<span closure_uid_bwndv8="118" style="color: blue;"></span><span style="color: blue;"><</span><span style="color: maroon;">TodayDayStyle</span> <span style="color: red;">BackColor</span>="<span style="color: blue;">#FFCC66</span>" <span style="color: red;">ForeColor</span>="<span style="color: blue;">White</span>" /<span closure_uid_bwndv8="119" style="color: blue;">></span><br />
<span closure_uid_bwndv8="119" style="color: blue;"></span><span style="color: blue;"><</span><span style="color: maroon;">OtherMonthDayStyle</span> <span style="color: red;">ForeColor</span>="<span style="color: blue;">#CC9966</span>" /<span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">NextPrevStyle</span> <span style="color: red;">Font-Size</span>="<span style="color: blue;">9pt</span>" <span style="color: red;">ForeColor</span>="<span style="color: blue;">#FFFFCC</span>" /<span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="120" style="color: blue;"><</span><span style="color: maroon;">DayHeaderStyle</span> <span style="color: red;">BackColor</span>="<span style="color: blue;">#FFCC66</span>" <span style="color: red;">Font-Bold</span>="<span style="color: blue;">True</span>" <span style="color: red;">Height</span>="<span style="color: blue;">1px</span>" /<span closure_uid_bwndv8="121" style="color: blue;">></span><br />
<span closure_uid_bwndv8="121" style="color: blue;"></span><span style="color: blue;"><</span><span style="color: maroon;">TitleStyle</span> <span style="color: red;">BackColor</span>="<span style="color: blue;">#990000</span>" <span style="color: red;">Font-Bold</span>="<span style="color: blue;">True</span>" <span style="color: red;">Font-Size</span>="<span style="color: blue;">9pt</span>" <span style="color: red;">ForeColor</span>="<span style="color: blue;">#FFFFCC</span>" /<span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="122" style="color: blue;"><</span>/<span style="color: maroon;">asp:Calendar</span><span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">br</span> /<span style="color: blue;">></span><span style="color: blue;"><</span>b<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">b</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="123" style="color: blue;"><</span><span style="color: maroon;">asp:Label</span> <span style="color: red;">ID</span>="<span style="color: blue;">LabelAction</span>" <span style="color: red;">runat</span>="<span style="color: blue;">server</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">asp:Label</span><span style="color: blue;">></span><span style="color: blue;"><</span><span style="color: maroon;">br</span> /<span closure_uid_bwndv8="124" style="color: blue;">></span><br />
<span closure_uid_bwndv8="124" style="color: blue;"></span><span style="color: blue;"><</span>/<span style="color: maroon;">b</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="125" style="color: blue;"><</span>/<span style="color: maroon;">div</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="126" style="color: blue;"><</span>/<span style="color: maroon;">div</span><span style="color: blue;">></span><br />
<span style="color: blue;"></span><span closure_uid_bwndv8="127" style="color: blue;"><</span>/<span style="color: maroon;">form</span><span closure_uid_bwndv8="128" style="color: blue;">></span><br />
<div closure_uid_bwndv8="129"><span closure_uid_bwndv8="128" style="color: blue;"></span><span style="color: blue;"><</span>/<span style="color: maroon;">body</span><span style="color: blue;">></span></div><div closure_uid_bwndv8="129"><br />
</div><div closure_uid_bwndv8="129"><br />
</div><div closure_uid_bwndv8="129"><span style="color: blue;">C# Code:</span></div><div closure_uid_bwndv8="129"><br />
</div><span style="color: blue;"><span style="color: blue;">using</span> System;<br />
<span style="color: blue;">using</span> System.Configuration;<br />
<span style="color: blue;">using</span> System.Data;<br />
<span style="color: blue;">using</span> System.Linq;<br />
<span style="color: blue;">using</span> System.Web;<br />
<span style="color: blue;">using</span> System.Web.Security;<br />
<span style="color: blue;">using</span> System.Web.UI;<br />
<span style="color: blue;">using</span> System.Web.UI.HtmlControls;<br />
<span style="color: blue;">using</span> System.Web.UI.WebControls;<br />
<span style="color: blue;">using</span> System.Web.UI.WebControls.WebParts;<br />
<span style="color: blue;">using</span> System.Xml.Linq;<br />
<span style="color: blue;">using</span> System.Collections;<br />
<br />
<span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> calender : System.Web.UI.Page<br />
{<br />
Hashtable HolidayList;<br />
<br />
<blockquote dir="ltr" style="margin-right: 0px;"><span style="color: blue;">protected</span> <span style="color: blue;">void</span> Page_Load(<span style="color: blue;">object</span> sender, EventArgs e)<br />
{<br />
HolidayList = Getholiday();<br />
Calendar1.Caption = <span style="color: maroon;">"Calender - Author: Espranza"</span>;<br />
Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;<br />
Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;<br />
Calendar1.TitleFormat = TitleFormat.MonthYear;<br />
Calendar1.ShowGridLines = <span style="color: maroon;">true</span>;<br />
Calendar1.DayStyle.Height = <span style="color: blue;">new</span> Unit(<span style="color: maroon;">50</span>);<br />
Calendar1.DayStyle.Width = <span style="color: blue;">new</span> Unit(<span style="color: maroon;">50</span>);<br />
Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Center;<br />
Calendar1.DayStyle.VerticalAlign = VerticalAlign.Middle;<br />
Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue;<br />
}<br />
<br />
<span style="color: blue;">protected</span> <span style="color: blue;">void</span> Calendar1_DayRender(<span style="color: blue;">object</span> sender, DayRenderEventArgs e)<br />
{<span style="color: blue;">if</span> (HolidayList[e.Day.Date.ToShortDateString()] != <span style="color: blue;">null</span>)<br />
{<br />
Literal literal1 = <span style="color: blue;">new</span> Literal();<br />
literal1.Text = <span style="color: maroon;">"<br/>"</span>;<br />
e.Cell.Controls.Add(literal1);<br />
Label label1 = <span style="color: blue;">new</span> Label();<br />
label1.Text = (<span style="color: blue;">string</span>)HolidayList[e.Day.Date.ToShortDateString()];<br />
label1.Font.Size = <span style="color: blue;">new</span> FontUnit(FontSize.Small);<br />
label1.Font.Bold = <span style="color: maroon;">true</span>;<br />
e.Cell.Controls.Add(label1);<br />
}<br />
}<br />
<br />
<span style="color: blue;">protected</span> <span style="color: blue;">void</span> Calendar1_SelectionChanged(<span style="color: blue;">object</span> sender, EventArgs e)<br />
{<br />
LabelAction.Text = <span style="color: maroon;">"Date changed to :"</span> + Calendar1.SelectedDate.ToShortDateString();<br />
}<br />
<br />
<span style="color: blue;">protected</span> <span style="color: blue;">void</span> Calendar1_VisibleMonthChanged(<span style="color: blue;">object</span> sender, MonthChangedEventArgs e)<br />
{<br />
LabelAction.Text = <span style="color: maroon;">"Month changed to :"</span> + e.NewDate.ToShortDateString();<br />
}<br />
<br />
<span style="color: blue;">private</span> Hashtable Getholiday()<br />
{<br />
Hashtable holiday = <span style="color: blue;">new</span> Hashtable();<br />
holiday[<span style="color: maroon;">"1/1/2011"</span>] = <span style="color: maroon;">"New Year"</span>;<br />
holiday[<span style="color: maroon;">"1/5/2011"</span>] = <span style="color: maroon;">"Guru Govind Singh Jayanti"</span>;<br />
holiday[<span style="color: maroon;">"1/8/2011"</span>] = <span style="color: maroon;">"Muharam (Al Hijra)"</span>;<br />
holiday[<span style="color: maroon;">"1/14/2011"</span>] = <span style="color: maroon;">"Pongal"</span>;<br />
holiday[<span style="color: maroon;">"1/26/2011"</span>] = <span style="color: maroon;">"Republic Day"</span>;<br />
holiday[<span style="color: maroon;">"2/23/2011"</span>] = <span style="color: maroon;">"Maha Shivaratri"</span>;<br />
holiday[<span style="color: maroon;">"3/10/2011"</span>] = <span style="color: maroon;">"Milad un Nabi (Birthday of the Prophet"</span>;<br />
holiday[<span style="color: maroon;">"3/21/2011"</span>] = <span style="color: maroon;">"Holi"</span>;<br />
holiday[<span style="color: maroon;">"3/21/2011"</span>] = <span style="color: maroon;">"Telugu New Year"</span>;<br />
holiday[<span style="color: maroon;">"4/3/2011"</span>] = <span style="color: maroon;">"Ram Navmi"</span>;<br />
holiday[<span style="color: maroon;">"4/7/2011"</span>] = <span style="color: maroon;">"Mahavir Jayanti"</span>;<br />
holiday[<span style="color: maroon;">"4/10/2011"</span>] = <span style="color: maroon;">"Good Friday"</span>;<br />
holiday[<span style="color: maroon;">"4/12/2011"</span>] = <span style="color: maroon;">"Easter"</span>;<br />
holiday[<span style="color: maroon;">"4/14/2011"</span>] = <span style="color: maroon;">"Tamil New Year and Dr Ambedkar Birth Day"</span>;<br />
holiday[<span style="color: maroon;">"5/1/2011,5/1/2011"</span>] = <span style="color: maroon;">"May Day"</span>;<br />
holiday[<span style="color: maroon;">"5/9/2011"</span>] = <span style="color: maroon;">"Buddha Jayanti and Buddha Purnima"</span>;<br />
holiday[<span style="color: maroon;">"6/24/2011"</span>] = <span style="color: maroon;">"Rath yatra"</span>;<br />
holiday[<span style="color: maroon;">"8/13/2011"</span>] = <span style="color: maroon;">"Krishna Jayanthi"</span>;<br />
holiday[<span style="color: maroon;">"8/14/2011"</span>] = <span style="color: maroon;">"Janmashtami"</span>;<br />
holiday[<span style="color: maroon;">"8/15/2011"</span>] = <span style="color: maroon;">"Independence Day"</span>;<br />
holiday[<span style="color: maroon;">"8/19/2011"</span>] = <span style="color: maroon;">"Parsi New Year"</span>;<br />
holiday[<span style="color: maroon;">"8/23/2011"</span>] = <span style="color: maroon;">"Vinayaka Chaturthi"</span>;<br />
holiday[<span style="color: maroon;">"9/2/2011"</span>] = <span style="color: maroon;">"Onam"</span>;<br />
holiday[<span style="color: maroon;">"9/5/2011"</span>] = <span style="color: maroon;">"Teachers Day"</span>;<br />
holiday[<span style="color: maroon;">"9/21/2011"</span>] = <span style="color: maroon;">"Ramzan"</span>;<br />
holiday[<span style="color: maroon;">"9/27/2011"</span>] = <span style="color: maroon;">"Ayutha Pooja"</span>;<br />
holiday[<span style="color: maroon;">"9/28/2011"</span>] = <span style="color: maroon;">"Vijaya Dasami (Dusherra)"</span>;<br />
holiday[<span style="color: maroon;">"10/2/2011"</span>] = <span style="color: maroon;">"Gandhi Jayanti"</span>;<br />
holiday[<span style="color: maroon;">"10/17/2011"</span>] = <span style="color: maroon;">"Diwali & Govardhan Puja"</span>;<br />
holiday[<span style="color: maroon;">"10/19/2011"</span>] = <span style="color: maroon;">"Bhaidooj"</span>;<br />
holiday[<span style="color: maroon;">"11/2/2011"</span>] = <span style="color: maroon;">"Guru Nanak Jayanti"</span>;<br />
holiday[<span style="color: maroon;">"11/14/2011"</span>] = <span style="color: maroon;">"Children's Day"</span>;<br />
holiday[<span style="color: maroon;">"11/28/2011"</span>] = <span style="color: maroon;">"Bakrid"</span>;<br />
holiday[<span style="color: maroon;">"12/25/2011"</span>] = <span style="color: maroon;">"Christmas Day"</span>;<br />
holiday[<span style="color: maroon;">"12/28/2011"</span>] = <span style="color: maroon;">"Muharram"</span>;<span style="color: blue;">return</span> holiday;<br />
}<br />
}</blockquote></span></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-54467458808935102002011-07-19T12:16:00.000-07:002011-09-16T11:36:31.284-07:00Windows Communication Foundation Architecture Overview<div dir="ltr" style="text-align: left;" trbidi="on"><style>
<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} h4 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->
</style><br />
<h4></h4><h2><style>
<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} h4 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:"Times New Roman"; font-weight:bold;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->
</style> </h2><h4>Contents</h4><a href="http://msdn.microsoft.com/en-us/library/aa480210.aspx#wcfarch_topic1">Introduction</a><br />
<a href="http://msdn.microsoft.com/en-us/library/aa480210.aspx#wcfarch_topic2">WCF Fundamentals</a><br />
<a href="http://msdn.microsoft.com/en-us/library/aa480210.aspx#wcfarch_topic3">Code Examples</a><br />
<a href="http://msdn.microsoft.com/en-us/library/aa480210.aspx#wcfarch_topic4">Summary</a><br />
<br />
<h2>Introduction</h2>This document provides a high-level view of the Windows Communication Foundation (WCF) architecture. It is intended to explain key concepts in WCF and how they fit together. There are a few code examples to further illustrate the concepts, but code is not the emphasis of this document. <br />
The rest of this document is organized in two main sections: <br />
<ul type="disc"><li class="MsoNormal">WCF Fundamentals: Covers key concepts in WCF, terms, and architectural components.</li>
</ul><span style="font-family: "Times New Roman"; font-size: 12pt;">Code Examples: Provides a few short code examples intended to illustrate and reify the concepts covered in WCF Fundamentals.</span><br />
<br />
<style>
<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} h2 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:2; font-size:18.0pt; font-family:"Times New Roman"; font-weight:bold;} h3 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:"Times New Roman"; font-weight:bold;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} p.fig, li.fig, div.fig {mso-style-name:fig; mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} p.label, li.label, div.label {mso-style-name:label; mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->
</style> <br />
<h2>WCF Fundamentals</h2>A WCF Service is a program that exposes a collection of <i>Endpoint</i>s. Each Endpoint is a portal for communicating with the world. <br />
A Client is a program that exchanges messages with one or more Endpoints. A Client may also expose an Endpoint to receive Messages from a Service in a duplex message exchange pattern. <br />
The following sections describe these fundamentals in more detail.<br />
<h3>Endpoints</h3>A Service Endpoint has an <i>Address</i>, a <i>Binding</i>, and a <i>Contract</i>. <br />
The Endpoint's Address is a network address <i>where</i> the Endpoint resides. The <b>EndpointAddress</b> class represents a WCF Endpoint Address.<br />
The Endpoint's Binding specifies <i>how</i> the Endpoint communicates with the world including things like transport protocol (e.g., TCP, HTTP), encoding (e.g., text, binary), and security requirements (e.g., SSL, SOAP message security). The <b>Binding</b> class represents a WCF Binding.<br />
The Endpoint's Contract specifies <i>what</i> the Endpoint communicates and is essentially a collection of messages organized in operations that have basic Message Exchange Patterns (MEPs) such as one-way, duplex, and request/reply. The <b>ContractDescription</b> class represents a WCF Contract.<br />
The <b>ServiceEndpoint</b> class represents an Endpoint and has an EndpointAddress, a Binding, and a ContractDescription corresponding to the Endpoint's Address, Binding, and Contract, respectively<br />
<br />
<br />
<style>
<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} h4 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:"Times New Roman"; font-weight:bold;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->
</style> <br />
<h4>EndpointAddress</h4>An EndpointAddress is basically a URI, an Identity, and a collection of optional headers as shown in Figure 2.<br />
An Endpoint's security identity is normally its URI; however, in advanced scenarios the identity can be explicitly set independent of the URI using the <b>Identity</b> address property.<br />
The optional headers are used to provide additional addressing information beyond the Endpoint's URI. For example, address headers are useful for differentiating between multiple Endpoints that share the same address URI.<br />
<br />
<o:smarttagtype name="place" namespaceuri="urn:schemas-microsoft-com:office:smarttags"></o:smarttagtype><style>
<!-- /* Font Definitions */ @font-face {font-family:Helvetica; panose-1:2 11 5 4 2 2 2 2 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:Courier; panose-1:2 7 4 9 2 2 5 2 4 4; mso-font-charset:0; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:"Tms Rmn"; panose-1:2 2 6 3 4 5 5 2 3 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:Helv; panose-1:2 11 6 4 2 2 2 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:"New York"; panose-1:2 4 5 3 6 5 6 2 3 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:System; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:"MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-alt:"MS 明朝"; mso-font-charset:128; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134676480 16 0 131072 0;} @font-face {font-family:Batang; panose-1:2 3 6 0 0 1 1 1 1 1; mso-font-alt:바탕; mso-font-charset:129; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 151388160 16 0 524288 0;} @font-face {font-family:SimSun; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:宋体; mso-font-charset:134; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:PMingLiU; panose-1:2 1 6 1 0 1 1 1 1 1; mso-font-alt:新細明體; mso-font-charset:136; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:1 134742016 16 0 1048576 0;} @font-face {font-family:"MS Gothic"; panose-1:2 11 6 9 7 2 5 8 2 4; mso-font-alt:"MS ゴシック"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134676480 16 0 131072 0;} @font-face {font-family:Dotum; panose-1:2 11 6 0 0 1 1 1 1 1; mso-font-alt:돋움; mso-font-charset:129; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 151388160 16 0 524288 0;} @font-face {font-family:SimHei; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:黑体; mso-font-charset:134; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:MingLiU; panose-1:2 1 6 9 0 1 1 1 1 1; mso-font-alt:細明體; mso-font-charset:136; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134742016 16 0 1048576 0;} @font-face {font-family:Mincho; panose-1:2 2 6 9 4 3 5 8 3 5; mso-font-alt:明朝; mso-font-charset:128; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134676480 16 0 131072 0;} @font-face {font-family:Gulim; panose-1:2 11 6 0 0 1 1 1 1 1; mso-font-alt:굴림; mso-font-charset:129; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 151388160 16 0 524288 0;} @font-face {font-family:Century; panose-1:2 4 6 3 5 7 5 2 3 3; mso-font-charset:0; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:"Angsana New"; panose-1:2 2 6 3 5 4 5 2 3 4; mso-font-charset:222; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:16777217 0 0 0 65536 0;} @font-face {font-family:"Cordia New"; panose-1:2 11 3 4 2 2 2 2 2 4; mso-font-charset:222; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:16777217 0 0 0 65536 0;} @font-face {font-family:Mangal; panose-1:0 0 4 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:32768 0 0 0 0 0;} @font-face {font-family:Latha; panose-1:2 0 4 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:1048576 0 0 0 0 0;} @font-face {font-family:Sylfaen; panose-1:1 10 5 2 5 3 6 3 3 3; mso-font-charset:0; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:16778883 0 512 0 13 0;} @font-face {font-family:Vrinda; panose-1:0 0 4 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Raavi; panose-1:2 0 5 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Shruti; panose-1:2 0 5 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Sendnya; panose-1:0 0 4 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Gautami; panose-1:2 0 5 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Tunga; panose-1:0 0 4 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:"Estrangelo Edessa"; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Kartika; panose-1:1 1 1 0 1 1 1 1 1 1; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:"Arial Unicode MS"; panose-1:2 11 6 4 2 2 2 2 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:Tahoma; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} h2 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:2; font-size:18.0pt; font-family:"Times New Roman"; font-weight:bold;} h3 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:"Times New Roman"; font-weight:bold;} h4 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:"Times New Roman"; font-weight:bold;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} code {font-family:"Courier New"; mso-ascii-font-family:"Courier New"; mso-fareast-font-family:"Courier New"; mso-hansi-font-family:"Courier New"; mso-bidi-font-family:"Courier New";} pre {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; font-size:10.0pt; font-family:"Courier New"; mso-fareast-font-family:"Courier New";} p.fig, li.fig, div.fig {mso-style-name:fig; mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} p.label, li.label, div.label {mso-style-name:label; mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} ins {mso-style-type:export-only; text-decoration:none;} span.msoIns {mso-style-type:export-only; mso-style-name:""; text-decoration:underline; text-underline:single;} span.msoDel {mso-style-type:export-only; mso-style-name:""; text-decoration:line-through; color:red;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:39325580; mso-list-type:hybrid; mso-list-template-ids:735068556 -380465938 428255248 216421344 694740758 -262895358 -1242165762 263508434 -120449168 1971870888;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} @list l1 {mso-list-id:181671699; mso-list-type:hybrid; mso-list-template-ids:-1047904938 1751699768 -177185356 1434101974 -2099456934 929566534 1482357482 -1443347570 387240752 1822468668;} @list l1:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l2 {mso-list-id:208686196; mso-list-type:hybrid; mso-list-template-ids:1887215530 -624531386 -1573721312 -647578836 578959852 1792724392 1633304484 -890096878 -943671002 1793252594;} @list l2:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} @list l3 {mso-list-id:421533474; mso-list-type:hybrid; mso-list-template-ids:1938949540 -644187562 -1023003516 -116505730 -930427004 -1733135920 -1343308102 1696119822 -270761906 -1402191590;} @list l3:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l4 {mso-list-id:571814761; mso-list-type:hybrid; mso-list-template-ids:165069830 -1879911262 -656515856 12750864 1494225292 1968232700 -91607920 78119014 -1608476728 1253488692;} @list l4:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l5 {mso-list-id:836379462; mso-list-type:hybrid; mso-list-template-ids:-144660092 -735303172 18763402 1504184362 -1363505090 196752412 1763589736 1829949202 -1859102728 750164342;} @list l5:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l6 {mso-list-id:866526640; mso-list-type:hybrid; mso-list-template-ids:791578090 -42588452 288554748 -1944042534 1971095462 1402259592 -1931182734 88753758 456456004 -1678474746;} @list l6:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} @list l7 {mso-list-id:999695759; mso-list-type:hybrid; mso-list-template-ids:467182870 -513375268 679792168 -1940883588 707921534 1501083884 -958476090 -1287481980 -2121896484 288015062;} @list l7:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l8 {mso-list-id:1315836710; mso-list-type:hybrid; mso-list-template-ids:-1169931102 1547883270 1156204376 -1917393298 -1735765462 -2061600144 -1542960592 1905033102 -67188244 365347532;} @list l8:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l9 {mso-list-id:1399212657; mso-list-type:hybrid; mso-list-template-ids:1357559244 -861495710 -1876519616 1400265296 861338692 1047434774 1756255962 1521520726 -796505968 1121109148;} @list l9:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l10 {mso-list-id:1682931486; mso-list-type:hybrid; mso-list-template-ids:-363961222 943892316 580574584 -919842256 -2008266220 1591373730 845989722 1935722012 -967558758 1181489826;} @list l10:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l10:level2 {mso-level-tab-stop:1.0in; mso-level-number-position:left; text-indent:-.25in;} @list l11 {mso-list-id:1732345135; mso-list-type:hybrid; mso-list-template-ids:1907661028 -664530656 999852118 -814328266 -1326036952 -2099857966 -718893544 -1680173380 624834976 545032306;} @list l11:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l12 {mso-list-id:1751388477; mso-list-type:hybrid; mso-list-template-ids:-40343906 485289264 -515987006 303988794 -1519220382 145639622 1567780960 -1983896864 1437637990 -500789756;} @list l12:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l12:level2 {mso-level-tab-stop:1.0in; mso-level-number-position:left; text-indent:-.25in;} @list l13 {mso-list-id:2104572888; mso-list-type:hybrid; mso-list-template-ids:520280302 -557005014 820395464 -1784780672 1304751698 -2079961532 399038452 1761742356 1656662478 -1110256260;} @list l13:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l14 {mso-list-id:2127195498; mso-list-type:hybrid; mso-list-template-ids:2019732588 1510353414 -1750015766 577653804 -958397748 -1013812776 151046132 2140698776 -300766958 -1535866252;} @list l14:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} -->
</style> <br />
<h4>Bindings</h4><div class="MsoNormal">A Binding has a name, a namespace, and a collection of composable binding elements (Figure 3). The Binding's name and namespace uniquely identify it in the service's metadata. Each binding element describes an aspect of <i>how</i> the Endpoint communicates with the world. </div><div class="fig"></div><br />
For example, Figure 4 shows a binding element collection containing three binding elements. The presence of each binding element describes part of the <i>how</i> of communicating with the Endpoint. The <b>TcpTransportBindingElement</b> indicates that the Endpoint will communicate with the world using TCP as the transport protocol. <b>ReliableSessionBindingElement</b> indicates that the Endpoint uses reliable messaging to provide message delivery assurances. <b>SecurityBindingElement</b> indicates that the Endpoint uses SOAP message security. Each binding element usually has properties that further describe the specifics of the <i>how</i> of communicating with the Endpoint. For example, the ReliableSessionBindingElement has an <b>Assurances</b> property that specifies the required message delivery assurances, such as none, at least once, at most once, or exactly once.<br />
<div class="fig"></div><br />
The order and types of binding elements in Bindings are significant: The collection of binding elements is used to build a communications stack ordered according to the order of binding elements in the binding elements collection. The last binding element to be added to the collection corresponds to the bottom component of the communications stack, while the first one corresponds to the top component. Incoming messages flow through the stack from the bottom upwards, while outgoing messages flow from the top downwards. Therefore the order of binding elements in the collection directly affects the order in which communications stack components process messages. Note that WCF provides a set of pre-defined bindings that can be used in the majority of scenarios instead of defining custom bindings.<br />
<h4>Contracts</h4>A WCF Contract is a collection of Operations that specifies <i>what</i> the Endpoint communicates to the outside world. Each operation is a simple message exchange, for example one-way or request/reply message exchange. <br />
The ContractDescription class is used to describe WCF Contracts and their operations. Within a ContractDescription, each <b>Contract</b> operation has a corresponding <b>OperationDescription</b> that describes aspects of the operation such as whether the operation is one-way or request/reply. Each OperationDescription also describes the messages that make up the operation using a collection of <b>MessageDescriptions</b>.<br />
<br />
A ContractDescription is usually created from an interface or class that defines the Contract using the WCF programming model. This type is annotated with <b>ServiceContractAttribute</b> and its methods that correspond to Endpoint Operations are annotated with <b>OperationContractAttribute</b>. You can also build a ContractDescription by hand without starting with a CLR type annotated with attributes.<br />
A <i>duplex</i> Contract defines two logical sets of operations: A set that the Service exposes for the Client to call and a set that the Client exposes for the Service to call. The programming model for defining a duplex Contract is to split each set in a separate type (each type must be a class or an interface) and annotate the contract that represents the service's operations with ServiceContractAttribute, referencing the contract that defines the client (or callback) operations. In addition, ContractDescription contains a reference to each of the types thereby grouping them into one duplex Contract.<br />
Similar to Bindings, each Contract has a Name and Namespace that uniquely identify it in the Service's metadata. <br />
Each Contract also has a collection of ContractBehaviors that are modules that modify or extend the contract's behavior. The next section covers behaviors in more detail.<br />
<h4>Behaviors</h4>Behaviors are types that modify or extend Service or Client functionality. For example, the metadata behavior that <b>ServiceMetadataBehavior</b> implemented controls whether the Service publishes metadata. Similarly, the security behavior controls impersonation and authorization, while the transactions behavior controls enlisting in and auto-completing transactions. <br />
Behaviors also participate in the process of building the channel and can modify that channel based on user-specified settings and/or other aspects of the Service or Channel.<br />
A Service Behavior is a type that implements <b>IServiceBehavior</b> and applies to Services. Similarly, a Channel Behavior is a type that implements <b>IChannelBehavior</b> and applies to Client Channels.<br />
<h4>Service and Channel Descriptions</h4>The <b>ServiceDescription</b> class is an in-memory structure that describes a WCF Service including the Endpoints exposed by the Service, the Behaviors applied to the Service, and the type (a class) that implements the Service (see Figure 6). ServiceDescription is used to create metadata, code/config, and channels.<br />
You can build this ServiceDescription object by hand. You can also create it from a type annotated with certain WCF attributes, which is the more common scenario. The code for this type can be written by hand or generated from a WSDL document using a WCF tool called svcutil.exe. <br />
Although ServiceDescription objects can be created and populated explicitly, they are often created behind the scenes as part of running the Service.<br />
<br />
Similarly on the client side, a <b>ChannelDescription</b> describes a WCF Client's Channel to a specific Endpoint (Figure 7). The ChannelDescription class has a collection of IchannelBehaviors, which are Behaviors applied to the Channel. It also has a ServiceEndpoint that describes the Endpoint with which the Channel will communicate.<br />
Note that, unlike ServiceDescription, ChannelDescription contains only one ServiceEndpoint that represents the target Endpoint with which the Channel will communicate.<br />
<br />
<h4>WCF Runtime</h4>The WCF Runtime is the set of objects responsible for sending and receiving messages. For example, things like formatting messages, applying security, and transmitting and receiving messages using various transport protocols, as well as dispatching received messages to the appropriate operation, all fall within the WCF runtime. The following sections explain the key concepts of the WCF runtime.<br />
<h4>Message</h4>The WCF Message is the unit of data exchange between a Client and an Endpoint. A Message is essentially an in-memory representation of a SOAP message <b>InfoSet</b>. Note that Message is not tied to text XML. Rather, depending on which encoding mechanism is used, a Message can be serialized using the WCF binary format, text XML, or any other custom format.<br />
<h4>Channels</h4>Channels are the core abstraction for sending Messages to and receiving Messages from an Endpoint. Broadly speaking, there are two categories of Channels: Transport Channels handle sending or receiving opaque octet streams using some form of transport protocol such as TCP, UDP, or MSMQ. Protocol Channels, on the other hand, implement a SOAP-based protocol by processing and possibly modifying messages. For example, the security Channel adds and processes SOAP message headers and may modify the body of the message by encrypting it. Channels are composable such that a Channel may be layered on top of another Channel that is in turn layered on top of a third Channel.<br />
<h4>EndpointListener</h4>An EndpointListener is the runtime equivalent of a ServiceEndpoint. The EndpointAddress, Contract, and Binding of ServiceEndpoint (representing <i>where</i>, <i>what</i> and <i>how</i>), correspond to the EndpointListener's listening address, message filtering and dispatch, and channel stack, respectively. The EndpointListener contains the Channel stack that is responsible for sending and receiving messages.<br />
<h4>ServiceHost and ChannelFactory</h4>The WCF Service runtime is usually created behind the scenes by calling <b>ServiceHost.Open</b>. <b>ServiceHost</b> (Figure 6) drives the creation of a ServiceDescription from on the Service type and populates the ServiceDescription's ServiceEndpoint collection with Endpoints defined in config or code, or both. ServiceHost then uses the ServiceDescription to create the channel stack in the form of an EndpointListener object for each ServiceEndpoint in the ServiceDescription. <br />
<br />
Similarly, on the client side, the Client runtime is created by a <b>ChannelFactory</b>, which is the Client's equivalent of ServiceHost.<br />
ChannelFactory drives the creation of a ChannelDescription based on a Contract type, a Binding, and an EndpointAddress. It then uses this ChannelDescription to create the Client's channel stack.<br />
Unlike the Service runtime, the Client runtime does not contain EndpointListeners because a Client always initiates connection to the Service, so there is no need to "listen" for incoming connections.<br />
<h2>Code Examples</h2>This section provides code examples that show how Services and Clients are built. These examples are intended to reify the above concepts and not to teach WCF programming.<br />
<h3>Defining and Implementing a Contract</h3>As mentioned above, the easiest way to define a contract is creating an interface or a class and annotating it with <b>ServiceContractAttribute</b>, allowing the system to easily create from it a ContractDescription. <br />
When using interfaces or classes to define contracts, each interface or class method that is a member of the contract must be annotated with <b>OperationContractAttribute</b>. For example:<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">using System.ServiceModel;</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//a WCF contract defined using an interface</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">[ServiceContract]</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public interface IMath</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> [OperationContract]</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> int Add(int x, int y);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre>Implementing the contract in this case is simply a matter of creating a class that implements <b>IMath</b>. That class becomes the WCF Service class. For example:<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//the service class implements the interface</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class MathService : IMath</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public int Add(int x, int y)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> { return x + y; }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><h3>Defining Endpoints and Starting the Service</h3>Endpoints can be defined in code or in config. In the example below, the <b>DefineEndpointImperatively</b> method shows the easiest way to define Endpoints in code and start the service. <br />
<b>DefineEndpointInConfig</b> method shows the equivalent endpoint defined in config (config example follows the code below). <br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class WCFServiceApp</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public void DefineEndpointImperatively()</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //create a service host for MathService</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> ServiceHost sh = new ServiceHost(typeof(MathService));</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //use the AddEndpoint helper method to</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //create the ServiceEndpoint and add it </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //to the ServiceDescription</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> sh.AddServiceEndpoint(</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> typeof(IMath), //contract type</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> new WSHttpBinding(), //one of the built-in bindings</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> "http://localhost/MathService/Ep1"); //the endpoint's address</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //create and open the service runtime</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> sh.Open();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public void DefineEndpointInConfig()</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //create a service host for MathService</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> ServiceHost sh = new ServiceHost (typeof(MathService));</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //create and open the service runtime</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> sh.Open();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><!-- configuration file used by above code --></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><configuration </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <system.serviceModel></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <services></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <!-- service element references the service type --></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <service type="MathService"></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <!-- endpoint element defines the ABC's of the endpoint --></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> <endpoint </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> address="http://localhost/MathService/Ep1"</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> binding="wsHttpBinding"</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> contract="IMath"/></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> </service></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> </services></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> </system.serviceModel></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"></configuration></pre><h3>Sending Messages to an Endpoint</h3>The code below shows two ways to send a message to the IMath endpoint. <b>SendMessageToEndpoint</b> hides the Channel creation, which happens behind the scenes while the <b>SendMessageToEndpointUsingChannel</b> example does it explicitly.<br />
The first example in SendMessageToEndpoint uses a tool named svcutil.exe and the Service's metadata to generate a Contract (IMath in this example), a proxy class (MathProxy in this example) that implements the Contract, and associated config (not shown here). Again, the Contract defined by IMath specifies the <i>what</i> (i.e., the operations that can be performed), while the generated config contains a Binding (the <i>how</i>) and an address (the <i>where</i>). <br />
Using this proxy class is simply a matter of instantiating it and calling the <b>Add</b> method. Behind the scenes, the proxy class will create a Channel and use that to communicate with the Endpoint.<br />
The second example in SendMessageToEndpointsUsingChannel below shows communicating with an Endpoint using ChannelFactory directly. In this example, instead of using a proxy class and config, a Channel is created directly using <b>ChannelFactory<IMath>.CreateChannel</b>. Also, instead of using config to define the Endpoint's address and Binding, the <b>ChannelFactory<IMath></b> constructor takes those two pieces of information as parameters. The third piece of information required to define an Endpoint, namely the Contract, is passed in as the type T.<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">using System.ServiceModel;</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//this contract is generated by svcutil.exe</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//from the service's metadata</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public interface IMath</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> [OperationContract]</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public int Add(int x, int y)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> { return x + y; }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//this class is generated by svcutil.exe</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//from the service's metadata</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">//generated config is not shown here</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class MathProxy : IMath</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> ...</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class WCFClientApp</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public void SendMessageToEndpoint()</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //this uses a proxy class that was</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //created by svcutil.exe from the service's metadata</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> MathProxy proxy = new MathProxy();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> int result = proxy.Add(35, 7);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public void SendMessageToEndpointUsingChannel()</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //this uses ChannelFactory to create the channel</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //you must specify the address, the binding and </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> //the contract type (IMath)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> ChannelFactory<IMath> factory=new ChannelFactory<IMath>(</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> new WSHttpBinding(),</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> new EndpointAddress("http://localhost/MathService/Ep1"));</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> IMath channel=factory.CreateChannel();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> int result=channel.Add(35,7);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> factory.Close();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><o:p> </o:p></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><h3>Defining a Custom Behavior</h3>Defining a custom Behavior is a matter of implementing IServiceBehavior (or IChannelBehavior for client-side behaviors). The code below shows an example behavior that implements IServiceBehavior. In IServiceBehavior.ApplyBehavior, the code inspects the ServiceDescription and writes out the Address, Binding, and Contract of each ServiceEndpoint, as well as the name of each Behavior in the ServiceDescription.<br />
This particular behavior is also an attribute (inherits from <b>System.Attribute</b>), making it possible to apply declaratively as will be shown below. However, behaviors are not required to be attributes.<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">[AttributeUsageAttribute(</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> AttributeTargets.Class,</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> AllowMultiple=false,</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Inherited=false)]</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class InspectorBehavior : System.Attribute, </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> System.ServiceModel.IServiceBehavior</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public void ApplyBehavior(</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> ServiceDescription description,</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Collection<DispatchBehavior> behaviors)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("-------- Endpoints ---------");</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> foreach (ServiceEndpoint endpoint in description.Endpoints)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("--> Endpoint");</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("Endpoint Address: {0}", </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> endpoint.Address);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("Endpoint Binding: {0}", </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> endpoint.Binding.GetType().Name);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("Endpoint Contract: {0}", </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> endpoint.Contract.ContractType.Name);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("-------- Service Behaviors --------");</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> foreach (IServiceBehavior behavior in description.Behaviors)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> {</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("--> Behavior");</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine("Behavior: {0}", behavior.GetType().Name);</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> Console.WriteLine();</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><h3>Applying a Custom Behavior</h3>All behaviors can be applied imperatively by adding an instance of the behavior to the ServiceDescription (or the ChannelDescription on the client side). For example, to apply the <b>InspectorBehavior</b> imperatively you would write:<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">ServiceHost sh = new ServiceHost(typeof(MathService));</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">sh.AddServiceEndpoint(</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> typeof(IMath), </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> new WSHttpBinding(),</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> "http://localhost/MathService/Ep1"); </pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><b><span style="font-family: "Courier New";">//Add the behavior imperatively</span></b></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><b><span style="font-family: "Courier New";">InspectorBehavior behavior = new InspectorBehavior();</span></b></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"><b><span style="font-family: "Courier New";">sh.Description.Behaviors.Add(behavior);</span></b></pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">sh.Open();</pre>Additionally, behaviors that inherit from System.Attribute may be applied declaratively to the service. For example, because InspectorBehavior inherits from System.Attribute, it can be applied declaratively like this:<br />
<pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">[InspectorBehavior]</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">public class MathService : IMath</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">{</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> public int Add(int x, int y)</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);"> { return x + y; }</pre><pre style="-moz-background-inline-policy: -moz-initial; background: none repeat scroll 0% 0% rgb(221, 221, 221);">}</pre><h2>Summary</h2>WCF Services expose a collection of Endpoints where each Endpoint is a portal for communicating with the world. Each Endpoint has an Address, a Binding, and a Contract (ABC). The Address is <i>where</i> the Endpoint resides, the Binding is <i>how</i> the Endpoint communicates, and the Contract is <i>what</i> the Endpoint communicates.<br />
On the Service, a ServiceDescription holds the collection of ServiceEndpoints each describing an Endpoint that the Service exposes. From this description, ServiceHost creates a runtime that contains an EndpointListener for each ServiceEndpoint in the ServiceDescription. The Endpoint's address, Binding, and Contract (representing the <i>where</i>, <i>what</i>, and <i>how</i>) correspond to the EndpointListener's listening address, message filtering and dispatch, and channel stack, respectively. <br />
Similarly, on the Client, a ChannelDescription holds the one ServiceEndpoint with which the Client communicates. From this ChannelDescription, ChannelFactory creates the channel stack that can communicate with the Service's Endpoint.</div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0tag:blogger.com,1999:blog-9073788502453820575.post-63256529712768223392011-07-05T04:12:00.000-07:002011-09-16T11:38:47.514-07:00Sql Helper Class<div dir="ltr" style="text-align: left;" trbidi="on">using System;<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
using System.Configuration;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
namespace MyClassLibrary.DAL<br />
{<br />
public class Database : IDisposable<br />
{<br />
private SqlConnection connection = null;<br />
private String connectionString = "dbConnection";<br />
public String ConnectionString<br />
{<br />
set { connectionString = value; }<br />
}<br />
#region Constructors<br />
/// <summary><br />
/// Default Constructor.<br />
/// </summary><br />
public Database() { }<br />
/// <summary><br />
/// Parameteraize constructor. It takes connection string as parameter.<br />
/// </summary><br />
/// <remarks>If connection string is not specified then Default connection string is "dbConnection"</remarks><br />
/// <param name="p_connectionString"></param><br />
public Database(string p_connectionString) { if (!string.IsNullOrEmpty(p_connectionString.Trim()))connectionString = p_connectionString; }<br />
#endregion<br />
#region AddParameter Methods<br />
private void AddParameter(SqlCommand command, string parameterName, SqlDbType dbType, int size, ParameterDirection direction,<br />
byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)<br />
{<br />
SqlParameter p = new SqlParameter(parameterName, dbType, size, direction, precision, scale, sourceColumn,<br />
sourceVersion, true, value, null, null, null);<br />
command.Parameters.Add(p);<br />
}<br />
public void AddParameter(SqlCommand command, string parameterName, SqlDbType dbType, int size, ParameterDirection direction, object value)<br />
{<br />
AddParameter(command, parameterName, dbType, size, direction, 0, 0, null, DataRowVersion.Current, value);<br />
}<br />
public void AddInParameter(SqlCommand command, string parameterName, SqlDbType dbType, object value)<br />
{<br />
AddParameter(command, parameterName, dbType, 0, ParameterDirection.Input, value);<br />
}<br />
public void AddOutParameter(SqlCommand command, string parameterName, SqlDbType dbType, int size)<br />
{<br />
AddParameter(command, parameterName, dbType, size, ParameterDirection.Output, null);<br />
}<br />
public object GetParameterValue(SqlCommand command, string parameterName)<br />
{<br />
return command.Parameters[parameterName].Value;<br />
}<br />
#endregion<br />
#region Methods for Generating SqlCommand<br />
private SqlCommand PrepareCommand(CommandType commandType, string commandText)<br />
{<br />
if (connection == null)<br />
{<br />
string _connectionString = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;<br />
connection = new SqlConnection(_connectionString);<br />
}<br />
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)<br />
{ <br />
connection.Open();<br />
}<br />
SqlCommand command = new SqlCommand(commandText, connection);<br />
command.CommandType = commandType;<br />
return command;<br />
}<br />
public SqlCommand GetStoreProcedureCommand(string spname)<br />
{ <br />
return PrepareCommand(CommandType.StoredProcedure, spname);<br />
}<br />
public SqlCommand GetSqlQueryCommand(string query)<br />
{<br />
return PrepareCommand(CommandType.Text, query);<br />
}<br />
#endregion<br />
#region Database Related Command Methods<br />
public int ExecuteNonQuery(SqlCommand command)<br />
{<br />
return command.ExecuteNonQuery();<br />
}<br />
public object ExecuteScalar(SqlCommand command)<br />
{<br />
return command.ExecuteScalar();<br />
}<br />
public SqlDataReader ExecuteReader(SqlCommand command)<br />
{<br />
return command.ExecuteReader(CommandBehavior.CloseConnection);<br />
}<br />
public SqlDataReader ExecuteReader(SqlCommand command, CommandBehavior commandBehavior)<br />
{<br />
return command.ExecuteReader(commandBehavior);<br />
}<br />
public DataTable LoadDataTable(SqlCommand command, string tableName)<br />
{<br />
using (SqlDataAdapter da = new SqlDataAdapter(command))<br />
{<br />
DataTable dt = new DataTable(tableName);<br />
da.Fill(dt);<br />
return dt;<br />
}<br />
}<br />
public DataSet LoadDataSet(SqlCommand command, string[] tableNames)<br />
{<br />
using (SqlDataAdapter da = new SqlDataAdapter(command))<br />
{<br />
DataSet ds = new DataSet();<br />
da.Fill(ds);<br />
for (int i = 0; i < ds.Tables.Count; i++)<br />
{<br />
ds.Tables[i].TableName = tableNames[i];<br />
}<br />
return ds;<br />
}<br />
}<br />
#endregion<br />
#region Methods for Transaction Handling<br />
private SqlTransaction PrepareTransaction(IsolationLevel isolationLevel)<br />
{<br />
if (connection == null)<br />
{<br />
string _connectionString = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;<br />
connection = new SqlConnection(_connectionString);<br />
}<br />
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)<br />
{<br />
connection.Open();<br />
}<br />
return connection.BeginTransaction(isolationLevel);<br />
}<br />
public SqlTransaction BeginTransaction()<br />
{<br />
return PrepareTransaction(IsolationLevel.ReadCommitted);<br />
}<br />
public SqlTransaction BeginTransaction(IsolationLevel isolationLevel)<br />
{<br />
return PrepareTransaction(isolationLevel);<br />
}<br />
public void Commit(SqlTransaction transaction)<br />
{<br />
if (transaction != null)<br />
transaction.Commit();<br />
}<br />
public void RollBack(SqlTransaction transaction)<br />
{<br />
if (transaction != null)<br />
transaction.Rollback();<br />
}<br />
#endregion<br />
#region IDisposable Members<br />
public void Dispose()<br />
{<br />
GC.SuppressFinalize(this);<br />
}<br />
#endregion<br />
~Database()<br />
{<br />
Dispose();<br />
}<br />
}</div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0Surat, Gujarat, India21.195 72.81944399999997621.0914665 72.698529499999978 21.2985335 72.940358499999974tag:blogger.com,1999:blog-9073788502453820575.post-10738457059681765422011-07-05T03:24:00.000-07:002011-09-16T11:40:32.173-07:00F# (F Sharp)<div dir="ltr" style="text-align: left;" trbidi="on"><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;"><b><span lang="EN">F#</span></b><span lang="EN"> (pronounced <b>F Sharp</b>) is a <a href="http://en.wikipedia.org/wiki/Multi-paradigm_programming_language" title="Multi-paradigm programming language"><span style="color: purple;">multi-paradigm programming language</span></a>, targeting the <a href="http://en.wikipedia.org/wiki/.NET_Framework" title=".NET Framework">.NET Framework</a>, that encompasses <a href="http://en.wikipedia.org/wiki/Functional_programming" title="Functional programming">functional programming</a> as well as <a href="http://en.wikipedia.org/wiki/Imperative_programming" title="Imperative programming"><span style="color: purple;">imperative</span></a> and <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a> disciplines. It is a variant of <a href="http://en.wikipedia.org/wiki/ML_%28programming_language%29" title="ML (programming language)">ML</a> and is largely compatible with the <a href="http://en.wikipedia.org/wiki/OCaml" title="OCaml">OCaml</a> implementation. F# was initially developed by <a href="http://en.wikipedia.org/wiki/Don_Syme" title="Don Syme">Don Syme</a> at <a href="http://en.wikipedia.org/wiki/Microsoft_Research" title="Microsoft Research">Microsoft Research</a> but is now being developed at <a href="http://en.wikipedia.org/wiki/Microsoft" title="Microsoft">Microsoft</a> Developer Division and is being distributed as a fully supported language in the <a href="http://en.wikipedia.org/wiki/.NET_Framework" title=".NET Framework">.NET Framework</a> and <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio" title="Microsoft Visual Studio">Visual Studio</a> as part of <a href="http://en.wikipedia.org/wiki/Visual_Studio_2010" title="Visual Studio 2010">Visual Studio 2010</a>.</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">F# is a <a href="http://en.wikipedia.org/wiki/Strongly_typed_programming_language" title="Strongly typed programming language">strongly typed</a> language that uses <a href="http://en.wikipedia.org/wiki/Type_inference" title="Type inference">type inference</a>. As a result, data types need not be explicitly declared by the programmer; they will be deduced by the compiler during compilation. However, F# also allows explicit data type declaration. Being a <a href="http://en.wikipedia.org/wiki/List_of_CLI_languages" title="List of CLI languages">CLI compliant language</a>, F# supports all CLI types and objects but it extends the type system and categorizes types as <a href="http://en.wikipedia.org/wiki/Immutable_object" title="Immutable object">immutable types</a> or mutable types. CLI objects classify as mutable types (which can be edited in-place), and are used to provide an <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a> model. Immutable types (editing such a type creates a new instance without overwriting the older one) are primarily used for <a href="http://en.wikipedia.org/wiki/Functional_programming" title="Functional programming">functional programming</a>.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">Like <a href="http://en.wikipedia.org/wiki/ML_%28programming_language%29" title="ML (programming language)">ML</a>, F# includes a functional programming component supporting <a href="http://en.wikipedia.org/wiki/Eager_evaluation" title="Eager evaluation">eager evaluation</a>. For functional programming, it provides several constructs and a set of immutable types: <i>tuples</i>, <i>records</i>, <i>discriminated unions</i> and <i>lists</i>.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">An <i><a href="http://en.wikipedia.org/wiki/N-tuple" title="N-tuple">n-tuple</a></i> represents a collection of <i>n</i> values, where <i>n</i> ≥ 0. The value <i>n</i> is called the <a href="http://en.wikipedia.org/wiki/Arity" title="Arity">arity</a> of the tuple. The type </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"><a href="http://en.wikipedia.org/wiki/Unit_type" title="Unit type">unit</a></span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> corresponds to the 0-tuple and it has one value only: </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">()</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">, which conveys no information. The type </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">unit</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> is used to implement functions that need no input and/or return no value. A 3-tuple would be represented as </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(A, B, C)</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">, where A, B and C are values of possibly different types. A tuple can be used only to store values when the number of values is known at design-time and stays constant throughout execution.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">A <i>record</i> is a specialization of tuple where the data members are named, as in </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{ Name:string; Age:int }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">. Records can be created as </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{ Name="AB"; Age=42 }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">. The </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">with</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> keyword is used to create a copy of a record, as in </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{ r with Name="CD" }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">, which creates a new record by copying </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">r</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> and changing the value of the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Name</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> field (assuming the record created in the last example was named </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">r</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">).</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">The <i>list</i> type is a regular <a href="http://en.wikipedia.org/wiki/Linked_list" title="Linked list">linked list</a> represented either using a </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">head::tail</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> notation (</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">::</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> is the <a href="http://en.wikipedia.org/wiki/Cons" title="Cons">cons</a> operator) or a shorthand as </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[item1; item2; item3]</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">. An empty list is written </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[]</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">The other sort of <a href="http://en.wikipedia.org/wiki/Algebraic_data_type" title="Algebraic data type">algebraic data type</a> mentioned, "<a href="http://en.wikipedia.org/wiki/Tagged_union" title="Tagged union">discriminated unions</a>" (<a href="http://en.wikipedia.org/wiki/Type-safe" title="Type-safe">type-safe</a> versions of <a href="http://en.wikipedia.org/wiki/Union_%28computer_science%29" title="Union (computer science)">C unions</a>), can be defined to hold a value of any of a pre-defined type. For example,</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">type</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> A </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | ConstructorX </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">of</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">string</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | ConstructorY </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">of</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">int</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">can hold values as instantiated by either constructor. The type of the values the constructors will act on can be defined as well.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">Constructors are used to create a view of the data type different from the actual implementation, as required for supporting the <a href="http://en.wikipedia.org/w/index.php?title=Active_Patterns&action=edit&redlink=1" title="Active Patterns (page does not exist)"><span style="color: #ba0000;">Active Patterns</span></a> concept.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup> Data types are created with the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">type</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> keyword. F# uses the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> keyword for binding type values to a name (variable).<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">F# uses <a href="http://en.wikipedia.org/wiki/Pattern_matching" title="Pattern matching">pattern matching</a> to resolve names into values. It is also used when accessing discriminated unions. Functions using a discriminated union provide different expressions to be invoked, associated with the data type in the union. The union is matched against these data types, and the expression associated with the match is invoked. F# also supports the <i>Active Patterns</i> pattern. It is used, for example, when a type needs to provide multiple views. For example, an exponential number will provide both the final value, as well as the base and exponents.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">All functions in F# are instances of the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">function</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> type, and are immutable as well.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup> Functions can either be <a href="http://en.wikipedia.org/wiki/Curry_function" title="Curry function">curried</a> or in uncurried form. Being an instance of a type, functions can be passed as arguments to other functions, resulting in <a href="http://en.wikipedia.org/wiki/Higher_order_function" title="Higher order function">higher order functions</a>. F# supports <a href="http://en.wikipedia.org/wiki/Lambda_calculus" title="Lambda calculus">lambda functions</a> and <a href="http://en.wikipedia.org/wiki/Closure_%28computing%29" title="Closure (computing)">closures</a> as well.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup> Like other functional programming languages, F# allows <a href="http://en.wikipedia.org/wiki/Function_composition" title="Function composition">function composition</a> using the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">>></span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> operator. Every statement in F#, including </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">if</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> statements and loops, is a composable expression with a definite return type as well.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup> Functions and expressions that do not return any value have a return type of </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">unit</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">The F# extended type system is implemented as <a href="http://en.wikipedia.org/wiki/Generic_programming" title="Generic programming">generic</a> .NET types. The <i>Record</i> type creates a .NET class with the field names as properties. <i>Tuples</i> are generic classes of type </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Tuple<_,_></span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">. The number of type parameters define the number and types of the elements in the tuple. Discriminated unions are implemented as <a href="http://en.wikipedia.org/wiki/Tagged_union" title="Tagged union">tagged unions</a>. Functions are of type </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">FastFunc<_,_></span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> with type parameters specifying the parameter and return types.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-3">[4]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">F#, like other .NET languages, can use .NET types and objects, using an imperative object-oriented style of programming. For imperative programming, F# supports </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">for</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> and </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">while</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> <a href="http://en.wikipedia.org/wiki/Control_flow#Loops" title="Control flow">loops</a>, <a href="http://en.wikipedia.org/wiki/Array_data_structure" title="Array data structure">arrays</a> (created with the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[| ... |]</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> syntax, and number sequences written in shorthand as in </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1 .. 25</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">) and support for creating </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Object types</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> (equivalent to .NET classes).<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup> F# also allows <a href="http://en.wikipedia.org/wiki/Metaprogramming" title="Metaprogramming">extending the syntax</a> to support embedding custom <a href="http://en.wikipedia.org/wiki/Domain-specific_language" title="Domain-specific language">domain-specific languages</a> within the F# language itself.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-overview-2">[3]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">F# provides <i>sequence expressions</i><sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-seq-4">[5]</a></sup> that allows for a defining a sequence block (</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">seq { ... }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> or </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[ ... ]</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> or </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[| ... |]</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">) encapsulating constructs (either functions, conditional expressions or loops) that act on a collection and another function (or lambda), such that the function is invoked on the results yielded from the collection collection-processing expressions. For example, </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">seq { for b in 0 .. 25 do if b < 15 then yield b*b }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> is a sequence expression that forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25. The sequence is <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" title="Lazy evaluation">lazily evaluated</a>, i. e., the collection is processed and results yielded on-demand. It can be used for filtering and is the basis of support for <a href="http://en.wikipedia.org/wiki/LINQ" title="LINQ">LINQ</a> queries. Sequence expressions are generalized as <i>Computation Expressions</i> which are equivalent to <a href="http://en.wikipedia.org/wiki/Monad_%28functional_programming%29" title="Monad (functional programming)">monads</a>.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-seq-4">[5]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">Sequence expressions and computation expressions are also used for creating <i>asynchronous workflows</i>.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-aw-5">[6]</a></sup> An asynchronous workflow is defined as a sequence of commands inside a </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">async{ ... }</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">, as in</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> asynctask </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">async</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> req </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> WebRequest.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Create(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">url</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let!</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> response </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> req.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">GetResponseAsync()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">use</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> stream </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> response.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">GetResponseStream()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">use</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> streamreader </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">new</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> System.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">IO</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">StreamReader(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">stream</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">return</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> streamreader.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">ReadToEnd()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">}</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">The </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let!</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> allows the rest of the async block to be defined as the delegate and passed as the <a href="http://en.wikipedia.org/wiki/Callback_%28computer_science%29" title="Callback (computer science)">callback function</a> of an asynchronous operation. This helps deal with <a href="http://en.wikipedia.org/wiki/Inversion_of_control" title="Inversion of control">inversion of control</a> issues.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-aw-5">[6]</a></sup> The async block is invoked using the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Async.Run</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> function. Multiple async blocks are executed in parallel using the </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Async.Parallel</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> function that takes a list of </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">async</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> objects (in the example, </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">asynctask</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;"> is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Async.Run</span><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">.<sup><a href="http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29#cite_note-aw-5">[6]</a></sup></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">F# comes with a <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio" title="Microsoft Visual Studio">Microsoft Visual Studio</a> language service that integrates it with the IDE. With the language service installed, Visual Studio can be used to create F# projects and the Visual Studio debugger used to debug F# code. In addition, it comes with a Visual Studio-hosted interactive console that executes F# code as it is being written.</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><b><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 18pt;">Examples</span></b></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">A few small samples follow:</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* This is commented *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Sample hello world program *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">printfn </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"Hello World!"</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative 32-bit integers, here shown in F#:</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> factorial n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">match</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">with</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">0</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | _ </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">*</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> factorial </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 10pt;"><span lang="EN" style="font-family: "Times New Roman","serif"; font-size: 12pt;">Recursive function examples:</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* print a list of numbers recursively *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList lst </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">match</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> lst </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">with</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | h :: t </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printf </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"%d\n"</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> h</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList t</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Same thing, using matching against list elements *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList2 l </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">match</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> l </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">with</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | h :: t </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printfn </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"%A"</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> h</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList2 t</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Using shorthand for match *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList3 </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">function</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | h :: t </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printfn </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"%A"</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> h</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printList3 t</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Or, using a higher-order function *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printlist4 lst </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">List</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">iter</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">printfn </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"%A")</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> lst</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Fibonacci Number formula *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fib n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">match</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">with</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">0</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> | _ </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fib </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">+</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fib </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">2)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* An alternative approach - a lazy recursive sequence of Fibonacci numbers *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">rec</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fibs </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">cache</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"><|</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">yield!</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[1;</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">for</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x, y </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">in</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">zip</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fibs </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"><|</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">skip</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fibs </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">+</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> y </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">}</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Another approach - a lazy infinite sequence of Fibonacci numbers *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fibSeq </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">unfold</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(fun</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">a,b</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> Some</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">a</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">+</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">b, </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">b, a</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">+</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">b</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)))</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">,</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Print even fibs *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> .. </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">10]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">List</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">map</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fib</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">List</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">filter</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(fun</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">%</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">2)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">0)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printlist</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Same thing, using sequence expressions *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">for</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> i </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">in</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">..</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">10</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">do</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> r </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> fib i</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">if</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> r </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">%</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">2</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">0</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">then</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">yield</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> r </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> printlist</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Sample Windows Forms Program *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* We need to open the System library for the STAThreadAttribute class *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">open</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> System</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* We need to open the Windows Forms library *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">open</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> System.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Windows</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Forms</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Create a window and set a few properties *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> form </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">new</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> Form</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Visible</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=true</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">, TopMost</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=true</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">, Text</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">="Welcome to F#")</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Create a label to show some text in the form *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> label </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> temp </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">new</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> Label</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">()</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">3</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">+</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(4</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">*</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">5)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Set the value of the Text*)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> temp.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Text</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"><-</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> sprintf </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"x = %d"</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Remember to return a value! *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> temp</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Add the label to the form *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">form.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Controls</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Add(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">label</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Finally, run the form *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">[<</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">STAThread</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">>]</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Application.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Run(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">form</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Async workflows sample (parallel CPU and I/O tasks) *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* A very naive prime number detector *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> isPrime </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n:</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">int)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> bound </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">int</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">System.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Math</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Sqrt(float</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">))</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{2</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> .. </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">bound}</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">exists</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(fun</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">-></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">%</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> x </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">0)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">not</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* We are using async workflows *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> primeAsync n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">async</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">return</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n, isPrime n</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">}</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Return primes between m and n using multiple threads *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">let</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> primes m n </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">=</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">{</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">m .. </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">n}</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Seq</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">map</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> primeAsync</span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> Async.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Parallel</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> Async.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">RunSynchronously</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Array</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">filter</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">snd</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Array</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">map</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">fst</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(* Run a test *)</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">primes </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1000000</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">1002000</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">|></span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">Array</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">.</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">iter</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"> </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">(</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">printfn </span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;">"%d")</span><span lang="EN" style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><br />
</div></div>Himanshu Manjarawalahttp://www.blogger.com/profile/13411988172800428582noreply@blogger.com0