<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wortzel&#039;s blog</title>
	<atom:link href="http://blogs.microsoft.co.il/aviwortzel/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.microsoft.co.il/aviwortzel</link>
	<description>.Net (2.0, 3.0, 3.5), C#, Asp.net, Com+, GIS(ESRI Software), Management, Analysis &#38; Design, Life, Trips, And more...</description>
	<lastBuildDate>Mon, 20 Jan 2014 08:21:14 +0000</lastBuildDate>
	<language>he-IL</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.2</generator>
	<item>
		<title>Using SSL in your application</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/07/11/using-ssl-in-your-application/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/07/11/using-ssl-in-your-application/#respond</comments>
		<pubDate>Sat, 11 Jul 2009 23:41:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Tips&Tricks]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=363338</guid>
		<description><![CDATA[One of the ways to add another security level to our application is to use SSL. SSL gives the ability to encrypt messages between two endpoints. If you will search the web you’ll probably notice that this ability is given to you [almost] out of the box. If you are already using IIS as your [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>One of the ways to add another security level to our application is to use SSL. SSL gives the ability to encrypt messages between two endpoints. If you will search the web you’ll probably notice that this ability is given to you [almost] out of the box. </p>
<p>If you are already using IIS as your web server and you want to add this security level, all you need to do is to check some check boxes in your web site configuration. In order to accomplish the configuration part you will be asked to provide a certificate. </p>
<p>The certificate (combination of identity and public key) is helping the client to authenticate the server. One of the common examples is your browser, when a user enters a secured web site the browser check its certificate using CA services, and verifies that the server is really who it claims to be.</p>
<p>There are many companies (<a href="http://www.verisign.com/" mce_href="http://www.verisign.com/">VeriSign</a> is one of the popular ones) which define the CA (certificate authority) and provide a certification services (it’s my recommendation for public internet applications). If you don’t want to pay money to a third party, you can create a free public certificate using <a href="http://www.openssl.org/" mce_href="http://www.openssl.org/">OpenSSL</a><a href="http://www.openssl.org/" mce_href="http://www.openssl.org/"> tools</a>, create self-singed certificate using <a href="http://msdn.microsoft.com/en-us/library/bfsktky3%28VS.80%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bfsktky3(VS.80).aspx">makecert utility</a> (you must remember to protect your private key), or even to build your own <a href="http://sial.org/howto/openssl/ca/" mce_href="http://sial.org/howto/openssl/ca/">CA server</a> (all this three are recommended for intranet applications). </p>
<p>Assume you choose one of the cheap ways, you still need to configure your software to validate the certificate. There are two ways to do it, the first one is using code, and the second by setting the server certificate in the client machine. In order to use a code you need to register to ServicePointManager.ServerCertificateValidationCallback event, this delegate passes the certificate details as a parameter and return whenever the certificate is valid or not. For example:</p>
<pre class="code"><span style="color: blue;">public void </span>GetData(<span style="color: blue;">string </span>url)<br>{<br>    <span style="color: blue;">using </span>(<span style="color: rgb(43, 145, 175);">WebClient </span>webClient = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">WebClient</span>())<br>    <span style="color: blue;">using </span>(<span style="color: rgb(43, 145, 175);">StreamReader </span>webClientStreamReader =<br>        <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">StreamReader</span>(webClient.OpenRead(url), <span style="color: rgb(43, 145, 175);">Encoding</span>.UTF8))<br>    {<br>        <span style="color: rgb(43, 145, 175);">ServicePointManager</span>.ServerCertificateValidationCallback += <br>            <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">RemoteCertificateValidationCallback</span>(customXertificateValidation);<br>        <span style="color: blue;">if </span>(webClientStreamReader != <span style="color: blue;">null</span>)<br>        {<br>            <span style="color: blue;">string </span>returnedStringFromMoma = webClientStreamReader.ReadToEnd();<br>        }<br>    }<br>}<br><span style="color: blue;">private static bool </span>customXertificateValidation(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">X509Certificate </span>cert, <span style="color: rgb(43, 145, 175);">X509Chain </span>chain, System.Net.Security.<span style="color: rgb(43, 145, 175);">SslPolicyErrors </span>error)<br>{<br>    <span style="color: blue;">return </span>(cert.Subject == <span style="color: rgb(163, 21, 21);">"CN=MyCustomCert, OU=Dev, …"</span>);<br>}</pre>
<p><a href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></a></p>
<p>In case you decided to set the server certificate in the client machine, you need to export the certificate from the server, and then to import the certificate into the client machine. You can do the import/export actions from the Windows Certificates manager.</p>
<p>I had another important issue I forget to mention, I know a lot of people which thought the using the request credential is secured, don’t let it to confuse you, the credential is just encoded it’s not encrypted.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/07/11/using-ssl-in-your-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pushing vs. Pulling</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/06/12/pushing-vs-pulling/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/06/12/pushing-vs-pulling/#respond</comments>
		<pubDate>Fri, 12 Jun 2009 16:18:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=336041</guid>
		<description><![CDATA[Few months ago I heard a lecture about &#34;Velocity&#34;, in the begging of the lecture the lecturer asked the audience the next question: &#34;Assume that you have a client which needs to communicate with a server, what is the best communication pattern to work with, pushing or pooling?&#34; 99% of the audience voted for the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Few months ago I heard a lecture about &quot;Velocity&quot;, in the begging of the lecture the lecturer asked the audience the next question: &quot;Assume that you have a client which needs to communicate with a server, what is the best communication pattern to work with, pushing or pooling?&quot; 99% of the audience voted for the pushing. It was a tricky question with a tricky answer – it depends.</p>
<p>I'll explain these two methods for all of those who aren't familiar with them.</p>
<p><b>Pushing</b> – in this concept, the responsibility to <i>transfer</i> the data is on the server. Each time the server has some new data to publish to the client the server will connect directly to the client and will send it all the relevant data <i>immediately</i>.</p>
<p><u>Pros:</u></p>
<ul>
<li>Using <i>minimum resources </i>to transfer a message (without extra traffic, network overload and etc)</li>
<li>The data will be <i>sent and</i> <i>received immediately</i> (without any delay).</li>
</ul>
<p><u>Cons:</u></p>
<ul>
<li><i>Scalability problem</i>: The server needs to be aware for all clients, and which data should be sent for each client.</li>
<li><i>Availability problem</i>: The client has high dependency on the server. In a case the server fell down, it'll be very difficult to determining it by the client. </li>
<li><i>Reliability and Durability problems</i>: on the other hand, in a case that the client fell down, the server needs to implement a retries mechanism and store the message in some persistence storage, in order to insure the message will be sent successfully and completely to the client.</li>
</ul>
<p><img src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image002_48F33BCF.jpg" style="display: inline;" title="clip_image002" alt="clip_image002" mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image002_48F33BCF.jpg" width="576" height="359"></p>
<p><b>Pulling</b> – in this method the responsibility for <i>retrieving</i> the data is on the client. The main challenge here is that the client doesn't know when he will need to connect the server in order to retrieve new data from it. In this case the client will need to use some monitoring techniques, every pre-defined interval. </p>
<p><u>Pros:</u></p>
<ul>
<li><i>Independency &#8211; </i>The client is fully autonomy. Each client only needs to be aware of its server.</li>
<li><i>Scalability</i>: this method is fully scalable.</li>
</ul>
<p><u>Cons:</u></p>
<ul>
<li><i>Very &quot;expensive&quot;</i> (using more resources, increase the network traffic, increase the processing time on server and client sides)</li>
<li><i>Delay time</i>: the message won't arrived immediately, it'll be sent with a delay.</li>
</ul>
<p><img src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image004_6D3FD264.jpg" style="display: inline;" title="clip_image004" alt="clip_image004" mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image004_6D3FD264.jpg" width="575" height="356"></p>
<p>One of the solutions for this problem is to use publisher-subscriber pattern (aka &quot;pub-sub&quot;). In this pattern we have a third component which call the &quot;pub-sub&quot;. Each client will subscribe to the pub-sub to get only messages which is relevant to him. The server will send all the messages to the pub-sub and will publish it to all subscribers' clients. In this pattern the server only need to send one message to one address without to be aware of all client locations.</p>
<p>&nbsp;<img src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image006_3FE5EEA7.jpg" style="display: inline;" title="clip_image006" alt="clip_image006" mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/clip_image006_3FE5EEA7.jpg" width="576" height="378"></p>
<p>If we'll return to the question for the beginning, next time you will need to decide which one of them to use, just think about the components architecture, analyze the communication requirements and then do the appropriate choose for your specific design.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/06/12/pushing-vs-pulling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wcf configuration &#8211; Throttling settings</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/05/12/wcf-configuration-throttling-settings/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/05/12/wcf-configuration-throttling-settings/#respond</comments>
		<pubDate>Tue, 12 May 2009 00:56:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=289503</guid>
		<description><![CDATA[In my pervious post I explained how to define the wcf service concurrency and instance context modes. In this post I will show you how to control these specific numbers (number of calls and number of instances). In wcf you can set these values using throttling settings. Like all other wcf configuration you can set [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx" mce_href="http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx">my pervious post</a> I explained how to define the wcf service concurrency and instance context modes. In this post I will show you how to control these specific numbers (number of calls and number of instances). In wcf you can set these values using throttling settings. Like all other wcf configuration you can set it by using a configuration file or to implement it directly in your source code. The throttling configuration contains three elements:   </p>
<p><b>1. maxConcurrentCalls [default: 16]: </b></p>
<ol> </ol>
<p>Refer to the service concurrency mode. In a case we define the service as a multiple concurrency mode, it'll define the number of threads we open in the service to handle requests. But, in a case we define the service as a single concurrency mode the service will ignore this value (because we have only single thread in the service anyway). </p>
<p><i>Increase this number if your service needs to process large number of requests at the same time</i>   </p>
<p><b>2. maxConcurrentInstances [default: Int32.Max]: </b></p>
<ol start="start"> </ol>
<p>Like the maxConcurrentCalls configuration it depends on other configurations. </p>
<p>If we define the service instance context mode to be Single the service will be ignore this value. In case the instance context mode set to per session, the real sessions number will be the minimum value between the maxConcurrencySessions and maxConcurrancyInstances. In case you define the service instance context mode as per call, it'll limit the number of active service instances. If the maximum number of instances exceeded all, the new requests will be queued.</p>
<p><i>Increase this number if you are using InstanceConcurrancyMode.PerCall or InstanceConcurrancyMode.PerSession and you have lightweight instances with heavy operations.</i>   </p>
<p><b>3. maxConcurrentSessions [default: 10]:</b> </p>
<ol start="start"> </ol>
<p>Refer only to a channel which supports sessions (like TCP) in all combinations of InstanceContextMode and InstanceConcurrencyMode configurations. Limits the number of active sessions in the service. Each time the client creates a new channel to the service it creates a new session in the service side (again, only for supported channels). Only when you close the channel gracefully (or after a timeout in the service side) the session will be released. </p>
<p><i>Increase this value if you have a lot of open channels to the service (and especially when some of them aren’t been closed gracefully…)</i></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/05/12/wcf-configuration-throttling-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wcf configuration &#8211; InstanceContextMode and ConcurrencyMode</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode/#respond</comments>
		<pubDate>Sat, 07 Mar 2009 20:52:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=243175</guid>
		<description><![CDATA[Two of the important configuration settings in WCF refer to InstanceContextMode and ConcurrencyMode. These two are very important to control the system resources in your live service. The first (InstanceContextMode) controls the number of instances for your service. In this case you have three options: Single &#8211; Creates only one service instance in the system. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Two of the important configuration settings in WCF refer to InstanceContextMode and ConcurrencyMode. These two are very important to control the system resources in your live service. The first (InstanceContextMode) controls the number of instances for your service. </p>
<p>In this case you have three options: </p>
<p><b>Single</b> &#8211; Creates only one service instance in the system.</p>
<p><b>PerSession</b> (default value) – Create a new service instance per session.</p>
<p><b>PerCall</b> – Creates a new service instance per call.</p>
<p>The second (ConcurrencyMode) controls how many threads can be run in the service at the same time. </p>
<p>For this configuration we have several options:</p>
<p><b>Single</b> – Single thread, only one thread in the entire service. You are in thread safe situation; don't worry about protecting your data, synchronization and etc. But in case your service is processing a request, all other request will wait (or failed on request timeout).</p>
<p><b>Reentrant</b> – This is a tricky one, its means that only one thread can be executed in the service but in case you call to another service the WCF infrastructure will release the lock from it (and then you should protect your service state like in the Multiple mode). You should use it to avoid cases of deadlock when two services with a single concurrency mode are calling each other.</p>
<p><b>Multiple</b> – Multi-thread, multiple threads can be run on the same time. In most cases you will reduce yours client's timeouts. But remember, this is not a magic solution, high number of thread will cost you in a context switch time (I'll cover the &quot;how to control the number of service active threads&quot; issue in one of my next posts).</p>
<p>Everything sounds simple by now, but the combination between the InstanceContextMode and the ConcurrencyMode is the fascinating part. To clarify this issue I summarized all possible combination in the next table:</p>
<p>&nbsp;</p>
<table style="border-color: Black; border-width: medium;" border="2" cellpadding="2" cellspacing="2">
<tbody style="border-color: Black; border-width: medium;">
<tr>
<td valign="top" width="79"></td>
<td valign="top" width="148">
<p>Concurrency</p>
<p>Mode.Single </p>
</td>
<td valign="top" width="148">
<p>Concurrency</p>
<p>Mode.Reentrant</p>
</td>
<td valign="top" width="176">
<p>Concurrency</p>
<p>Mode.Multiple</p>
</td>
</tr>
<tr>
<td valign="top" width="79">
<p>Instance</p>
<p>Context</p>
<p>Mode.</p>
<p>Single</p>
</td>
<td valign="top" width="148">
<p>Creates only one instance from the service (singleton) and only one thread can be executed in the service at the same time.</p>
<p><b>Classic case of single thread. </b></p>
</td>
<td valign="top" width="148">
<p>Creates only one instance from the service (singleton), multiple threads can be executed in callback scenarios.</p>
</td>
<td valign="top" width="176">
<p>Creates only one instance from the service (singleton), multiple threads can be executed in the service at the same time.</p>
<p><b>Almost without a bottleneck, must handle the synchronization issues.</b></p>
</td>
</tr>
<tr>
<td valign="top" width="79">
<p>Instance</p>
<p>Context</p>
<p>Mode.</p>
<p>PerSession</p>
</td>
<td valign="top" width="148">
<p>Each proxy gets is own instance, but because only one thread can be executed in the service at the same time all other requests will be queued. </p>
<p><b>bottleneck in case of many clients with a lot of calls.</b></p>
</td>
<td valign="top" width="148">
<p>Each proxy gets is own instance, multiple threads can be executed in the service at the same time in callback scenarios, you have to handle synchronization and data safe.</p>
</td>
<td valign="top" width="176">
<p>Each proxy gets is own instance, multiple threads can be executed in the service at the same time. You have to handle synchronization and data safe. </p>
<p><b>Overhead in serialized access to shared data.</b></p>
</td>
</tr>
<tr>
<td valign="top" width="79">
<p>Instance</p>
<p>Context</p>
<p>Mode.</p>
<p>PerCall</p>
</td>
<td colspan="3" valign="top" width="472">
<p>Each call gets a new and dedicated service instance (the service ignore the Concurrency mode in this case)</p>
<p><b>Overhead in instance creation for each call. No synchronization issues.</b></p>
</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wcf services – advanced NetTpcBinding configuration</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-services-advanced-nettpcbinding-configuration/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-services-advanced-nettpcbinding-configuration/#respond</comments>
		<pubDate>Sat, 07 Mar 2009 20:50:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=243174</guid>
		<description><![CDATA[In our application we chose WCF as a primary communication component. WCF provide several binding protocols and each one of them had its cons and pros. In our system we needed a high performance communication between several servers, it was the reason we used the WCF with NetTcpBinding. If you want to maximize your system [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In our application we chose WCF as a primary communication component. WCF provide several binding protocols and each one of them had its cons and pros. In our system we needed a high performance communication between several servers, it was the reason we used the WCF with NetTcpBinding. If you want to maximize your system utilization and having a better control on your services, you should make some fine tuning in the NetPctBinding. The default configurations for this binding are good for some classic scenarios, but like every other software if you need something more, you should override this default settings. It sounds like a simple task, just copy an existing binding configuration from the net and put it in your application, but actually it is very hard. The complication is caused by some serious reasons: the errors aren’t clear, the documentation is awful and some of the parameters are affected by each other and are also not documented. In my next posts I’ll write about some of this advanced configurations:</p>
<p>&#8211; <a href="/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx" mce_href="/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx">InstanceContextMode and ConcurrencyMode </a></p>
<p>&#8211; <a href="/blogs/aviwortzel/archive/2009/05/12/wcf-configuration-throttling-settings.aspx" mce_href="/blogs/aviwortzel/archive/2009/05/12/wcf-configuration-throttling-settings.aspx">Wcf configuration &#8211; Throttling settings</a><a href="http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx" mce_href="/blogs/aviwortzel/archive/2009/03/07/wcf-configuration-instancecontextmode-and-concurrencymode.aspx"> </a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/03/07/wcf-services-advanced-nettpcbinding-configuration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a high performance and scalability system</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2009/03/06/building-a-high-performance-and-scalability-system/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2009/03/06/building-a-high-performance-and-scalability-system/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 14:23:40 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DEV]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=242129</guid>
		<description><![CDATA[During the last few months I had some thoughts about developing a high performance and scalability systems. The idea of designing a complete system which can support almost unlimited traffic is very exciting and challenging. The main goal is to have maximum utilization of the current resources. Furthermore, is having the ability to add another [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>During the last few months I had some thoughts about developing a high performance and scalability systems. The idea of designing a complete system which can support almost unlimited traffic is very exciting and challenging. The main goal is to have maximum utilization of the current resources. Furthermore, is having the ability to add another box and to [almost] double the server performance. Each component should have the ability to scale and to work with several copies in different machines at the same time. </p>
<p>In my next posts I decided to focus on some of interesting issues from the architecture design world, like:</p>
<p>&#8211; Communication.</p>
<p>&#8211; Configuration.</p>
<p>&#8211; Performance</p>
<p>&#8211; State sharing</p>
<p>and more…</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2009/03/06/building-a-high-performance-and-scalability-system/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Single service handler with multiple contracts</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2008/12/09/single-service-handler-with-multiple-contracts/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2008/12/09/single-service-handler-with-multiple-contracts/#respond</comments>
		<pubDate>Tue, 09 Dec 2008 17:40:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=187868</guid>
		<description><![CDATA[During the previous week Rami (my teammate) and I had an interesting problem. We had to develop two WCF services with some shared collection. One of the solutions we had was to store this collection as a static collection, and then each one of them will be able to get it. But we found a [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>During the previous week Rami (my teammate) and I had an interesting problem. We had to develop two WCF services with some shared collection. One of the solutions we had was to store this collection as a static collection, and then each one of them will be able to get it. But we found a better solution. We created only one service handler class which implements the both contract interface at the same time. At the beginning we weren’t sure that this idea can be implemented, surprisingly it worked. As you probably know you can define several endpoints to the same service, each one of them with a different binding like this one:</p>
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">services</span><span style="color: blue;">&gt;<br>  &lt;</span><span style="color: rgb(163, 21, 21);">service<br>      </span><span style="color: red;">behaviorConfiguration</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.Service1Behavior</span>"<br>      <span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.ServiceHandler</span>"<span style="color: blue;">&gt;<br>    &lt;</span><span style="color: rgb(163, 21, 21);">endpoint<br>      </span><span style="color: red;">address</span><span style="color: blue;">=</span>"<span style="color: blue;">http://localhost:2010/ServiceHandler.svc</span>"<br>      <span style="color: red;">binding</span><span style="color: blue;">=</span>"<span style="color: blue;">wsHttpBinding</span>"<br>      <span style="color: red;">contract</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.ICustomerService</span>"<span style="color: blue;">&gt;<br>    &lt;/</span><span style="color: rgb(163, 21, 21);">endpoint</span><span style="color: blue;">&gt;<br>    &lt;</span><span style="color: rgb(163, 21, 21);">endpoint<br>      </span><span style="color: red;">address</span><span style="color: blue;">=</span>"<span style="color: blue;">net.tcp://localhost:2020/ServiceHandler.svc</span>"<br>      <span style="color: red;">binding</span><span style="color: blue;">=</span>"<span style="color: blue;">netTcpBinding</span>"<br>      <span style="color: red;">contract</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.ICustomerService</span>"<span style="color: blue;">&gt;<br>    &lt;/</span><span style="color: rgb(163, 21, 21);">endpoint</span><span style="color: blue;">&gt;<br>  &lt;/</span><span style="color: rgb(163, 21, 21);">service</span><span style="color: blue;">&gt;<br>&lt;/</span><span style="color: rgb(163, 21, 21);">services</span><span style="color: blue;">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></a></p>
<p>But our question was: Can we associate more than one contract to a single service?</p>
<p>So we created something like this:</p>
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">services</span><span style="color: blue;">&gt;<br>  &lt;</span><span style="color: rgb(163, 21, 21);">service<br>      </span><span style="color: red;">behaviorConfiguration</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.Service1Behavior</span>"<br>      <span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.ServiceHandler</span>"<span style="color: blue;">&gt;<br>    &lt;</span><span style="color: rgb(163, 21, 21);">endpoint<br>        </span><span style="color: red;">address</span><span style="color: blue;">=</span>"<span style="color: blue;">http://localhost:1056/ServiceHandler.svc</span>"<br>        <span style="color: red;">binding</span><span style="color: blue;">=</span>"<span style="color: blue;">wsHttpBinding</span>"<br>        <span style="color: red;">contract</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.IOrderService</span>"<span style="color: blue;">&gt;<br>    &lt;/</span><span style="color: rgb(163, 21, 21);">endpoint</span><span style="color: blue;">&gt;<br>    &lt;</span><span style="color: rgb(163, 21, 21);">endpoint<br>        </span><span style="color: red;">address</span><span style="color: blue;">=</span>"<span style="color: blue;">http://localhost:1056/ServiceHandler.svc</span>"<br>        <span style="color: red;">binding</span><span style="color: blue;">=</span>"<span style="color: blue;">wsHttpBinding</span>"<br>        <span style="color: red;">contract</span><span style="color: blue;">=</span>"<span style="color: blue;">WcfService.ICustomerService</span>"<span style="color: blue;">&gt;<br>    &lt;/</span><span style="color: rgb(163, 21, 21);">endpoint</span><span style="color: blue;">&gt;<br>  &lt;/</span><span style="color: rgb(163, 21, 21);">service</span><span style="color: blue;">&gt;<br>&lt;/</span><span style="color: rgb(163, 21, 21);">services</span><span style="color: blue;">&gt;<br></span></pre>
<p><a href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></a></p>
<p>After few tests it worked. Now our service handler class looks like this:</p>
<pre class="code">[<span style="color: rgb(43, 145, 175);">ServiceBehavior</span>(InstanceContextMode = <span style="color: rgb(43, 145, 175);">InstanceContextMode</span>.Single)]<br><span style="color: blue;">public class </span><span style="color: rgb(43, 145, 175);">ServiceHandler </span>: IOrderService, ICustomerService<br>{<br>    …<br>}</pre>
<p><a href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></a></p>
<p>We added the InstanceServiceMode service behavior attribute with the “Single” value to define only one service handler class instance for the entire service requests. Now we have one service handler class which implements more than one service contract. </p>
<p>According to this idea you can work with inheritance interfaces too, for example assume you have this interface:</p>
<pre class="code">[<span style="color: rgb(43, 145, 175);">ServiceContract</span>]<br><span style="color: blue;">public interface </span><span style="color: rgb(43, 145, 175);">ICRM </span>: ICustomerService, IOrderService<br>{<br>}</pre>
<p><a href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></a></p>
<p>You can easily define only one service and get two in the same price.</p>
<p>I <a href="/blogs/aviwortzel/attachment/187868.ashx" mce_href="/blogs/aviwortzel/attachment/187868.ashx">attached</a> a code sample of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2008/12/09/single-service-handler-with-multiple-contracts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My first visit in Asia</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2008/09/28/my-first-visit-in-asia/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2008/09/28/my-first-visit-in-asia/#respond</comments>
		<pubDate>Sun, 28 Sep 2008 17:27:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[OFFTOPIC]]></category>
		<category><![CDATA[Trips]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=147422</guid>
		<description><![CDATA[In the beginning of this month I was in a business trip to Asia to train our technical team members on a new software component we developed in our R&#38;D team. The main goal of this component is to monitor net events and map it to actions. The business user has the ability to creates [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>In the beginning of this month I was in a business trip to Asia to train our technical team members on a new software component we developed in our R&amp;D team. The main goal of this component is to monitor net events and map it to actions. The business user has the ability to creates flows in a web interface and to defines the relevant actions for each event. In our solution we used WCF to communicate between our services, in addition, we used FTP to transfer batch files to our backend server. </FONT></P><br />
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>You can look on some of my pictures <A class="" href="http://wortzel.spaces.live.com/photos/cns!6F81ECBECE9391D6!1001/?startingImageIndex=34&amp;commentsExpand=0&amp;addCommentExpand=0&amp;addCommentFocus=0&amp;pauseSlideshow=0" mce_href="http://wortzel.spaces.live.com/photos/cns!6F81ECBECE9391D6!1001/?startingImageIndex=34&amp;commentsExpand=0&amp;addCommentExpand=0&amp;addCommentFocus=0&amp;pauseSlideshow=0">here</A>.</FONT></P></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2008/09/28/my-first-visit-in-asia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bind an Enum</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2008/08/25/bind-an-enum/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2008/08/25/bind-an-enum/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 16:19:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Tips&Tricks]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=132534</guid>
		<description><![CDATA[It's very useful to take an enum and bind it into bind-able controls (like drop down list, check box, grid view and etc). For example, if we have an enum which represents bug statuses and you want to bind it into a drop down list. public enum Bug { Opened = 1, Closed = 2, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><P>It's very useful to take an enum and bind it into bind-able controls (like drop down list, check box, grid view and etc). For example, if we have an enum which represents bug statuses and you want to bind it into a drop down list. <PRE class=code><SPAN style="COLOR: blue">public enum </SPAN><SPAN style="COLOR: #2b91af">Bug<br />
</SPAN>{<br />
    Opened = 1,<br />
    Closed = 2,<br />
    Rejected = 3,<br />
    Resolved = 4,<br />
    InProgress = 5<br />
}</PRE><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A><br />
<P>In addition, usually in a UI we want to separate between the enum words (for example to change the &quot;InProgress&quot; to &quot;In Progress&quot;).<br />
<P>I search over the net and I found some solution but in all of them I needed to extend my current enum (like using attribute in <A href="http://www.codeproject.com/KB/cs/enumdatabinding.aspx" mce_href="http://www.codeproject.com/KB/cs/enumdatabinding.aspx">this solution</A>), so I decided to implement one of myself:<PRE class=code><SPAN style="COLOR: blue">public static class </SPAN><SPAN style="COLOR: #2b91af">EnumHelper<br />
</SPAN>{<br />
    <SPAN style="COLOR: blue">public static </SPAN><SPAN style="COLOR: #2b91af">IDictionary</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: blue">int</SPAN>&gt; GetList&lt;TEnum&gt;()<br />
    {<br />
        <SPAN style="COLOR: blue">return </SPAN>GetList&lt;TEnum&gt;(<SPAN style="COLOR: blue">false</SPAN>);<br />
    }</p>
<p>    <SPAN style="COLOR: blue">public static </SPAN><SPAN style="COLOR: #2b91af">IDictionary</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: blue">int</SPAN>&gt; GetList&lt;TEnum&gt;(<SPAN style="COLOR: blue">bool </SPAN>splitString)<br />
    {<br />
        <SPAN style="COLOR: #2b91af">Type </SPAN>type = <SPAN style="COLOR: blue">typeof</SPAN>(TEnum);<br />
        <SPAN style="COLOR: #2b91af">Dictionary</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: blue">int</SPAN>&gt; list = <SPAN style="COLOR: blue">new </SPAN><SPAN style="COLOR: #2b91af">Dictionary</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: blue">int</SPAN>&gt;();<br />
        <SPAN style="COLOR: #2b91af">Array </SPAN>enumValues = <SPAN style="COLOR: #2b91af">Enum</SPAN>.GetValues(type);<br />
        <SPAN style="COLOR: blue">foreach </SPAN>(<SPAN style="COLOR: #2b91af">Enum </SPAN>value <SPAN style="COLOR: blue">in </SPAN>enumValues)<br />
        {<br />
            <SPAN style="COLOR: blue">int </SPAN>enumValue = <SPAN style="COLOR: #2b91af">Convert</SPAN>.ToInt32(((TEnum)<SPAN style="COLOR: #2b91af">Enum</SPAN>.Parse(type, value.ToString())));<br />
            <SPAN style="COLOR: blue">string </SPAN>sb = (splitString) ? SplitString(value) : value.ToString();<br />
            list.Add(sb, enumValue);<br />
        }<br />
        <SPAN style="COLOR: blue">return </SPAN>list;<br />
    }</p>
<p>    <SPAN style="COLOR: blue">private static string </SPAN>SplitString(<SPAN style="COLOR: #2b91af">Enum </SPAN>value)<br />
    {<br />
        <SPAN style="COLOR: #2b91af">StringBuilder </SPAN>sb = <SPAN style="COLOR: blue">new </SPAN><SPAN style="COLOR: #2b91af">StringBuilder</SPAN>();<br />
        <SPAN style="COLOR: blue">char</SPAN>[] chars = value.ToString().ToCharArray();</p>
<p>        sb.Append(chars[0]);<br />
        <SPAN style="COLOR: blue">for </SPAN>(<SPAN style="COLOR: blue">int </SPAN>i = 1; i &lt; chars.Length &#8211; 1; i++)<br />
        {<br />
            <SPAN style="COLOR: blue">if </SPAN>((chars[i].ToString() == chars[i].ToString().ToUpper())<br />
                &amp;&amp; (chars[i &#8211; 1].ToString() != chars[i &#8211; 1].ToString().ToUpper()))<br />
            {<br />
                sb.Append(<SPAN style="COLOR: #a31515">&quot; &quot;</SPAN>);<br />
            }<br />
            sb.Append(chars[i]);<br />
        }<br />
        sb.Append(chars[chars.Length &#8211; 1]);<br />
        <SPAN style="COLOR: blue">return </SPAN>sb.ToString();<br />
    }<br />
}</PRE><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A><br />
<P>How do we use this code?<br />
<P>In case we just want to bind it without separate between the enum words, it will be like this:<PRE class=code><SPAN style="COLOR: blue">var </SPAN>bugs = <SPAN style="COLOR: #2b91af">EnumHelper</SPAN>.GetList&lt;<SPAN style="COLOR: #2b91af">Bug</SPAN>&gt;();</PRE><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A><br />
<P>And in case we want to separate between the words will have something like that:<PRE class=code><SPAN style="COLOR: blue">var </SPAN>bugs = <SPAN style="COLOR: #2b91af">EnumHelper</SPAN>.GetList&lt;<SPAN style="COLOR: #2b91af">Bug</SPAN>&gt;(<SPAN style="COLOR: blue">true</SPAN>);</PRE><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A><br />
<P><B>Challenge:</B><br />
<P>How do you synchronies between enum (which execute in web/service context) and a table/database context?<br />
<P>For example you have this bug statuses enum and in addition you have a bug table with numeric status column. The status column values are the same enum numeric values. After deployment, in order to understand this column value you must look at the enum that represent it. But in order to do so, you must open your code…</P><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2008/08/25/bind-an-enum/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to define state machine in Windows Workflow Foundation (WF)</title>
		<link>http://blogs.microsoft.co.il/aviwortzel/2008/07/15/how-to-define-state-machine-in-windows-workflow-foundation-wf/</link>
		<comments>http://blogs.microsoft.co.il/aviwortzel/2008/07/15/how-to-define-state-machine-in-windows-workflow-foundation-wf/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 00:49:00 +0000</pubDate>
		<dc:creator><![CDATA[avi.wortzel]]></dc:creator>
				<category><![CDATA[.Net Framework 3.0]]></category>
		<category><![CDATA[WF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=117449</guid>
		<description><![CDATA[Windows Workflow Foundation is a powerful framework to develop a workflow component in your application. This framework has two workflow types: Sequential workflow (pre-defined flow) and State Machine (event driven flow). In this post I’ll explain and show you how to define a workflow from the later kind. Each state machine consists of two major [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><DIV class=csharpcode-wrapper><br />
<P>Windows Workflow Foundation is a powerful framework to develop a workflow component in your application. This framework has two workflow types: Sequential workflow (pre-defined flow) and State Machine (event driven flow). In this post I’ll explain and show you how to define a workflow from the later kind.<br />
<P>Each state machine consists of two major components: state (unique configuration) and Transitions (between the states, based on events or input). Each state machine should have initialize state and completion state.<br />
<P>For example let see a state machine of bug(You can download the entire example from <A class="" href="http://blogs.microsoft.co.il/blogs/aviwortzel/attachment/117449.ashx" mce_href="/blogs/aviwortzel/attachment/117449.ashx">here</A>):<br />
<P><A href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image002_2.gif" mce_href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image002_2.gif"><IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=100 alt=clip_image002 src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image002_thumb.gif" width=326 border=0 mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image002_thumb.gif"></A><br />
<P>After we defined the state machine diagram we can implement it easily.<br />
<P>The first thing we should do is to open a state machine workflow console application:<br />
<P><A href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image004_2.jpg" mce_href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image004_2.jpg"><IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=180 alt=clip_image004 src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image004_thumb.jpg" width=240 border=0 mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image004_thumb.jpg"></A><br />
<P>In order to separate between the workflow design and the implementation we’ll create an interface of the state machine events. In the next step we’ll associate between the interface events to the workflow events. The ExternalDataExchange is necessary for exposing this interface to the workflow state machine.<br />
<DIV class=csharpcode-wrapper><br />
<DIV class=csharpcode><PRE class=csharpcode>[ExternalDataExchange]<br />
<SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>interface</SPAN> IBugService<br />
{<br />
    <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugOpened;<br />
    <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugRejected;<br />
    <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugSolved;<br />
    <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugClosed;<br />
}</PRE><PRE class=alteven>&nbsp;</PRE></DIV></DIV><br />
<P>As you probably notice I used a custom EventArgs to pass some relevant arguments as parameters to the event handler. This custom EventArgs should inherit from the ExternalDataEventAargs.<PRE class=csharpcode>[Serializable]<br />
Public <SPAN class=kwrd>class</SPAN> BugEventArgs : ExternalDataEventArgs<br />
{<br />
    <SPAN class=kwrd>public</SPAN> BugEventArgs(Guid instanceId, Bug bug)<br />
      :<SPAN class=kwrd>base</SPAN>(instanceId)<br />
    {<br />
        Bug = bug;<br />
        WaitForIdle = <SPAN class=kwrd>true</SPAN>;<br />
    }</p>
<p>    <SPAN class=kwrd>public</SPAN> Bug Bug { get; set; }<br />
}<br />
</PRE><br />
<P>A very important issue is the WaitForIdle property. You must set the value of this property to “True” (believe me it took me some time to figure it out), it tells to the workflow runtime to raise the event only when the workflow became idle. In case that this property set to “False” and you try to have quick transition between states, you probably will get this unclearly error message:<br />
<P><I>“Event &quot;BugRejected&quot; on interface type &quot;Wortzel.WF.SimpleStateMachine.IBugService&quot; for instance id &quot;50943c87-348d-4a88-9ada-d440c4989eca&quot; cannot be delivered.” (With inner exception : &quot;Queue 'Message Properties Interface Type:Wortzel.WF.SimpleStateMachine.IBugService Method Name:BugRejected CorrelationValues: is not enabled.&quot;)</I><br />
<P>The service class which going to be the outer API for our state machine, will implement the IServiceBug interface. In addition, this class will contain the entire raise event methods:<PRE class=csharpcode>[Serializable]<br />
<SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>class</SPAN> BugService:IBugService<br />
{<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> RaiseBugOpenedEvent(Guid instanceId, Bug bug)<br />
    {<br />
        <SPAN class=kwrd>if</SPAN> (BugOpened != <SPAN class=kwrd>null</SPAN>)<br />
        {<br />
            BugOpened(<SPAN class=kwrd>null</SPAN>, <SPAN class=kwrd>new</SPAN> BugEventArgs(instanceId, bug));<br />
        }<br />
    }<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> RaiseBugRejectedEvent(Guid instanceId, Bug bug)<br />
    {<br />
        <SPAN class=kwrd>if</SPAN> (BugRejected != <SPAN class=kwrd>null</SPAN>)<br />
        {<br />
            BugRejected(<SPAN class=kwrd>null</SPAN>, <SPAN class=kwrd>new</SPAN> BugEventArgs(instanceId, bug));<br />
        }<br />
    }<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> RaiseBugSolvedEvent(Guid instanceId, Bug bug)<br />
    {<br />
        <SPAN class=kwrd>if</SPAN> (BugSolved != <SPAN class=kwrd>null</SPAN>)<br />
        {<br />
            BugSolved(<SPAN class=kwrd>null</SPAN>, <SPAN class=kwrd>new</SPAN> BugEventArgs(instanceId, bug));<br />
        }<br />
    }<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> RaiseBugClosedEvent(Guid instanceId, Bug bug)<br />
    {<br />
        <SPAN class=kwrd>if</SPAN> (BugClosed != <SPAN class=kwrd>null</SPAN>)<br />
        {<br />
            BugClosed(<SPAN class=kwrd>null</SPAN>, <SPAN class=kwrd>new</SPAN> BugEventArgs(instanceId, bug));<br />
        }<br />
    }</p>
<p>    <SPAN class=preproc>#region</SPAN> IBugService Members<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugOpened;<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugRejected;<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugSolved;<br />
    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>event</SPAN> EventHandler&lt;BugEventArgs&gt; BugClosed;<br />
    <SPAN class=preproc>#endregion</SPAN><br />
}</PRE><br />
<P>And now we’ll create the workflow itself. We’ll create an event-driven state machine, each event represents a single transition in our state machine. One of the drawbacks in this model is in case we want to share a single event across multiple states. For example, if we have 2 states (BugSlovedState and BugRejectedState state) which both of them need to define a transition to the BugClosedState state we must define two different events (OnBugClosed1 and OnBugClosed2). I understand the needs to define two events but it’s still annoying…<br />
<P><A href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image006_2.jpg" mce_href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image006_2.jpg"><IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=244 alt=clip_image006 src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image006_thumb.jpg" width=209 border=0 mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image006_thumb.jpg"></A><br />
<P>Each state has two activities: HandleExternalEventActivity and SetStateActivity. The former associates the state machine and the event (in the IBugService interface) and the later set the next state after this event fired.<br />
<P><A href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image008_2.jpg" mce_href="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image008_2.jpg"><IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=244 alt=clip_image008 src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image008_thumb.jpg" width=193 border=0 mce_src="http://blogs.microsoft.co.il/blogs/aviwortzel/WindowsLiveWriter/HowtodefinestatemachineinWindowsWorkflow_88E/clip_image008_thumb.jpg"></A><br />
<P>The last thing we have to do is to activate this state machine. In order to do it we need to write some code which creates the workflow runtime, associates the BugService with our workflow and then creates a workflow instance and starts it.<PRE class=csharpcode><SPAN class=kwrd>static</SPAN> <SPAN class=kwrd>void</SPAN> Main(<SPAN class=kwrd>string</SPAN>[] args)<br />
{<br />
    <SPAN class=kwrd>using</SPAN>(WorkflowRuntime workflowRuntime = <SPAN class=kwrd>new</SPAN> WorkflowRuntime())<br />
    {<br />
        ExternalDataExchangeService dataExchangeService = <SPAN class=kwrd>new</SPAN><br />
        ExternalDataExchangeService();<br />
        workflowRuntime.AddService(dataExchangeService);<br />
        BugService bugService = <SPAN class=kwrd>new</SPAN> BugService();<br />
        dataExchangeService.AddService(bugService);<br />
        WorkflowInstance instance =<br />
            workflowRuntime.CreateWorkflow(<SPAN class=kwrd>typeof</SPAN>(BugWorkflow));<br />
        instance.Start();<br />
        Bug bug = <SPAN class=kwrd>new</SPAN> Bug() { BugId = 1, AssignTo = <SPAN class=str>&quot;Someone&quot;</SPAN> };<br />
        bugService.RaiseBugOpenedEvent(instance.InstanceId, bug);<br />
        bugService.RaiseBugRejectedEvent(instance.InstanceId, bug);<br />
        bugService.RaiseBugClosedEvent(instance.InstanceId, bug);<br />
    }<br />
}</PRE><br />
<P>And that it all, we created a state machine workflow. If you want, you can add a tracking service (SqlTrackingQuery) which can help you to determine the entire flow for a specific state machine instance.<br />
<P>When I used it I had a wired behavior: when I printed the current state to the console after I changed several states one after another, I got an inconsistent data (the state name doesn’t always update). My guess that is caused due to racing problem in the state machine engine (only when I gave it much time between the states transition it shows me the right data). Is it bug or feature?</P></DIV><br />
<DIV class=csharpcode-wrapper><br />
<DIV class=csharpcode><A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"></A>&nbsp;</DIV></DIV></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/aviwortzel/2008/07/15/how-to-define-state-machine-in-windows-workflow-foundation-wf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
