<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ryan Hauert</title>
	<atom:link href="http://rhauert.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rhauert.wordpress.com</link>
	<description>Creating competitive advantage one method at a time</description>
	<lastBuildDate>Sun, 22 May 2011 16:10:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rhauert.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ryan Hauert</title>
		<link>http://rhauert.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rhauert.wordpress.com/osd.xml" title="Ryan Hauert" />
	<atom:link rel='hub' href='http://rhauert.wordpress.com/?pushpress=hub'/>
		<item>
		<title>PostSharp RC3 MSBuild/Code Analysis error</title>
		<link>http://rhauert.wordpress.com/2008/06/16/postsharp-rc3-msbuildcode-analysis-error/</link>
		<comments>http://rhauert.wordpress.com/2008/06/16/postsharp-rc3-msbuildcode-analysis-error/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 15:57:09 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[PostSharp]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[PostSharp RC3]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/06/16/postsharp-rc3-msbuildcode-analysis-error/</guid>
		<description><![CDATA[I&#8217;ve been meaning to write this post ever since David and I came across this error after we installed the new PostSharp RC3 released a few weeks ago.&#160; After building a project and running code analysis on it, we received the following error: &#8220;CA0055 : * Could not load file: &#8216;@(IntermediateAssembly-&#62;&#8217;obj\Debug\Before-PostSharp\ExtendHealth.Utilities.dll&#8217;.&#8221;&#160; After a little digging, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=38&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to write this post ever since <a href="http://blogs.rev-net.com/ddewinter" target="_blank">David </a>and I came across this error after we installed the new <a href="http://www.postsharp.org/blog/2008/05/announcing-postsharp-10-rc3.html" target="_blank">PostSharp RC3</a> released a few weeks ago.&nbsp; After building a project and running code analysis on it, we received the following error: &#8220;CA0055 : * Could not load file: &#8216;@(IntermediateAssembly-&gt;&#8217;obj\Debug\Before-PostSharp\ExtendHealth.Utilities.dll&#8217;.&#8221;&nbsp; After a little digging, we found a small typo in the latest .targets file included in the PostSharp install.&nbsp; A couple characters are missing from line 186 of PostSharp-1.0.targets: </p>
<blockquote><p>&lt;CreateProperty Value=&#8221;@(IntermediateAssembly-&gt;&#8217;$(_PostSharpInputBin)<font color="#ff0000">&#8216;)</font>&#8220;&gt;</p>
</blockquote>
<p>The &#8216; and ) in red above were missing.&nbsp; After we fixed the typo, code analysis worked perfectly.&nbsp; This typo has been fixed in the latest <a href="http://download.postsharp.org/builds/1.0/" target="_blank">daily build</a> (1.0.9.377), but that build was emitting invalid IL and causing InvalidProgramExceptions to be thrown.&nbsp; It&#8217;s simple to fix, so we&#8217;ve just remembered to do it on each machine using the latest release candidate.&nbsp; The default location of the targets file is &#8220;%ProgramFiles%\PostSharp 1.0&#8243;.</p>
<p>NOTE: If running Vista, be sure to run the text editor with administrative privileges to update the file.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=38&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/06/16/postsharp-rc3-msbuildcode-analysis-error/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Function Mapping with Inherited Types</title>
		<link>http://rhauert.wordpress.com/2008/05/20/function-mapping-with-inherited-types/</link>
		<comments>http://rhauert.wordpress.com/2008/05/20/function-mapping-with-inherited-types/#comments</comments>
		<pubDate>Tue, 20 May 2008 19:50:58 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Entity Framework designer]]></category>
		<category><![CDATA[function mapping]]></category>
		<category><![CDATA[SSDL]]></category>
		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/?p=27</guid>
		<description><![CDATA[This post was inspired by a post on the Entity Framework forums asking for an example of mapping functions to a base type and its inherited types.&#160; I&#8217;m going to show two ways to go about it with a simple model.&#160; Note that this isn&#8217;t necessarily the best data model, it&#8217;s just a Table-Per-Hierarchy (TPH) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=27&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post was inspired by a <a href="http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3371663&amp;SiteID=1" target="_blank">post</a> on the Entity Framework forums asking for an example of mapping functions to a base type and its inherited types.&nbsp; I&#8217;m going to show two ways to go about it with a simple model.&nbsp; Note that this isn&#8217;t necessarily the best data model, it&#8217;s just a Table-Per-Hierarchy (TPH) example.&nbsp; We have a top-level Person type with an Employee subtype, which subsequently has a Manager subtype, defined in one table:</p>
<p>CREATE TABLE [dbo].[Person](<br />&nbsp;&nbsp;&nbsp; [PersonId] [int] IDENTITY(1,1) NOT NULL,<br />&nbsp;&nbsp;&nbsp; [FirstName] [varchar](50) NOT NULL,<br />&nbsp;&nbsp;&nbsp; [PersonTypeId] [int] NOT NULL,<br />&nbsp;&nbsp;&nbsp; [EmployeeTypeId] [int] NULL,<br />&nbsp;&nbsp;&nbsp; [HireDate] [datetime] NULL,<br />&nbsp;&nbsp;&nbsp; [TeamName] [varchar](50) NULL,<br />&nbsp;&nbsp;&nbsp; [OfficeLocation] [varchar](50) NULL,<br />CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
<p>The Entity Model is defined below:
<p><a href="http://rhauert.files.wordpress.com/2008/05/functionmappingmodel1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="388" alt="FunctionMappingModel" src="http://rhauert.files.wordpress.com/2008/05/functionmappingmodel-thumb1.png?w=445&#038;h=388" width="445" border="0"></a>
<p>There are at least two ways to map the three entities to CUD stored procedures.&nbsp; We could create one procedure per table, or we could create one procedure per type.&nbsp; In this post I&#8217;ll show how to map the entities in the hierarchy to the same three stored procedures, which I generated using the <a href="http://blogs.rev-net.com/ddewinter/2008/04/26/entity-framework-stored-procedure-generation/" target="_blank">T4 template</a> <a href="http://blogs.rev-net.com/ddewinter" target="_blank">David DeWinter</a> and I developed.&nbsp; The Insert, Update, and Delete stored procedures are defined as follows:</p>
<p><span style="color:blue;">CREATE PROCEDURE</span> &#8220;dbo&#8221;.&#8221;zsp_Person_Insert&#8221;<br />(<br />&nbsp;&nbsp;&nbsp; @FirstName <span style="color:blue;">varchar</span>(50),<br />&nbsp;&nbsp;&nbsp; @PersonTypeId <span style="color:blue;">int</span>,<br />&nbsp;&nbsp;&nbsp; @EmployeeTypeId <span style="color:blue;">int</span>,<br />&nbsp;&nbsp;&nbsp; @HireDate <span style="color:blue;">datetime</span>,<br />&nbsp;&nbsp;&nbsp; @TeamName <span style="color:blue;">varchar</span>(50),<br />&nbsp;&nbsp;&nbsp; @OfficeLocation <span style="color:blue;">varchar</span>(50)<br />)<br /><span style="color:blue;">AS</span><br /><span style="color:blue;">BEGIN</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">INSERT INTO</span> &#8220;dbo&#8221;.&#8221;Person&#8221;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;FirstName&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;PersonTypeId&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;EmployeeTypeId&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;HireDate&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;TeamName&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;OfficeLocation&#8221;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">VALUES</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @FirstName,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @PersonTypeId,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @EmployeeTypeId,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @HireDate,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @TeamName,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @OfficeLocation<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">SELECT SCOPE_IDENTITY</span>() AS PersonId<br /><span style="color:blue;">END</span><br />GO
<p><span style="color:blue;">CREATE PROCEDURE</span> &#8220;dbo&#8221;.&#8221;zsp_Person_Update&#8221;<br />(<br />&nbsp;&nbsp;&nbsp; @PersonId <span style="color:blue;">int</span>,<br />&nbsp;&nbsp;&nbsp; @FirstName <span style="color:blue;">varchar</span>(50),<br />&nbsp;&nbsp;&nbsp; @PersonTypeId int,<br />&nbsp;&nbsp;&nbsp; @EmployeeTypeId <span style="color:blue;">int</span>,<br />&nbsp;&nbsp;&nbsp; @HireDate <span style="color:blue;">datetime</span>,<br />&nbsp;&nbsp;&nbsp; @TeamName <span style="color:blue;">varchar</span>(50),<br />&nbsp;&nbsp;&nbsp; @OfficeLocation <span style="color:blue;">varchar</span>(50)<br />)<br /><span style="color:blue;">AS</span><br /><span style="color:blue;">BEGIN</span><br />&nbsp;&nbsp;&nbsp; <span style="color:blue;">UPDATE</span> &#8220;dbo&#8221;.&#8221;Person&#8221;<br />&nbsp;&nbsp;&nbsp; <span style="color:blue;">SET</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;FirstName&#8221; = @FirstName,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;PersonTypeId&#8221; = @PersonTypeId,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;EmployeeTypeId&#8221; = @EmployeeTypeId,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;HireDate&#8221; = @HireDate,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;TeamName&#8221; = @TeamName,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;OfficeLocation&#8221; = @OfficeLocation<br />&nbsp;&nbsp;&nbsp; <span style="color:blue;">WHERE</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;PersonId&#8221; = @PersonId<br /><span style="color:blue;">END</span><br />GO
<p><span style="color:blue;">CREATE PROCEDURE</span> &#8220;dbo&#8221;.&#8221;zsp_Person_Delete&#8221;<br />(<br />&nbsp;&nbsp;&nbsp; @PersonId <span style="color:blue;">int</span><br />)<br /><span style="color:blue;">AS</span><br /><span style="color:blue;">BEGIN</span><br />&nbsp;&nbsp;&nbsp; <span style="color:blue;">DELETE FROM</span> &#8220;dbo&#8221;.&#8221;Person&#8221;<br />&nbsp;&nbsp;&nbsp; <span style="color:blue;">WHERE</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;PersonId&#8221; = @PersonId<br /><span style="color:blue;">END</span><br />GO</p>
<p>The trick in this case is to create some &#8220;wrapper&#8221; functions in the SSDL for the subtypes as Colin Meek suggested in <a href="http://forums.microsoft.com/forums/ShowPost.aspx?PostID=3267581&amp;SiteID=1" target="_blank">this forum post</a>.&nbsp;&nbsp; We don&#8217;t need to create functions for deleting, because the original delete stored procedure works fine.&nbsp; Here is the SSDL I ended up with:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">zsp_Person_Delete</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">zsp_Person_Insert</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonTypeId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">EmployeeTypeId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">OfficeLocation</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">zsp_Person_Update</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonTypeId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">EmployeeTypeId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">OfficeLocation</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Employee_Insert</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;![CDATA[</span><span style="color:gray;">exec dbo.zsp_Person_Insert @FirstName=@Firstname, @PersonTypeId=2, @EmployeeTypeId=1, @HireDate=@HireDate, @TeamName=@TeamName, @OfficeLocation=null</span><span style="color:blue;">]]&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Employee_Update</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;![CDATA[</span><span style="color:gray;">exec dbo.zsp_Person_Update @FirstName=@Firstname, @PersonTypeId=2, @EmployeeTypeId=1, @HireDate=@HireDate, @TeamName=@TeamName, @OfficeLocation=null</span><span style="color:blue;">]]&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Manager_Insert</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;![CDATA[</span><span style="color:gray;">exec dbo.zsp_Person_Insert @FirstName=@Firstname, @PersonTypeId=2, @EmployeeTypeId=2, @HireDate=@HireDate, @TeamName=@TeamName, @OfficeLocation=@OfficeLocation</span><span style="color:blue;">]]&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">OfficeLocation</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Function</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Manager_Update</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">&#8230;</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;![CDATA[</span><span style="color:gray;">exec dbo.zsp_Person_Update @FirstName=@Firstname, @PersonTypeId=2, @EmployeeTypeId=2, @HireDate=@HireDate, @TeamName=@TeamName, @OfficeLocation=@OfficeLocation</span><span style="color:blue;">]]&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">CommandText</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">PersonId</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">int</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">FirstName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">HireDate</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">datetime</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">TeamName</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Parameter</span><span style="color:blue;"> </span><span style="color:red;">Name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">OfficeLocation</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">varchar</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">Mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">In</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">Function</span><span style="color:blue;">&gt;</span></p>
</div>
<p>These &#8220;fake&#8221; stored procedures call the real stored procedure and pass in constant values where needed, such as the columns used in the conditions of the subtypes.&nbsp; After creating the functions in the SSDL, we just need to map them to the entities using the designer.&nbsp; They now show up in the drop down menu in the Mapping Details window:</p>
<p><a href="http://rhauert.files.wordpress.com/2008/05/wrapperfunctions1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="194" alt="WrapperFunctions" src="http://rhauert.files.wordpress.com/2008/05/wrapperfunctions-thumb1.png?w=373&#038;h=194" width="373" border="0"></a> </p>
<p>We map the real stored procedures to the base Person type and ignore the extra parameters, or we could have created &#8220;wrappers&#8221; for the Person entity as well.</p>
<p><a href="http://rhauert.files.wordpress.com/2008/05/personmapping1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="746" alt="PersonMapping" src="http://rhauert.files.wordpress.com/2008/05/personmapping-thumb1.png?w=619&#038;h=746" width="619" border="0"></a> </p>
<p>The Employee type is mapped to the wrapper functions.&nbsp; The properties inherited from Person aren&#8217;t automagically mapped, but it&#8217;s easy enough to select them from the drop down.</p>
<p><a href="http://rhauert.files.wordpress.com/2008/05/employeemapping1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="642" alt="EmployeeMapping" src="http://rhauert.files.wordpress.com/2008/05/employeemapping-thumb1.png?w=623&#038;h=642" width="623" border="0"></a> </p>
<p>And, finally, the Manager entity:</p>
<p><a href="http://rhauert.files.wordpress.com/2008/05/managermapping1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="689" alt="ManagerMapping" src="http://rhauert.files.wordpress.com/2008/05/managermapping-thumb1.png?w=623&#038;h=689" width="623" border="0"></a> </p>
<p>You might have noticed that the Employee and Manager insert function mappings don&#8217;t show any Result Column Bindings.&nbsp; I did add them, but the designer doesn&#8217;t show them.&nbsp; It&#8217;s a bug I described in my <a href="http://rhauert.wordpress.com/2008/05/14/new-entity-framework-designer-bugs-in-sp1-beta/" target="_blank">last post</a>.&nbsp; The last quirk to remember when mapping these &#8220;wrapper&#8221; functions is that they will be deleted from the SSDL after running the Update Model from Database wizard.&nbsp; If this solution doesn&#8217;t seem attractive, there is another option.&nbsp; You can create stored procedures for each type and map those procedures to their respective entity.&nbsp; I won&#8217;t show how to do that here, but it should be fairly straightforward.</p>
<p>Special thanks to Dave for his original work setting up the function mappings for our large model.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/27/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/27/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=27&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/05/20/function-mapping-with-inherited-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>

		<media:content url="http://rhauert.files.wordpress.com/2008/05/functionmappingmodel-thumb1.png" medium="image">
			<media:title type="html">FunctionMappingModel</media:title>
		</media:content>

		<media:content url="http://rhauert.files.wordpress.com/2008/05/wrapperfunctions-thumb1.png" medium="image">
			<media:title type="html">WrapperFunctions</media:title>
		</media:content>

		<media:content url="http://rhauert.files.wordpress.com/2008/05/personmapping-thumb1.png" medium="image">
			<media:title type="html">PersonMapping</media:title>
		</media:content>

		<media:content url="http://rhauert.files.wordpress.com/2008/05/employeemapping-thumb1.png" medium="image">
			<media:title type="html">EmployeeMapping</media:title>
		</media:content>

		<media:content url="http://rhauert.files.wordpress.com/2008/05/managermapping-thumb1.png" medium="image">
			<media:title type="html">ManagerMapping</media:title>
		</media:content>
	</item>
		<item>
		<title>New Entity Framework designer bugs in SP1 beta</title>
		<link>http://rhauert.wordpress.com/2008/05/14/new-entity-framework-designer-bugs-in-sp1-beta/</link>
		<comments>http://rhauert.wordpress.com/2008/05/14/new-entity-framework-designer-bugs-in-sp1-beta/#comments</comments>
		<pubDate>Wed, 14 May 2008 18:25:08 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[Entity Framework designer]]></category>
		<category><![CDATA[EntityKey]]></category>
		<category><![CDATA[EntityReference]]></category>
		<category><![CDATA[LoadIfNeeded]]></category>
		<category><![CDATA[result column binding]]></category>
		<category><![CDATA[Visual Studio Service Pack 1 beta]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/05/14/new-entity-framework-designer-bugs-in-sp1-beta/</guid>
		<description><![CDATA[David DeWinter and I have come across a few new and annoying bugs since we installed the Visual Studio Service Pack 1 beta yesterday.&#160; The first and most annoying is detailed in a forum post in the MSDN Entity Framework forum.&#160; The Error List window now lists fake errors for all of our subtypes that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=16&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.rev-net.com/ddewinter" target="_blank">David DeWinter</a> and I have come across a few new and annoying bugs since we installed the Visual Studio Service Pack 1 beta yesterday.&nbsp; The first and most annoying is detailed in a <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3324129&amp;SiteID=1" target="_blank">forum post</a> in the <a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=533&amp;SiteID=1" target="_blank">MSDN Entity Framework forum</a>.&nbsp; The Error List window now lists fake errors for all of our subtypes that are children of abstract entities with a message similar to &#8220;Entity types B, C, D are mapped to table A without discriminating conditions on all mappings to the table.&#8221;&nbsp; The project still builds and the errors can be ignored.</p>
<p>Another bug we found today relates to result column bindings for mapped sprocs on subtypes.&nbsp; We have the results from Insert sprocs mapped to properties on the entity, such the as the Id.&nbsp; These bindings don&#8217;t show up at all in the Mapping Details window, even though they exist in the MSL in the .edmx file.&nbsp; At first I thought the mapping wasn&#8217;t there, and tried to add one.&nbsp; After deselecting that entity and reselecting it the Mapping Details window showed no result column bindings.&nbsp; I opened the file in the XML editor and looked at the mapping for that insert sproc.&nbsp; There were several duplicate result column bindings there from my attempts to add it with the designer.&nbsp; We&#8217;ve reported this bug at <a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=344058" target="_blank">Microsoft Connect</a>.</p>
<p>The last problem we ran into is not actually a bug, but a change in behavior.&nbsp; Dave created a <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3341877&amp;SiteID=1" target="_blank">thread</a> about it at the Entity Framework forum.&nbsp; We had created the following extension method relying on the EntityKey of an EntityReference:</p>
<div style="font-size:10pt;color:black;font-family:courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Loads the specified </span><span style="color:gray;">&lt;see cref=&#8221;EntityReference&#8221;/&gt;</span><span style="color:green;"> if it is not loaded</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> and only if there is an actual reference to load.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;param name=&#8221;entityReference&#8221;&gt;</span><span style="color:green;">The entity reference </span><span style="color:gray;">&lt;/param&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> LoadIfNeeded(<span style="color:blue;">this</span> <span style="color:#2b91af;">EntityReference</span> entityReference)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// The EntityKey will be null if there is no reference.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (!entityReference.IsLoaded &amp;&amp; entityReference.EntityKey != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entityReference.Load();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>We had a situation where an entity shares a 0..1 relationship with another entity.&nbsp; Calling Load() when that relationship was null caused an unnecessary database hit, which was exaggerated when called in a loop on several entities.&nbsp; We&#8217;ve since been able to remove the need for a LoadIfNeeded() method, but it is still nice to have an EntityKey on references for other uses.</p>
<p>We&#8217;ll likely run into a few more caveats with the new beta, but the fixes to previous bugs are always welcome.&nbsp; Hopefully it won&#8217;t be too long of a wait before another release.&nbsp; One thing that surprised me was how long it took to install the service pack, much longer than the install of the .NET Framework 3.5 SP1 beta.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=16&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/05/14/new-entity-framework-designer-bugs-in-sp1-beta/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Partial sprocs?</title>
		<link>http://rhauert.wordpress.com/2008/04/28/partial-sprocs/</link>
		<comments>http://rhauert.wordpress.com/2008/04/28/partial-sprocs/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 14:11:49 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[partial methods]]></category>
		<category><![CDATA[partial sprocs]]></category>
		<category><![CDATA[stored procedures]]></category>
		<category><![CDATA[T4 templates]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/04/28/partial-sprocs/</guid>
		<description><![CDATA[Dave and I have written a T4 template to generate Entity Framework-friendly INSERT, UPDATE, and DELETE stored procedures.&#160; I&#8217;ll detail the template in a future post.&#160; I had the crazy idea of implementing something similar to C#&#8217;s partial methods in SQL, which I named &#8220;partial stored procedures.&#8221;&#160; The reason behind the idea is the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=15&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[</p>
<p><a href="http://blogs.rev-net.com/ddewinter/" target="_blank">Dave</a> and I have written a <a href="http://blogs.rev-net.com/ddewinter/index.php/2008/04/26/entity-framework-stored-procedure-generation/" target="_blank">T4 template</a> to generate Entity Framework-friendly INSERT, UPDATE, and DELETE stored procedures.&nbsp; I&#8217;ll detail the template in a future post.&nbsp; I had the crazy idea of implementing something similar to C#&#8217;s <a href="http://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspx" target="_blank">partial methods</a> in SQL, which I named &#8220;partial stored procedures.&#8221;&nbsp; The reason behind the idea is the same reason partial methods exist: a code generator (a T4 template in our case) generates the same code every time, overwriting any changes someone might have made to the generated code.&nbsp; The Entity Framework and LINQ to SQL designers create partial methods in the generated code to provide points where a developer can execute code, such as before and after a property changes.&nbsp; It&#8217;s possible to do something similar when generating stored procedures, but the performance hit might be discouraging.&nbsp; Here&#8217;s an example of what the code generator might create:</p>
<blockquote><p>CREATE PROCEDURE [dbo].[Person_Insert] <br />&nbsp;&nbsp;&nbsp; @name varchar(50)<br />AS<br />BEGIN<br />&nbsp;&nbsp;&nbsp; IF OBJECT_ID(&#8216;[dbo].[usp_BeforePerson_Insert]&#8216;) IS NOT NULL<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXEC [dbo].[usp_BeforePerson_Insert] @name</p>
<p>&nbsp;&nbsp;&nbsp; INSERT INTO [Test] VALUES (@name)<br />&nbsp;&nbsp;&nbsp; SELECT SCOPE_IDENTITY()</p>
<p>&nbsp;&nbsp;&nbsp; IF OBJECT_ID(&#8216;[dbo].[usp_AfterPerson_Insert]&#8216;) IS NOT NULL<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXEC [dbo].[usp_AfterPerson_Insert] @name<br />END</p></blockquote>
<p>I did a small amount of testing while I played around with the idea.&nbsp; Inserting 100,000 rows into the two-column table using a regular stored procedure took 31 seconds.&nbsp; After adding the additional SQL, but without creating the &#8220;partial&#8221; stored procedures, 100,000 executions took 35 seconds to complete.&nbsp; When I created the &#8220;before&#8221; stored procedure with a simple SELECT in it, the time rose to 54 seconds.&nbsp; Although it&#8217;s an interesting idea, there isn&#8217;t really any need for something like partial methods in SQL.&nbsp; The same thing can be accomplished by adding INSTEAD OF and AFTER triggers to the table.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=15&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/04/28/partial-sprocs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Strange Behavior with InternalsVisibleTo</title>
		<link>http://rhauert.wordpress.com/2008/04/15/strange-behavior-with-internalsvisibleto/</link>
		<comments>http://rhauert.wordpress.com/2008/04/15/strange-behavior-with-internalsvisibleto/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 15:40:54 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Workflow Foundation]]></category>
		<category><![CDATA[AssemblyKeyFile]]></category>
		<category><![CDATA[AssemblyKeyFileAttribute]]></category>
		<category><![CDATA[compile errors]]></category>
		<category><![CDATA[friend assemblies]]></category>
		<category><![CDATA[InternalsVisibleTo]]></category>
		<category><![CDATA[InternalsVisibleToAttribute]]></category>
		<category><![CDATA[PublicKeyToken]]></category>
		<category><![CDATA[workflow]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/04/15/strange-behavior-with-internalsvisibleto/</guid>
		<description><![CDATA[I ran into an interesting situation the other day while using the InternalsVisibleToAttribute.&#160; Our team is developing three libraries as part of our current project, and all three of them are signed with the same key.&#160; Assembly A makes its internals visible to Assembly B and C.&#160; Assembly A and B build successfully, but Assembly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=14&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I ran into an interesting situation the other day while using the <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx" target="_blank">InternalsVisibleToAttribute</a>.&nbsp; Our team is developing three libraries as part of our current project, and all three of them are signed with the same key.&nbsp; Assembly A makes its internals visible to Assembly B and C.&nbsp; Assembly A and B build successfully, but Assembly C fails with the error: </p>
<blockquote><p>Friend access was granted to &#8216;AssemblyC, PublicKey=0024000004800000&#8230;&#8217;, but the output assembly is named &#8216;AssemblyC, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null&#8217;. Try adding a reference to &#8216;AssemblyC, PublicKey=0024000004800000&#8230;&#8217; or changing the output assembly name to match.</p>
</blockquote>
<p>We obviously can&#8217;t add a reference to Assembly C in Assembly A, as that would cause a circular dependency.&nbsp; The only clue we could get out of that error message is the <strong>PublicKeyToken=null</strong> bit.&nbsp; Why would the PublicKeyToken be null if the assembly is signed?&nbsp; I wanted to confirm that the assembly was signed with <a href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> or sn.exe, but there was no dll to check because the build failed.&nbsp; After commenting out all of the code that accessed the internal parts of Assembly A, we got Assembly C to build and confirmed that it was indeed signed.&nbsp; From the output window, we saw that the &#8220;WorkflowCompilation&#8221; task was executing and failing, and that the &#8220;CoreCompile&#8221; wasn&#8217;t even running.&nbsp; It seems that the compilation fails because Assembly C is not yet signed when the compiler attempts to verify the InternalsVisibleTo attribute on Assembly A.&nbsp; We managed to get the project to build by adding the AssemblyKeyFile attribute to it:</p>
<blockquote><p>[assembly: AssemblyKeyFile("MyKey.snk")]</p>
</blockquote>
<p>This produces the warnings &#8220;Use command line option &#8216;/keyfile&#8217; or appropriate project settings instead of &#8216;AssemblyKeyFile&#8217;&#8221; and &#8220;Option &#8216;keyfile&#8217; overrides attribute &#8216;System.Reflection.AssemblyKeyFileAttribute&#8217; given in a source file or added module&#8221; that we have to ignore.&nbsp; I&#8217;ve found this problem to happen to any workflow assemblies that attempt to access the internals of another assembly.&nbsp; I&#8217;ve submitted a bug report about it <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=338293" target="_blank">here</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=14&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/04/15/strange-behavior-with-internalsvisibleto/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Return Statements inside try Blocks</title>
		<link>http://rhauert.wordpress.com/2008/04/11/return-statements-inside-try-blocks/</link>
		<comments>http://rhauert.wordpress.com/2008/04/11/return-statements-inside-try-blocks/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 22:45:49 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dispose]]></category>
		<category><![CDATA[finally]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[IsNullOrEmpty]]></category>
		<category><![CDATA[try/finally]]></category>
		<category><![CDATA[using]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/04/11/return-statements-inside-try-blocks/</guid>
		<description><![CDATA[I recently wrote an extension method named IsNullOrEmpty() for IEnumerable&#60;&#62; types.&#160; Dave had already written one all IEnumerable types: &#160;&#160;&#160;&#160;&#160;&#160;&#160; public static bool IsNullOrEmpty(this IEnumerable source) &#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (source != null) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IEnumerator enumerator = source.GetEnumerator(); &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return !enumerator.MoveNext(); &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return true; &#160;&#160;&#160;&#160;&#160;&#160;&#160; } This method has been useful [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=13&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently wrote an extension method named IsNullOrEmpty() for IEnumerable&lt;&gt; types.&nbsp; <a href="http://blogs.rev-net.com/ddewinter/" target="_blank">Dave</a> had already written one all IEnumerable types:</p>
<div style="font-size:10pt;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsNullOrEmpty(<span style="color:blue;">this</span> <span style="color:#2b91af;">IEnumerable</span> source)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (source != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IEnumerator</span> enumerator = source.GetEnumerator();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> !enumerator.MoveNext();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:blue;">true</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>This method has been useful countless times, but <a href="http://hikmatbek.wordpress.com/" target="_blank">Bek</a> realized this could leave open database connections, or cause other problems related to not calling Dispose.&nbsp; I added the overload:</p>
<div style="font-size:10pt;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsNullOrEmpty&lt;T&gt;(<span style="color:blue;">this</span> <span style="color:#2b91af;">IEnumerable</span>&lt;T&gt; source)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (source != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">using</span> (<span style="color:#2b91af;">IEnumerator</span>&lt;T&gt; enumerator = source.GetEnumerator())</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> !enumerator.MoveNext();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:blue;">true</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>A question was raised about whether the generated finally block would be executed if the method returned inside the try block.&nbsp; I couldn&#8217;t remember the answer, but a quick test with <a href="http://www.linqpad.net/" target="_blank">LINQPad</a> gave us our answer:</p>
<div style="font-size:10pt;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">try</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">finally</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#a31515;">&#8220;Hello world&#8221;</span>.Dump();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>&#8220;Hello world&#8221; was displayed in the output box.&nbsp; If the finally block weren&#8217;t executed before the method returned, we&#8217;d have to store the return value in a temporary variable and place the return statement after the end of the using/finally block.&nbsp; It&#8217;s one of the small tidbits that are nice to know.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=13&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/04/11/return-statements-inside-try-blocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler</title>
		<link>http://rhauert.wordpress.com/2008/04/04/project-euler/</link>
		<comments>http://rhauert.wordpress.com/2008/04/04/project-euler/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 15:37:58 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQPad]]></category>
		<category><![CDATA[problem solving]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/04/04/project-euler/</guid>
		<description><![CDATA[The other day my team leader, Mark, pointed me to a great site for testing your problem solving skills.&#160; Project Euler contains over 180 mathematical or computer programming related problems that you can solve yourself.&#160; You can create an account and submit your answers to track your progress.&#160; Once you&#8217;ve solved the problem, you gain [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=12&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The other day my team leader, <a href="http://itdilemma.wordpress.com/" target="_blank">Mark</a>, pointed me to a great site for testing your problem solving skills.&nbsp; <a href="http://projecteuler.net/index.php" target="_blank">Project Euler</a> contains over 180 mathematical or computer programming related problems that you can solve yourself.&nbsp; You can create an account and submit your answers to track your progress.&nbsp; Once you&#8217;ve solved the problem, you gain access that problems thread where you can discuss it.&nbsp; I&#8217;ve started from the beginning using C# in <a href="http://www.linqpad.net/" target="_blank">LINQPad</a>.&nbsp; I do one or two each day as a brain exercise before I begin work.&nbsp; I&#8217;m considering creating a new page to house my solutions, beginning with the nine I&#8217;ve solved so far.&nbsp; In any case, check out the site and try your hand at some of the problems.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=12&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/04/04/project-euler/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Reproducing Automatic Presence with the UCC API &#8211; Part 2</title>
		<link>http://rhauert.wordpress.com/2008/04/01/reproducing-automatic-presence-with-the-ucc-api-part-2/</link>
		<comments>http://rhauert.wordpress.com/2008/04/01/reproducing-automatic-presence-with-the-ucc-api-part-2/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 22:47:51 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[P/Invoke]]></category>
		<category><![CDATA[UCC]]></category>
		<category><![CDATA[handles]]></category>
		<category><![CDATA[presence]]></category>
		<category><![CDATA[windows message loop]]></category>
		<category><![CDATA[WtsApi32.dll]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/04/01/reproducing-automatic-presence-with-the-ucc-api-part-2/</guid>
		<description><![CDATA[My last post detailed how to monitor user activity and update the user&#8217;s state when they become inactive.&#160; With that code, we can switch between Available, Inactive, and Away states based on user input via the keyboard or mouse.&#160; The next step is to notify OCS when the user locks their machine, by changing the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=8&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://rhauert.wordpress.com/2008/03/27/reproducing-automatic-presence-with-the-ucc-api-part-1/">last post</a> detailed how to monitor user activity and update the user&#8217;s state when they become inactive.&nbsp; With that code, we can switch between <strong>Available</strong>, <strong>Inactive</strong>, and <strong>Away</strong> states based on user input via the keyboard or mouse.&nbsp; The next step is to notify OCS when the user locks their machine, by changing the user&#8217;s availability to <strong>Away</strong>.&nbsp; To do this, we&#8217;re going to need to use P/Invoke again, this time calling the <a href="http://msdn2.microsoft.com/en-us/library/aa383841.aspx" target="_blank">WTSRegisterSessionNotification</a> function, part of <strong>WtsApi32.dll</strong>.&nbsp; This will only work on a Windows XP or newer OS.&nbsp; This function takes a handle to a window we own, which is then notified of session changes by the WM_WTSSESSION_CHANGE message.&nbsp; To receive that notification we need a class that can hook into the windows message loop, something derived from System.Windows.Forms.Control, which I added as a private nested class in the PresenceManager:</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Registers for session changes for this session by calling </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> WTSRegisterSessionNotification.&nbsp; Provides events that </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> notify when the machine has been locked/unlocked.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">sealed</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">SessionChangeHandler</span> : <span style="color:#2b91af;">Control</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport(<span style="color:#a31515;">"WtsApi32.dll"</span>, SetLastError = <span style="color:blue;">true</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:blue;">return</span>: MarshalAs(<span style="color:#2b91af;">UnmanagedType</span>.Bool)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">bool</span> WTSRegisterSessionNotification(</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IntPtr</span> hWnd, [MarshalAs(<span style="color:#2b91af;">UnmanagedType</span>.U4)]<span style="color:blue;">int</span> dwFlags);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport(<span style="color:#a31515;">"WtsApi32.dll"</span>, SetLastError = <span style="color:blue;">true</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:blue;">return</span>: <span style="color:#2b91af;">MarshalAs</span>(<span style="color:#2b91af;">UnmanagedType</span>.Bool)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">bool</span> WTSUnRegisterSessionNotification(<span style="color:#2b91af;">IntPtr</span> hWnd);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> NOTIFY_FOR_THIS_SESSION = 0;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> WM_WTSSESSION_CHANGE = 0x2b1;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> WTS_SESSION_LOCK = 0&#215;7;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> WTS_SESSION_UNLOCK = 0&#215;8;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Raised when the machine has been locked.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">EventHandler</span> MachineLocked;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Raised when the machine has been unlocked.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">EventHandler</span> MachineUnlocked;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> SessionChangeHandler()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (!WTSRegisterSessionNotification(<span style="color:blue;">this</span>.Handle, NOTIFY_FOR_THIS_SESSION))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Marshal</span>.ThrowExceptionForHR(<span style="color:#2b91af;">Marshal</span>.GetLastWin32Error());</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> WndProc(<span style="color:blue;">ref</span> <span style="color:#2b91af;">Message</span> m)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (m.Msg == WM_WTSSESSION_CHANGE)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> value = m.WParam.ToInt32();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (value == WTS_SESSION_LOCK)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnMachineLocked(<span style="color:#2b91af;">EventArgs</span>.Empty);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> <span style="color:blue;">if</span> (value == WTS_SESSION_UNLOCK)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnMachineUnlocked(<span style="color:#2b91af;">EventArgs</span>.Empty);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">base</span>.WndProc(<span style="color:blue;">ref</span> m);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> OnHandleDestroyed(<span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// unregister this instance before it&#8217;s destroyed</span></p>
</div>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (!WTSUnRegisterSessionNotification(<span style="color:blue;">this</span>.Handle))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Marshal</span>.ThrowExceptionForHR(<span style="color:#2b91af;">Marshal</span>.GetLastWin32Error());</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">base</span>.OnHandleDestroyed(e);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> OnMachineLocked(<span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">EventHandler</span> temp = MachineLocked;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (temp != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp(<span style="color:blue;">this</span>, e);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> OnMachineUnlocked(<span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">EventHandler</span> temp = MachineUnlocked;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (temp != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp(<span style="color:blue;">this</span>, e);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
</div>
<p style="margin:0;">Surprisingly, WTSRegisterSessionNotification was not yet documented at <a href="http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification" target="_blank">pinvoke.net</a>, so I added a page for it.&nbsp; The SessionChangeHandler registers itself when it is constructing, and makes a corresponding call to WTSUnRegisterSessionNotification just before the handle is destroyed.&nbsp; Any time the machine is locked or unlocked, the appropiate event is fired, which the PresenceManager can easily handle:</p>
<p style="margin:0;">&nbsp;</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> MachineLocked(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// disable the timer and notify OCS that the user is away</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myLastInputTimer.Change(<span style="color:#2b91af;">Timeout</span>.Infinite, <span style="color:#2b91af;">Timeout</span>.Infinite);</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.Away;</p>
</div>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> MachineUnlocked(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// change the machine&#8217;s state back to available and re-enable the timer</span></p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.Away;</p>
</div>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myLastInputTimer.Change(2000, 2000);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>We can use the same code to publish the presence state to OCS as we did in the timer callback, which I&#8217;ll cover in part three.&nbsp; If you&#8217;re not sure it worked, adding a few calls to Debug.WriteLine can help.&nbsp; At this point, the PresenceManager handles availability the same way we are used to seeing from other IM clients.&nbsp; It just needs to be instantiated and kept somewhere, and it will handle automatic presence until disposed.&nbsp; In the last part of this series I&#8217;ll cover publishing presence state to OCS, and the issues I encountered with the UCC API.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=8&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/04/01/reproducing-automatic-presence-with-the-ucc-api-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Reproducing Automatic Presence with the UCC API &#8211; Part 1</title>
		<link>http://rhauert.wordpress.com/2008/03/27/reproducing-automatic-presence-with-the-ucc-api-part-1/</link>
		<comments>http://rhauert.wordpress.com/2008/03/27/reproducing-automatic-presence-with-the-ucc-api-part-1/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 16:57:22 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[P/Invoke]]></category>
		<category><![CDATA[UCC]]></category>
		<category><![CDATA[communicator]]></category>
		<category><![CDATA[GetLastInputInfo]]></category>
		<category><![CDATA[presence]]></category>
		<category><![CDATA[timers]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/03/27/reproducing-automatic-presence-with-the-ucc-api-part-1/</guid>
		<description><![CDATA[Most popular IM clients, including Communicator 2007, automatically modify the user&#8217;s status when they are inactive or away.&#160; While piloting Office Communications Server we created a simple application that had the main features of Communicator (chat, A/V, presence, etc.) to show that we could incorporate that functionality into our in-house applications so that our agents [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=7&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Most popular IM clients, including Communicator 2007, automatically modify the user&#8217;s status when they are inactive or away.&nbsp; While piloting Office Communications Server we created a simple application that had the main features of Communicator (chat, A/V, presence, etc.) to show that we could incorporate that functionality into our in-house applications so that our agents only have to work with one piece of software.&nbsp; The presence functionality is important to us, as we don&#8217;t want to route inbound calls from <a href="http://www.microsoft.com/speech/speech2007/default.mspx" target="_blank">Speech Server</a> to an agent who isn&#8217;t available.&nbsp; As part of the pilot, I created a simple presence manager to monitor user activity and update availability when necessary.&nbsp; After five minutes of inactivity, a state value of <strong>Inactive</strong> is published.&nbsp; After 15 minutes, a state value of <strong>Away</strong> is published.&nbsp; Finally, if the user locks the machine, a state value of <strong>Away</strong> is published.</p>
<p>Basically, there are two different things the manager needs to detect: the last time there was input (keyboard/mouse) to the machine, and when Windows is locked.&nbsp; After a little digging, I found the easiest way to achieve our first goal was to P/Invoke the <a href="http://msdn2.microsoft.com/en-us/library/ms646302.aspx" target="_blank">GetLastInputInfo</a> <a href="http://pinvoke.net/default.aspx/user32.GetLastInputInfo" target="_blank">function</a>, which returns the time of the last keyboard/mouse activity.&nbsp; Using a timer, we can call this method as often as necessary to check if our thresholds (five and 15 minutes) have been reached.&nbsp; For the sake of simplicity, I removed the code that publishes the state to OCS.&nbsp; We could also easily add logic to check if the user is on the phone, so the &#8220;In a Call&#8221; state isn&#8217;t overwritten.&nbsp; </p>
<p>NOTE: I&#8217;m using the System.Threading.Timer class.</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">internal</span> <span style="color:blue;">sealed</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">PresenceManager</span> : <span style="color:#2b91af;">IDisposable</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">"user32.dll"</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">bool</span> GetLastInputInfo(<span style="color:blue;">ref</span> <span style="color:#2b91af;">LASTINPUTINFO</span> plii);</p>
<p style="margin:0;">&nbsp;</p>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:#2b91af;">Timer</span> myLastInputTimer;</p>
<p style="margin:0;">&nbsp;</p>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> PresenceManager()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { </p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// initialize the timer to call back every two seconds</span></p>
</div>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myLastInputTimer = <span style="color:blue;">new</span> <span style="color:#2b91af;">Timer</span>(CheckIdleTime, <span style="color:blue;">null</span>, 2000, 2000);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p style="margin:0;">&nbsp;</p>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Gets the current availability of the signed-in user.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:#2b91af;">Availability</span> CurrentAvailability { <span style="color:blue;">get</span>; <span style="color:blue;">private</span> <span style="color:blue;">set</span>; } </p>
<p style="margin:0;">&nbsp;</p>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Callback method for </span><span style="color:gray;">&lt;see cref=&#8221;myLastInputTimer&#8221;/&gt;</span><span style="color:green;"> to check the idle time </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> of the computer and update the status if needed.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;param name=&#8221;state&#8221;&gt;</span><span style="color:green;">The state, unused by this method.</span><span style="color:gray;">&lt;/param&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> CheckIdleTime(<span style="color:blue;">object</span> state)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">LASTINPUTINFO</span> lastInputInfo = <span style="color:#2b91af;">LASTINPUTINFO</span>.Create();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (GetLastInputInfo(<span style="color:blue;">ref</span> lastInputInfo))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// get the idle time by subtracting the tick count of the last input </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// event from the total tick count of the system</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> idleTicks = <span style="color:#2b91af;">Environment</span>.TickCount &#8211; lastInputInfo.dwTime;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">TimeSpan</span> idleTime = <span style="color:#2b91af;">TimeSpan</span>.FromSeconds(idleTicks / 1000.0);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Availability</span> currentAvailability = <span style="color:blue;">this</span>.CurrentAvailability;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (idleTime &lt; <span style="color:#2b91af;">TimeSpan</span>.FromMinutes(5))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (currentAvailability != <span style="color:#2b91af;">Availability</span>.Available)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// publish available state to OCS here&#8230;</span></p>
<p style="margin:0;"><span style="color:green;"></span>&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.Available;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> <span style="color:blue;">if</span> (idleTime &lt; <span style="color:#2b91af;">TimeSpan</span>.FromMinutes(15))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (currentAvailability != <span style="color:#2b91af;">Availability</span>.AvailableIdle</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; currentAvailability != <span style="color:#2b91af;">Availability</span>.BusyIdle)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (currentAvailability == <span style="color:#2b91af;">Availability</span>.Busy)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// publish busy-idle state to OCS here&#8230;</span></p>
<p style="margin:0;"><span style="color:green;"></span>&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.BusyIdle;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// publish inactive state to OCS here&#8230;</span></p>
<p style="margin:0;"><span style="color:green;"></span>&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.AvailableIdle;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> <span style="color:blue;">if</span> (<span style="color:blue;">c</span>urrentAvailability != <span style="color:#2b91af;">Availability</span>.Away)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// publish away state to OCS here&#8230;</span></p>
<p style="margin:0;"><span style="color:green;"></span>&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.CurrentAvailability = <span style="color:#2b91af;">Availability</span>.Away;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
</div>
<p style="margin:0;">Now we just need to define the <span style="color:#2b91af;">LASTINPUTINFO </span>structure that we pass in GetLastInputInfo.&nbsp; I nested it inside the manager:</p>
<p style="margin:0;">&nbsp;</p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Structure used by the </span><span style="color:gray;">&lt;see cref=&#8221;GetLastInputInfo&#8221;/&gt;</span><span style="color:green;"> method.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">StructLayout</span>(<span style="color:#2b91af;">LayoutKind</span>.Sequential)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">struct</span> <span style="color:#2b91af;">LASTINPUTINFO</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">readonly</span> <span style="color:blue;">int</span> size = <span style="color:#2b91af;">Marshal</span>.SizeOf(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">LASTINPUTINFO</span>));</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Creates a new </span><span style="color:gray;">&lt;see cref=&#8221;LASTINPUTINFO&#8221;/&gt;</span><span style="color:green;"> initialized and ready to </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> be used by the </span><span style="color:gray;">&lt;see cref=&#8221;GetLastInputInfo&#8221;/&gt;</span><span style="color:green;"> method.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;returns&gt;&lt;/returns&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">LASTINPUTINFO</span> Create()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">LASTINPUTINFO</span> lii = <span style="color:blue;">new</span> <span style="color:#2b91af;">LASTINPUTINFO</span>();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lii.cbSize = size;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> lii;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> The size of this instance, required by the </span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">/// </span><span style="color:gray;">&lt;see cref=&#8221;GetLastInputInfo&#8221;/&gt;</span><span style="color:green;"> method.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">MarshalAs</span>(<span style="color:#2b91af;">UnmanagedType</span>.U4)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">int</span> cbSize;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> The system tick-count at the last recorded user input, set</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> by the </span><span style="color:gray;">&lt;see cref=&#8221;GetLastInputInfo&#8221;/&gt;</span><span style="color:green;"> method.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">MarshalAs</span>(<span style="color:#2b91af;">UnmanagedType</span>.U4)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">int</span> dwTime;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
</div>
<p>GetLastInputInfo requires the cbSize field to be set before the method is called, so I added a Create method to return an instance ready to be uses.&nbsp; The timer calls back on a thread retrieved from the thread pool, but we can publish to OCS from any thread.&nbsp; I found that two seconds was a good interval that updated the state to <strong>Available</strong> in a reasonable time.&nbsp; In the second post I&#8217;ll cover detecting when the machine is locked/unlocked using another P/Invoke operation. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=7&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/03/27/reproducing-automatic-presence-with-the-ucc-api-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing event subscriptions in UCC</title>
		<link>http://rhauert.wordpress.com/2008/03/24/managing-event-subscriptions-in-ucc/</link>
		<comments>http://rhauert.wordpress.com/2008/03/24/managing-event-subscriptions-in-ucc/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 20:02:55 +0000</pubDate>
		<dc:creator>Ryan Hauert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[UCC]]></category>
		<category><![CDATA[Advise]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[PowerCollections]]></category>

		<guid isPermaLink="false">http://rhauert.wordpress.com/2008/03/24/managing-event-subscriptions-in-ucc/</guid>
		<description><![CDATA[The event model when interacting with COM interfaces is different from the .NET event model.&#160; It is more similar to the Observer Pattern used in Java with &#8220;listeners&#8221; than delegates in .NET.&#160; To listen for events raised by objects in the UCC API, you must advise the object of your implemented event &#8220;sink&#8221;.&#160; The event [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=6&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn2.microsoft.com/en-us/library/bb664392.aspx" target="_blank">event model</a> when interacting with COM interfaces is different from the .NET event model.&nbsp; It is more similar to the Observer Pattern used in Java with &#8220;listeners&#8221; than delegates in .NET.&nbsp; To listen for events raised by objects in the UCC API, you must <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.comtypes.iconnectionpoint.advise.aspx" target="_blank">advise</a> the object of your implemented event &#8220;sink&#8221;.&nbsp; The event handler methods are defined in the various interfaces in the UCC API that begin with an underscore, such as _IUccPlatformEvents.&nbsp; </p>
<p>When you call Advise, you get a &#8220;cookie&#8221; that is used to call Unadvise at a later time.&nbsp; Unadvise should always be called to prevent memory leaks, but what do we do with all the cookies in the meantime?&nbsp; The documentation has a <a href="http://msdn2.microsoft.com/en-us/library/bb804156.aspx" target="_blank">good example</a> of registering for UCC events, but it uses a custom two-dimensional Dictionary2D, defined at the bottom of the sample, which basically uses a Dictionary of Dictionaries.&nbsp; The outer dictionary stores an inner dictionary for each type of _IUccXXEvents interface subscribed to, and the inner dictionary stores the cookie for each object that raises the events listed in that interface.</p>
<p>While using a Dictionary2D is certainly one way to go, I decided I wanted a more lightweight cookie jar.&nbsp; I took advantage of the <a href="http://www.wintellect.com/powercollections/docs/Wintellect.PowerCollections.Pair%602.html" target="_blank">Pair</a> structure defined in Wintellect&#8217;s <a href="http://www.codeplex.com/PowerCollections" target="_blank">PowerCollections</a> library to store the cookies in a single Dictionary. </p>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt;, <span style="color:blue;">int</span>&gt; cookies = <span style="color:blue;">new</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt;, <span style="color:blue;">int</span>&gt;();</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> Advises the specified </span><span style="color:gray;">&lt;paramref name=&#8221;source&#8221;/&gt;</span><span style="color:green;"> that the specified</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;paramref name=&#8221;sink&#8221;/&gt;</span><span style="color:green;"> is a registered event sink for events</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> defined in </span><span style="color:gray;">&lt;typeparamref name=&#8221;T&#8221;/&gt;</span><span style="color:green;">.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;typeparam name=&#8221;T&#8221;&gt;</span><span style="color:green;">The interface defining the event handlers that</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> are implemented by the </span><span style="color:gray;">&lt;paramref name=&#8221;sink&#8221;/&gt;&lt;/typeparam&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;param name=&#8221;source&#8221;&gt;</span><span style="color:green;">The source of the events.</span><span style="color:gray;">&lt;/param&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;param name=&#8221;sink&#8221;&gt;</span><span style="color:green;">The event sink.</span><span style="color:gray;">&lt;/param&gt;</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Advise&lt;T&gt;(<span style="color:blue;">object</span> source, T sink)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt; key = <span style="color:blue;">new</span> <span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt;(<span style="color:blue;">typeof</span>(T), source);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// Prevent the same sink from advising on a certain object/type combination more than once.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// Otherwise, our event handler will be called multiple times, which can cause problems.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (!cookies.ContainsKey(key))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IConnectionPointContainer</span> container = (<span style="color:#2b91af;">IConnectionPointContainer</span>)source;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IConnectionPoint</span> cp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> cookie;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Guid</span> guid = <span style="color:blue;">typeof</span>(T).GUID;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.FindConnectionPoint(<span style="color:blue;">ref</span> guid, <span style="color:blue;">out</span> cp);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cp.Advise(sink, <span style="color:blue;">out</span> cookie);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cookies.Add(key, cookie);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Unadvise&lt;T&gt;(<span style="color:blue;">object</span> source)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// get the cached cookie for the source</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt; key = <span style="color:blue;">new</span> <span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:blue;">object</span>&gt;(<span style="color:blue;">typeof</span>(T), source);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> cookie;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (cookies.TryGetValue(key, <span style="color:blue;">out</span> cookie))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IConnectionPointContainer</span> container = (<span style="color:#2b91af;">IConnectionPointContainer</span>)source;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IConnectionPoint</span> cp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Guid</span> guid = <span style="color:blue;">typeof</span>(T).GUID;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.FindConnectionPoint(<span style="color:blue;">ref</span> guid, <span style="color:blue;">out</span> cp);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cp.Unadvise(cookie);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">// remove the cookie from the cached cookies</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cookies.Remove(key);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">This dictionary uses the combination of the type of interface and the source of the event as its key.&nbsp; We could also throw an exception if the key already exists, rather than simply doing nothing.&nbsp; Adding synchronization is the last step we have to do.&nbsp; It&#8217;s simple enough that I won&#8217;t cover it here.&nbsp; When we&#8217;re done advising for the event is simple: <span style="color:#2b91af;">Utilities</span>.Advise&lt;<span style="color:#2b91af;">_IUccSessionEvents</span>&gt;(uccSession, <span style="color:blue;">this</span>);</p>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:consolas, courier new;">
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;</p>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rhauert.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rhauert.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rhauert.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rhauert.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rhauert.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rhauert.wordpress.com&amp;blog=3217641&amp;post=6&amp;subd=rhauert&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rhauert.wordpress.com/2008/03/24/managing-event-subscriptions-in-ucc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5835b0d761ec1a4beadd4c3597d075d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rhauert</media:title>
		</media:content>
	</item>
	</channel>
</rss>
