tag:blogger.com,1999:blog-27210944597509547632024-03-14T04:59:37.595+01:00Damjan's playgroundDamjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-2721094459750954763.post-17787687455858714632014-03-26T22:44:00.001+01:002015-04-24T14:46:02.608+02:00WCF impersonation with reusable proxy<div dir="ltr" id="docs-internal-guid-1299bde5-0031-d76c-3204-1607c9099c09" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Windows Communication Foundation (WCF) is a powerful framework for building distributed service oriented applications. Services offer resources in the specific domain which are accessed by the clients. In general clients call a service to have the service perform some action on the client’s behalf - this is an </span><b><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">impersonation</span></b><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. It allows the service to act as the client while performing the action. To be more specific - the service runs in the client security context (e.g. service thread is executed in a security context that is different from the process that owns the thread). As shown in the picture below the impersonation is used to deal with credentials on the same machine </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Computer A</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">:</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://1.bp.blogspot.com/-CgaOJjO8QOw/UzVrNaExa2I/AAAAAAAAD1k/ALJCYDeY8QA/s1600/WCF_Impersonation.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://1.bp.blogspot.com/-CgaOJjO8QOw/UzVrNaExa2I/AAAAAAAAD1k/ALJCYDeY8QA/s1600/WCF_Impersonation.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Impersonation</td></tr>
</tbody></table>
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Delegation</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> is a special way of flowing impersonation using client security context to the back-end service (e.g. impersonation chain along services). Impersonation is used to access resources on the same machine as the service, while delegation is used to access resources that are remote to the initial service. The picture below shows the delegation is used to delegate credentials between different machines </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Computer A</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> and </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Computer B</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">:</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-sLyb6rmUX6E/UzVrVw5Db5I/AAAAAAAAD1s/NK-myC8wl4I/s1600/WCF_Delegation.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://2.bp.blogspot.com/-sLyb6rmUX6E/UzVrVw5Db5I/AAAAAAAAD1s/NK-myC8wl4I/s1600/WCF_Delegation.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Delegation</td></tr>
</tbody></table>
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">WCF offers three impersonation options for WCF service operations via enumeration type </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">System.ServiceModel.ImpersonationOption</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">:</span></div>
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<ol id="docs-internal-guid-1299bde5-0048-b908-4d03-cd6be9c814c6" style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">NotAllowed</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: impersonation is not performed on particular service operation.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br class="kix-line-break" /></span></div>
</li>
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Allowed</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: impersonation is performed when caller Windows identity token (credentials) is available and the service is configured to impersonate on all operations using </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ImpersonateCallerForAllOperations</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> in WCF configuration section </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ServiceAuthorizationBehavior</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<br /></div>
</li>
</ol>
<ol start="3" style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Required</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: impersonation is required and the caller Windows identity token has to be available unless an exception will occur.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<br /></div>
</li>
</ol>
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">By default, WCF applies </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">NotAllowed</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> impersonation option for all operations. In order to enable it, there are two possibilities:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<ol style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: upper-alpha; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Setting </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">OperationBehavior</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> attribute to be set to </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Required</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> at specific operation. This attribute cannot be applied on contract but only at the actual operation’s implementation. </span><span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">[OperationBehavior(Impersonation = ImpersonationOption.Required)]<br class="kix-line-break" />public string Ping() {</span></div>
</li>
</ol>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ....<br /> }</span><br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br /></span></div>
<ol start="2" style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: upper-alpha; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Setting </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ImpersonateCallerForAllOperations</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> property to </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">true</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> in the Service Authorization Behavior in configuration section. This implies impersonation at service level for all its operations.</span></div>
</li>
</ol>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <serviceBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <behavior name="ServiceBehavior"></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <serviceAuthorization impersonateCallerForAllOperations="true" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </behavior></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </serviceBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">In the next example we see how to obtain the caller (client) identity token inside the service operation. To be more generic we use abstract class for getting client token information using </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">System.ServiceModel.ServiceSecurityContext</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> class:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">namespace EE.Framework.Services {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public abstract class BaseService {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> protected string CallerUserName {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> get { return this.CallerIdentity.Name ?? null; }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> protected WindowsIdentity CallerIdentity {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> get { return ServiceSecurityContext.Current.WindowsIdentity; }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">In the service class we can get information about the client credentials. The service contract </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">IPingContract</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> is the following:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">namespace EE.Framework.ServiceContracts {</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> [ServiceContract(Namespace = "http://myuri/services/ping")]</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public interface IPingContract {</span></div>
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> [OperationContract()]</span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> string Ping();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The service class </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">PingService</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> is defined as:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">namespace EE.Framework.Services {</span></div>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public class PingService: BaseService, IPingContract {</span></div>
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br /> [OperationBehavior(Impersonation = ImpersonationOption.Required)]</span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public string Ping() {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return string.Format("Reply from {0} @ {1} [caller: {2}].", </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-align: left; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Environment.MachineName, DateTime.Now, base.CallerUserName);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The service will be hosted in </span><a href="http://msdn.microsoft.com/en-us/library/ms733766%28v=vs.110%29.aspx" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;">IIS</span></a><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> so the web.config file needs to be modified. As we use </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">OperationBehavior</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> attribute the </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ImpersonateCallerForAllOperations</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> property is not needed (it is commented):</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><system.serviceModel></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <services></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <!-- PingService: TCP endpoint --></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <service behaviorConfiguration="PingServiceBehavior" </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> name="EE.Framework.Services.PingService"></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <endpoint address="tcp" binding="netTcpBinding" </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> contract="EE.Framework.ServiceContracts.IPingContract"</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> name="NetTcpBinding_IPingContract" </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> bindingName="NetTcpBinding_IPingContract" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </service></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </services></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <!-- BINDINGS --></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <bindings></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <netTcpBinding></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <binding name="NetTcpBinding_IPingContract"></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <security mode="None" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </binding></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </netTcpBinding></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </bindings></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <!-- BEHAVIORS --></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <serviceBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <behavior name="PingServiceBehavior"></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <serviceMetadata httpGetEnabled="true" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <serviceDebug includeExceptionDetailInFaults="true" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <!-- <serviceAuthorization impersonateCallerForAllOperations="true" /> --></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </behavior></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </serviceBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></system.serviceModel></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br />The service file </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">PingService.svc</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> contains the following markup code:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><%@ ServiceHost Service="EE.Framework.Services.PingService, EE.Framework.Services" %></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The impersonation of the client Windows identity token is driven by enumeration type </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">System.Security.Principal.TokenImpersonationLevel</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<ol style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">None</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: the impersonation on WCF service is disabled.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br /></span></div>
</li>
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Anonymous</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: the WCF service authenticates client as anonymous user but cannot retrieve information about identification or impersonation.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br /></span></div>
</li>
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Identification</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: the WCF service can authenticate clients but cannot make impersonation. This is default value of the WCF client.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<br /></div>
</li>
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Impersonation</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: the WCF service can authenticate, retrieve information for identification and impersonate client’s security context on its local system. The WCF service cannot impersonate client on remote system.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<br /></div>
</li>
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; list-style-type: decimal; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Delegation</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">: the WCF service can authenticate, retrieve information for identification and perform impersonation on remote system.</span></div>
</li>
</ol>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">If the WCF client is the web application, we need to enable ASP.NET impersonation (which is disabled by default) and enable Windows authentication (anonymous has to be disabled):</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><configuration></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <system.web></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <identity impersonate="true" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <authentication mode="Windows" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </system.web></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></configuration></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">We need to set specific impersonation level as well. This can be done in a two ways:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<ol style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: upper-alpha; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Configuration file</span></div>
</li>
</ol>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <system.serviceModel></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <endpointBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <behavior></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <clientCredentials></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> <windows allowedImpersonationLevel="Impersonation" /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </clientCredentials></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </behavior></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </endpointBehaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </behaviors></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></system.serviceModel></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<ol start="2" style="margin-bottom: 0pt; margin-top: 0pt;">
<li dir="ltr" style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: upper-alpha; text-decoration: none; vertical-align: baseline;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Setting impersonation at proxy</span></div>
</li>
</ol>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Security.Principal;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TokenImpersonationLevel til = TokenImpersonationLevel.Impersonation;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">PingServiceProxy proxy = new PingServiceProxy();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">proxy.ClientCredentials.Windows.AllowedImpersonationLevel = til;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">When calling service from the client we need to impersonate block of code using </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">WindowsIdentity</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> class:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Security.Principal;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TokenImpersonationLevel til = TokenImpersonationLevel.Impersonation;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using (((WindowsIdentity)User.Identity).Impersonate()) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> PingServiceProxy proxy = new PingServiceProxy();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> proxy.ClientCredentials.Windows.AllowedImpersonationLevel = til;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> string result = proxy.Ping();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">After that we get the following string result with caller name:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Reply from DEV-PC @ 20.12.2013 13:39:41 [caller: DEV-PC\DAMJANK].</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The next step is to use reusable client proxy that can be applied for any WCF service using its contract only. Brandon Zeider wrote a good </span><a href="http://brandonzeider.me/2011/microsoft-net/building-a-reusable-service-client/" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;">article</span></a><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> how to create generic service client using WCF </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ChannelFactory</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> with Castle’s </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">DynamicProxy</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Below is an example of service call with generic proxy (</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">endpoint</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> variable contains the name of the service endpoint defined in </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">web.config</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">):</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Security.Principal;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">string endpoint = "NetTcpBinding_IPingContract";</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TokenImpersonationLevel til = TokenImpersonationLevel.Impersonation;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using (((WindowsIdentity)User.Identity).Impersonate()) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> string result = ServiceClient<IPingContract>.CreateProxy(endpoint, til).Ping();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">There are two classes where we added additional </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TokenImpersonationLevel</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> parameter - </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ServiceClient</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> and </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">WcfInterceptor</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ServiceClient</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> is a static class that returns a proxy object which implements service contract (interface).</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using Castle.DynamicProxy;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.ServiceModel;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.ServiceModel.Channels;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Security.Principal;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">public static class ServiceClient<T> where T : class {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private static ProxyGenerator generator = new ProxyGenerator();</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static T CreateProxy() {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return generator.CreateInterfaceProxyWithoutTarget<T>(new WcfInterceptor<T>());</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static T CreateProxy(string endpointConfigName) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return generator.CreateInterfaceProxyWithoutTarget<T>(new </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> WcfInterceptor<T>(endpointConfigName));</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static T CreateProxy(string endpointConfigName, </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> TokenImpersonationLevel tokenImpersonationLevel) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return generator.CreateInterfaceProxyWithoutTarget<T>(new </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> WcfInterceptor<T>(endpointConfigName, tokenImpersonationLevel));</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static T CreateProxy(Binding binding, EndpointAddress address) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return generator.CreateInterfaceProxyWithoutTarget<T>(new </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> WcfInterceptor<T>(binding, address));</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static T CreateProxy(Binding binding, EndpointAddress address, </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> TokenImpersonationLevel tokenImpersonationLevel) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return generator.CreateInterfaceProxyWithoutTarget<T>(new </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> WcfInterceptor<T>(binding, address, tokenImpersonationLevel));</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">WcfInterceptor</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> is internal class instantiated by </span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ServiceClient</span><span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. It accepts service contract as generic parameter.</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.ServiceModel;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.ServiceModel.Channels;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using Castle.DynamicProxy;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.ServiceModel.Description;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Net;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">using System.Security.Principal;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">...</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">internal sealed class WcfInterceptor<T> : IInterceptor where T : class {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private ChannelFactory<T> factory = null;</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public ClientCredentials Credentials {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> get { return this.factory.Credentials; }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor() {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(TokenImpersonationLevel til) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.AllowedImpersonationLevel = til;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(string endpointConfigName) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>(endpointConfigName);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(string endpointConfigName, TokenImpersonationLevel til) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>(endpointConfigName);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.AllowedImpersonationLevel = til;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(Binding binding, EndpointAddress address) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>(binding, address);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(Binding binding, EndpointAddress address, </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> TokenImpersonationLevel til) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory = new ChannelFactory<T>(binding, address);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.AllowedImpersonationLevel = til;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(NetworkCredential credential) : this() {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.ClientCredential = credential;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(string endpointConfigName, NetworkCredential credential) : </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this(endpointConfigName) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.ClientCredential = credential;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public WcfInterceptor(Binding binding, EndpointAddress address, </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> NetworkCredential credential) : this(binding, address) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.factory.Credentials.Windows.ClientCredential = credential;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public void Intercept(IInvocation invocation) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> T channel = CreateChannel();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> invocation.ReturnValue = invocation.Method.Invoke(channel, invocation.Arguments);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> CloseChannel(channel);</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private T CreateChannel() {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> return this.factory.CreateChannel();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private void CloseChannel(T channel) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> if (channel != null) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> try {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ICommunicationObject obj = (ICommunicationObject)channel;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> if (obj.State != CommunicationState.Faulted) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> if (obj.State != CommunicationState.Closed) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> obj.Close();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> } else {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> obj.Abort();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Logger.Debug(string.Format("* WcfInterceptor.CloseChannel </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> (ComState={0})", obj.State));</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> } catch {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> try {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ((ICommunicationObject)channel).Abort();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> } catch { throw; }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Consolas; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span>Damjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.com0tag:blogger.com,1999:blog-2721094459750954763.post-64821079750643981492012-11-15T21:32:00.000+01:002012-11-21T22:29:15.233+01:00Windows 8 RTM Memory leak - NDU.SYS?Few days after Win8 RTM launch I decided to install it on my old good HP Pavilion DV7 laptop. Win7 served me perfectly for the last two years and I was quite curious how Win8 (Windows 8 Pro Build 9200 x64) compares with it.<br />
<br />
I made a clean installation that took ~20min. New Modern UI..nice..polished desktop, speed,..nice too. Memory usage at start-up was similar as with Win7 (~1GB of total 4GB) but now the story begins. When I started to copy big files around LAN (ISO files, AVI,..), torrents,.. memory usage started to constantly grow. The new task manager was showing that the non-paged memory pool raised very fast; memory usage stopped at ~3.6GB and then machine started to crawl. Only restart helped every-time. The problem was always reproducible.<br />
<br />
Just Chrome browser and MS Word were running most of the time..memory usage 3.5GB !?!?!<br />
<br />
<img height="434px;" id="internal-source-marker_0.46650685446748985" src="https://lh5.googleusercontent.com/CJW3Uv7cES51iwptuRbfrza_D18WmNFFjmXGluZ2_6EAVWItX-Rf-_DU1kqqYSRqBc7WLhLNiNtlmbm76DNkmLSRhtQYGCA4Nj-w1oWArTJEO8pm9qrD" width="518px;" /><br />
<br />
<img height="434px;" id="internal-source-marker_0.46650685446748985" src="https://lh4.googleusercontent.com/OVb8MkznQCPDu-Nkh9Ih9vtZO_96I1Q4y3GypRf-3Yr8XVRYZzpJAcEbtf_I-fmdDaBBxM9yMelBDL9BpRGh_t6jd5TGydHTBNTdUxm6EniiPdqb4fPp" width="518px;" /><br />
<br />
I suspected some third-party network driver is reason for this. I used the tool “<i>poolmon.exe</i>” (part of Windows Driver Kit) to find out which driver is causing the memory leak (<a href="http://support.microsoft.com/kb/177415" target="_blank">link1</a>, <a href="http://haslerchan.blogspot.com/2011/08/memory-leak-trace-it-with-poolmon.html" target="_blank">link2</a>, <a href="http://blogs.msdn.com/b/ntdebugging/archive/2012/08/30/troubleshooting-pool-leaks-part-2-poolmon.aspx" target="_blank">link3</a>). <i>Poolmon.exe</i> showed that non-paged memory grows constantly when copying big files (few 100MB). The driver tag found by <i>poolmon.exe</i> was “Wfpn” and <i>findstr</i> found “<i>netio.sys</i>” that is the part of Win8 core networking subsystem. Therefore, one of network related driver is a problem, so let’s find which one.<br />
<br />
Laptop has intergrated gigabit NIC from Realtek (RTL8168) so I tried to update it:<br />
1. installed Win7 drivers (Win8 still not supported) from HP official site -> memory leak remains;<br />
2. installed Win8 latest drivers from Realtek -> memory leak remains;<br />
<br />
I even disabled LAN interface and tried WLAN only -> still memory leak.<br />
<br />
Therefore, the culprit is not NIC/WLAN driver at all. The next step was booting machine in “safe mode with networking” and I was surprised - no more memory leaks!! I made a list of loaded network drivers (using <a href="http://www.nirsoft.net/utils/driverview.html" target="_blank">DriverView</a>) and compared it with the list of drivers in normal boot. The following kernel drivers were not loaded in safe mode:<br />
- lltdio.sys,<br />
- mslldp.sys,<br />
- Ndu.sys,<br />
- rspndr.sys,<br />
- srv.sys, <br />
- srv2.sys, <br />
- srvnet.sys,<br />
- wanarp.sys.<br />
<br />
After disabling Ndu.sys (Windows Network Data Usage Monitoring Driver) with <a href="http://technet.microsoft.com/en-us/sysinternals/bb963902.aspx" target="_blank">Autoruns</a> and performing normal boot - voila memory leak has gone!!!!<br />
<br />
<a href="http://batcmd.com/windows/8/services/ndu/" target="_blank">Ndu</a> driver was introduced with Win8 and is actually quite buggy in combination with RTL8168 NIC. Microsoft should fix that in some SP...let's hope soon.<br />
<br />
I wonder if anyone else has encountered a similar memory leak?Damjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.com33tag:blogger.com,1999:blog-2721094459750954763.post-31882518585268573662012-06-29T13:36:00.000+02:002012-07-10T15:11:50.595+02:00Quartz.net task scheduling<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">Enterprise business apps often require that some code
is executed on regular basis as scheduled task (e.g. notifications, state
polling,..). There are some common ways how to achieve that:</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- writing Windows Service application with a timer</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- writing external .exe application run by Windows
Task Scheduler</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- plumbing some timer specific code</span><br />
<span lang="EN-US">- ... </span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- using Quartz.net</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US"><a href="http://quartznet.sourceforge.net/" target="_blank"><span style="color: blue;">Quartz.net 2.0</span></a> is open
source job scheduling framework that can be used from smallest apps to large
scale enterprise systems. Quartz.net is a pure .NET library written in C#. It
is ported from popular open source Java scheduling framework Quartz.</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">Below is a simple recipe how to add support for
scheduled jobs under ASP.NET (MVC) app. I use this approach for polling
MSMQ queue on specific intervals to receive new data packets. It is worth noting
that the Quartz.net framework is very powerful and we use only basic features.</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">1. Quartz.net configuration can be done either
programmatically or using external XML config file. The main components
of Quartz that need to be configured are:</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- </span><i><span lang="EN-US" style="font-family: Courier;">ThreadPool</span></i><span lang="EN-US">: provides a
set of threads to use when executing jobs;</span><span lang="EN-US"> </span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- </span><i><span lang="EN-US" style="font-family: Courier;">JobStore</span></i><span lang="EN-US">: is
responsible for keeping track of all work data that is given to the scheduler
(jobs, triggers, calendars). There are two job store implementations (custom
stores can be added by implementing </span><span lang="EN-US" style="font-family: Courier;">IJobStore</span><span lang="EN-US"> interface):</span><span lang="EN-US" style="font-family: Courier;"> RAMJobStore</span><span lang="EN-US">, </span><span lang="EN-US" style="font-family: Courier;">AdoJobStore</span><span lang="EN-US">.</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- </span><i><span lang="EN-US" style="font-family: Courier;">Scheduler</span></i><span lang="EN-US">: scheduler
itself by a given name and handled instances of a </span><span lang="EN-US" style="font-family: Courier;">JobStore</span><span lang="EN-US"> and </span><span lang="EN-US" style="font-family: Courier;">ThreadPool</span><span lang="EN-US">.</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">We add the following lines in web.config (app.config)
file (reference to Quartz.dll is required):</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">…</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"><</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">configSections</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">section</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">name</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">type</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">System.Configuration.NameValueSectionHandler</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">configSections</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"><</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">quartz</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <!--</span><span lang="EN-US" style="color: green; font-family: Consolas; font-size: 8pt;"> configure Thread Pool </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">--></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.threadPool.type</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">Quartz.Simpl.SimpleThreadPool, Quartz</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.threadPool.threadCount</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">10</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.threadPool.threadPriority</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">Normal</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <!--</span><span lang="EN-US" style="color: green; font-family: Consolas; font-size: 8pt;"> configure Job Store </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">--></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.jobStore.misfireThreshold</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">60000</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.jobStore.type</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">Quartz.Simpl.RAMJobStore, Quartz</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <!--</span><span lang="EN-US" style="color: green; font-family: Consolas; font-size: 8pt;"> configure scheduler </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">--></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.scheduler.instanceName</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">MSMQScheduler</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <!--</span><span lang="EN-US" style="color: green; font-family: Consolas; font-size: 8pt;"> configure jobs and triggers
definition </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">--></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.plugin.xml.type</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.plugin.xml.fileNames</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">~/quartzjobs.config</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">add</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">key</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">quartz.plugin.xml.scanInterval</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: red; font-family: Consolas; font-size: 8pt;">value</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">10</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"> /></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">quartz</span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">…</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">2. Definition of jobs & triggers can be also done
programmatically or using external XML config file (quartzjobs.config). About
jobs and triggers:</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- </span><i><span lang="EN-US" style="font-family: Courier;">Job</span></i><span lang="EN-US">: we make
.NET class executable by the scheduler simply by making it implement the </span><span lang="EN-US" style="font-family: Courier;">IJob</span><span lang="EN-US"> interface that has only one method </span><span lang="EN-US" style="font-family: Courier;">Execute(...)</span><span lang="EN-US">. It is
executed by the scheduler.</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">Here is our implementation of the MessageQueueJob:</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">namespace</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> EE.SmsFramework.Dispatcher.Scheduler {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">public</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">class</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">MessageQueueJob</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">: </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">IJob</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">public</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">void</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> Execute(</span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">IJobExecutionContext</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> context) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">try</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Logger</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">.Info(</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">"QUARTZ: Scheduled Job started"</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: green; font-family: Consolas; font-size: 8pt;">// packet dispatcher</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">PacketDispatcher</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> dispatcher = </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">new</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">PacketDispatcher</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> dispatcher.ProcessMessages();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Logger</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">.Info(</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">"QUARTZ: Scheduled Job ended"</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> } </span><span lang="EN-US" style="color: blue; font-family: Consolas; font-size: 8pt;">catch</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> (</span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Exception</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> ex) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Logger</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">.Error(</span><span lang="EN-US" style="color: #a31515; font-family: Consolas; font-size: 8pt;">" --> PacketDispatcher Execute Error:"</span><span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">, ex);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="color: black; font-family: Consolas; font-size: 8pt;">}</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">- </span><i><span lang="EN-US" style="font-family: Courier;">Trigger</span></i><span lang="EN-US">: it is used
to fire the execution of jobs.Most commonly used types are </span><span lang="EN-US" style="font-family: Courier;">SimpleTrigger</span><span lang="EN-US"> (single execution of a job at a given time) and </span><span lang="EN-US" style="font-family: Courier;">CronTrigger</span><span lang="EN-US"> (calendar-like schedules with <a href="http://www.cronmaker.com/" target="_blank"><span style="color: blue;">CRON</span></a>
expressions).</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">Here is our definition of a single job (</span><span lang="EN-US" style="font-family: Courier;">MSMQJob</span><span lang="EN-US">) and a single trigger (</span><span lang="EN-US" style="font-family: Courier;">MSMQJobTrigger</span><span lang="EN-US">) inside
quartzjobs.config:</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"><?</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">xml</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span style="color: red; font-family: Consolas; font-size: 8pt;">version</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">1.0</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">?></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-scheduling-data</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span style="color: red; font-family: Consolas; font-size: 8pt;">xmlns</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">http://quartznet.sourceforge.net/JobSchedulingData</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span style="color: red; font-family: Consolas; font-size: 8pt;">xmlns:xsi</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">http://www.w3.org/2001/XMLSchema-instance</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span style="color: red; font-family: Consolas; font-size: 8pt;">version</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">=</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">2.0</span><span style="color: black; font-family: Consolas; font-size: 8pt;">"</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">processing-directives</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">overwrite-existing-data</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">true</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">overwrite-existing-data</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">processing-directives</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">schedule</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJob</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJobs</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">description</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">Dispatches messages from MSMQ and sends them to SMS Postar.</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">description</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-type</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">EE.SmsFramework.Dispatcher.Scheduler.MessageQueueJob, SmsFramework.Dispatcher</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-type</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">durable</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">true</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">durable</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">recover</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">false</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">recover</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">trigger</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">cron</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJobTrigger</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJobs</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">description</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">Triggers MSMQ dispatcher job</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">description</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJob</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-name</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">MSMQJobs</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-group</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <!--</span><span style="color: green; font-family: Consolas; font-size: 8pt;"> every 15 minutes </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">--></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> <</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">cron-expression</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">0 0/15 * * * ?</span><span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">cron-expression</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">cron</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">trigger</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"> </</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">schedule</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: blue; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">job-scheduling-data</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">3. Instantiation of a Quartz.net Scheduler instance is
maintained by </span><span lang="EN-US" style="font-family: Courier;">StdSchedulerFactory</span><span lang="EN-US"> which implements </span><span lang="EN-US" style="font-family: Courier;">ISchedulerFactory</span><span lang="EN-US">. It uses a set of properties (</span><span lang="EN-US" style="font-family: Courier;">NameValueCollection</span><span lang="EN-US">) to create and initialize a Quartz Scheduler. The
properties are generally stored in and loaded from a file, but can also be
created by your program and handed directly to the factory. Simply calling </span><span lang="EN-US" style="font-family: Courier;">GetScheduler()</span><span lang="EN-US"> on the factory will produce the scheduler, initialize
it (and its </span><span lang="EN-US" style="font-family: Courier;">ThreadPool</span><span lang="EN-US"> and </span><span lang="EN-US" style="font-family: Courier;">JobStore</span><span lang="EN-US">), and return a handle to its public interface.</span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span lang="EN-US">We instantiate Scheduler instance inside the </span><span lang="EN-US" style="font-family: Courier;">HttpApplication</span><span lang="EN-US"> </span><span lang="EN-US" style="font-family: Courier;">OnStart</span><span lang="EN-US"> event
(Global.asax). Web application instance is required to be always alive (IIS
application pool must not be stopped). This is simply done by executing void
web request inside </span><span lang="EN-US" style="font-family: Courier;">OnEnd</span><span lang="EN-US"> event. </span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">public</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">class</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">MvcApplication</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> : </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">HttpApplication</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">private</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">IScheduler</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> scheduler = </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">null</span><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">protected</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">void</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> Application_Start(</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">object</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> sender, </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">EventArgs</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> e) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> ...</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: green; font-family: Consolas; font-size: 8pt;">// quartz scheduler</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 4pt;"> </span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">ISchedulerFactory</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> factory = </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">new</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">StdSchedulerFactory</span><span style="color: black; font-family: Consolas; font-size: 8pt;">();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">this</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.scheduler = factory.GetScheduler();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">this</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.scheduler.Start();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> ...</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">protected</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">void</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> Application_End(</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">object</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> sender, </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">EventArgs</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> e) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> ...</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">this</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.PingServer();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span>
<br />
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: grey; font-family: Consolas; font-size: 8pt;">///</span><span style="color: green; font-family: Consolas; font-size: 8pt;"> </span><span style="color: grey; font-family: Consolas; font-size: 8pt;"><summary></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: grey; font-family: Consolas; font-size: 8pt;">///</span><span style="color: green; font-family: Consolas; font-size: 8pt;"> Pings the server - startup the IIS application pool.</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: grey; font-family: Consolas; font-size: 8pt;">///</span><span style="color: green; font-family: Consolas; font-size: 8pt;"> </span><span style="color: grey; font-family: Consolas; font-size: 8pt;"></summary></span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">private</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">void</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> PingServer() {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">try</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">WebClient</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> client = </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">new</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">WebClient</span><span style="color: black; font-family: Consolas; font-size: 8pt;">();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> client.DownloadString(</span><span style="color: blue; font-family: Consolas; font-size: 8pt;">string</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.Format(</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">"http://{0}{1}"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">, </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Environment</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.MachineName, </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">VirtualPathUtility</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.ToAbsolute(</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">"~/"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">)));</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> } </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">catch</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> (</span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">WebException</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> webex) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: green; font-family: Consolas; font-size: 8pt;">// possible 'Unauthorized' - do nothing</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> } </span><span style="color: blue; font-family: Consolas; font-size: 8pt;">catch</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> (</span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Exception</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> ex) {</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 8pt;">Logger</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.Error(</span><span style="color: #a31515; font-family: Consolas; font-size: 8pt;">" PingServer error:"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">, ex);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> ...</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div>
<div class="MsoNoSpacing">
<span lang="EN-US"> </span></div>
<div class="MsoNoSpacing">
<span lang="EN-US">The job </span><span lang="EN-US" style="font-family: Courier;">MSMQJob</span><span lang="EN-US"> is
triggered in its own thread every 15 minutes:</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">... </span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">13:45:00.0133
[Thread:Worker-2] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
started</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">13:45:01.0133
[Thread:Worker-2] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
ended</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">14:00:00.0133
[Thread:Worker-1] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
started</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">14:00:01.0133
[Thread:Worker-1] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
ended</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">14:15:00.0133
[Thread:Worker-4] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
started</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">14:15:01.0133
[Thread:Worker-4] EE.SmsFramework.Common.Logger[INFO]: QUARTZ: Scheduled Job
ended</span></div>
<div class="MsoNoSpacing">
<span lang="EN-US" style="font-family: Consolas; font-size: 8pt;">... </span></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<br /></div>Damjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.com2tag:blogger.com,1999:blog-2721094459750954763.post-53418597640770386122012-02-20T20:20:00.000+01:002012-07-17T14:17:13.605+02:00.NET Reflection: improvedEvery serious .NET developer probably already used .NET reflection mechanism to dynamically invoke methods. Recently I've written plugable framework (using <a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">IoC</a> pattern) and I've found out that there is much faster way to invoke methods on different types/assemblies using <span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">System.Reflection.Emit.DynamicMethod</span></span> than regular reflection using <span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">Type.InvokeMember()</span></span>. C# 2.0 (3.5) is required.<br />
<br />
The method in different assembly is invoked using delegate. Below is the implementation of static class <span style="font-family: "Courier New",Courier,monospace;">FastMethodInvoker</span> (we actually generate IL specific code):<br />
<blockquote class="tr_bq">
<pre style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas;"><span style="font-size: x-small;"><span style="color: blue;"> public</span> <span style="color: blue;">delegate</span> <span style="color: blue;">object</span> <span style="color: #2b91af;">FastInvokeHandler</span>(<span style="color: blue;">object</span> _target, <span style="color: blue;">object</span>[] _params);
<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;"> A class to invoke methods (DynamicMethod mechanism).</span></span>
<span class="cmt" style="background-color: transparent; border-width: 0px; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px; vertical-align: baseline;"><span class="com" style="background-color: transparent; border-width: 0px; color: #880000; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px; vertical-align: baseline;"></span></span><span style="font-size: x-small;"><span style="color: green;"></span> <span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"></summary></span>
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">FastMethodInvoker</span> {
<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;"> Invokes method.</span>
<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;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"_target"</span><span style="color: grey;">></span><span style="color: green;">Instance of target object.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"_sMethodName"</span><span style="color: grey;">></span><span style="color: green;">Method name.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"_params"</span><span style="color: grey;">></span><span style="color: green;">Array of params.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><returns></span><span style="color: green;">Result of method.</span><span style="color: grey;"></returns></span>
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> InvokeMethod(<span style="color: blue;">object</span> _target, <span style="color: blue;">
string</span> _sMethodName, <span style="color: blue;">
object</span>[] _params) {
<span style="color: #2b91af;">Type</span> type = _target.GetType();
<span style="color: blue;">bool</span> bExists = type.GetMethods().Any(t => t.Name == _sMethodName);
<span style="color: blue;">if</span> (!bExists)
<span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentException</span>(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"Non existing method '{0}' on type '{1}'."</span>,
_sMethodName,
_target.GetType().Name));
<span style="color: #2b91af;">MethodInfo</span> mi = type.GetMethod(_sMethodName);
<span style="color: #2b91af;">FastInvokeHandler</span> fastInvoker = GetMethodInvoker(mi);
<span style="color: blue;">return</span> fastInvoker(_target, _params) ?? <span style="color: blue;">null</span>;
}
<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;"> Returns DynamicMethod.</span>
<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;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"_methodInfo"</span><span style="color: grey;">></span><span style="color: green;">MethodInfo instance.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><returns></span><span style="color: green;">FastInvokeHandler delegate.</span><span style="color: grey;"></returns></span>
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">FastInvokeHandler</span> GetMethodInvoker(<span style="color: #2b91af;">MethodInfo</span> _methodInfo) {
<span style="color: #2b91af;">DynamicMethod</span> dynamicMethod = <span style="color: blue;">new</span> <span style="color: #2b91af;">DynamicMethod</span>(<span style="color: blue;">string</span>.Empty, <span style="color: blue;">
typeof</span>(<span style="color: blue;">object</span>), <span style="color: blue;">
new</span> <span style="color: #2b91af;">Type</span>[] {<span style="color: blue;">typeof</span>(<span style="color: blue;">object</span>), <span style="color: blue;">typeof</span>(<span style="color: blue;">object</span>[])},
_methodInfo.DeclaringType.Module);
<span style="color: #2b91af;">ILGenerator</span> il = dynamicMethod.GetILGenerator();
<span style="color: #2b91af;">ParameterInfo</span>[] ps = _methodInfo.GetParameters();
<span style="color: #2b91af;">Type</span>[] paramTypes = <span style="color: blue;">new</span> <span style="color: #2b91af;">Type</span>[ps.Length];
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> ii = 0; ii < paramTypes.Length; ii++) {
<span style="color: blue;">if</span> (ps[ii].ParameterType.IsByRef)
paramTypes[ii] = ps[ii].ParameterType.GetElementType();
<span style="color: blue;">else</span>
paramTypes[ii] = ps[ii].ParameterType;
}
<span style="color: #2b91af;">LocalBuilder</span>[] locals = <span style="color: blue;">new</span> <span style="color: #2b91af;">LocalBuilder</span>[paramTypes.Length];
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> ii = 0; ii < paramTypes.Length; ii++) {
locals[ii] = il.DeclareLocal(paramTypes[ii], <span style="color: blue;">true</span>);
}
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> ii = 0; ii < paramTypes.Length; ii++) {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldarg_1);
EmitFastInt(il, ii);
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldelem_Ref);
EmitCastToReference(il, paramTypes[ii]);
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Stloc, locals[ii]);
}
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldarg_0);
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> ii = 0; ii < paramTypes.Length; ii++) {
<span style="color: blue;">if</span> (ps[ii].ParameterType.IsByRef)
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldloca_S, locals[ii]);
<span style="color: blue;">else</span>
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldloc, locals[ii]);
}
il.EmitCall(<span style="color: #2b91af;">OpCodes</span>.Callvirt, _methodInfo, <span style="color: blue;">null</span>);
<span style="color: blue;">if</span> (_methodInfo.ReturnType == <span style="color: blue;">typeof</span>(<span style="color: blue;">void</span>))
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldnull);
<span style="color: blue;">else</span>
EmitBoxIfNeeded(il, _methodInfo.ReturnType);
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> ii = 0; ii < paramTypes.Length; ii++) {
<span style="color: blue;">if</span> (ps[ii].ParameterType.IsByRef) {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldarg_1);
EmitFastInt(il, ii);
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldloc, locals[ii]);
<span style="color: blue;">if</span> (locals[ii].LocalType.IsValueType)
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Box, locals[ii].LocalType);
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Stelem_Ref);
}
}
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ret);
<span style="color: #2b91af;">FastInvokeHandler</span> invoker =
(<span style="color: #2b91af;">FastInvokeHandler</span>)dynamicMethod.CreateDelegate(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">FastInvokeHandler</span>));
<span style="color: blue;">return</span> invoker;
}
<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;"> Emits the cast to reference.</span>
<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;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"il"</span><span style="color: grey;">></span><span style="color: green;">The il.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"type"</span><span style="color: grey;">></span><span style="color: green;">The type.</span><span style="color: grey;"></param></span>
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> EmitCastToReference(<span style="color: #2b91af;">ILGenerator</span> il, System.<span style="color: #2b91af;">Type</span> type) {
<span style="color: blue;">if</span> (type.IsValueType) {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Unbox_Any, type);
} <span style="color: blue;">else</span> {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Castclass, type);
}
}
<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;"> Emits the box if needed.</span>
<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;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"il"</span><span style="color: grey;">></span><span style="color: green;">The il.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"type"</span><span style="color: grey;">></span><span style="color: green;">The type.</span><span style="color: grey;"></param></span>
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> EmitBoxIfNeeded(<span style="color: #2b91af;">ILGenerator</span> il, System.<span style="color: #2b91af;">Type</span> type) {
<span style="color: blue;">if</span> (type.IsValueType) {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Box, type);
}
}
<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;"> Emits the fast int.</span>
<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;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"il"</span><span style="color: grey;">></span><span style="color: green;">The il.</span><span style="color: grey;"></param></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><param name=</span><span style="color: grey;">"value"</span><span style="color: grey;">></span><span style="color: green;">The value.</span><span style="color: grey;"></param></span>
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> EmitFastInt(<span style="color: #2b91af;">ILGenerator</span> il, <span style="color: blue;">int</span> value) {
<span style="color: blue;">switch</span> (value) {
<span style="color: blue;">case</span> -1:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_M1);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 0:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_0);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 1:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_1);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 2:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_2);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 3:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_3);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 4:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_4);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 5:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_5);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 6:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_6);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 7:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_7);
<span style="color: blue;">return</span>;
<span style="color: blue;">case</span> 8:
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_8);
<span style="color: blue;">return</span>;
}
<span style="color: blue;">if</span> (value > -129 && value < 128) {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4_S, (<span style="color: #2b91af;">SByte</span>)value);
} <span style="color: blue;">else</span> {
il.Emit(<span style="color: #2b91af;">OpCodes</span>.Ldc_I4, value);
}
}
}</span></pre>
</blockquote>
The usage is simple:<br />
<br />
a) Invoking some method on external DLL assembly:<br />
<blockquote class="tr_bq">
<pre style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas;"><span style="font-size: x-small;">...<span style="color: green;">
// invoke method MyMethod(string) on type MyNamespace.MyClass (MyAssembly.dll)</span>
<span style="color: #2b91af;">Assembly</span> a = <span style="color: #2b91af;">Assembly</span>.LoadFrom(<span style="color: #a31515;">"MyAssembly.dll"</span>);
<span style="color: blue;">object</span> instance = a.CreateInstance(<span style="color: #a31515;">"MyNamespace.MyClass"</span>);
<span style="color: blue;">object</span>[] args = <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { <span style="color: #a31515;">"some_string_argument"</span> };
<span style="color: blue;">object</span> result = </span><span style="font-size: x-small;"><span style="color: #2b91af;">FastMethodInvoker.</span></span><span style="font-size: x-small;">InvokeMethod(instance, <span style="color: #a31515;">"MyMethod"</span>, args);
...</span></pre>
</blockquote>
<br />
b) Invoking some method on known instance:<br />
<blockquote class="tr_bq">
<pre style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas;"><span style="font-size: x-small;">... <span style="color: green;">
// e.g. invoke GetXml() on DataSet instance</span>
<span style="color: #2b91af;">Type</span> type = <span style="color: blue;">typeof</span>(System.Data.<span style="color: #2b91af;">DataSet</span>);
<span style="color: blue;">object</span> instance = <span style="color: #2b91af;">Activator</span>.CreateInstance(type);
<span style="color: blue;">object</span> result = InvokeMethod(instance, <span style="color: #a31515;">"GetXml"</span>, <span style="color: blue;">null</span>);
...</span></pre>
</blockquote>
As a matter of fact this mechanism is about 5x - 10x faster than regular .NET reflection.<br />
<br />Damjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.com5tag:blogger.com,1999:blog-2721094459750954763.post-38336153913168601332007-12-25T15:18:00.000+01:002007-12-25T15:29:50.409+01:00Winters moments<span style="font-family:lucida grande;"><span style="font-family:georgia;">Last Sunday before Christmas it was cloudy and cold winter day. I haven't taken pictures for a while and I went to the neighbor hill where is always so many nice moments to catch. I took my old good Fujifilm S5500 camera and I took some shots.. you can see them in the Winter moments slideshow.. Friendly horses were nearby..</span><br /><br /><a style="font-family: georgia;" href="http://picasaweb.google.com/damjank/Winter" target="_blank">Link to pictures</a><br /></span>Damjan Kovachttp://www.blogger.com/profile/15447410583277217117noreply@blogger.com0