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

<channel>
	<title>Dan Rigsby - Coding Up Style &#187; Wcf</title>
	<atom:link href="http://www.danrigsby.com/blog/index.php/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danrigsby.com/blog</link>
	<description>Developer.Speaker.Blogger</description>
	<lastBuildDate>Tue, 17 Nov 2009 18:29:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New Webcast: REST Support in WCF</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 13:55:00 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[Wcf]]></category>
		<category><![CDATA[Webcast]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/</guid>
		<description><![CDATA[ I have a new 10 min 12 sec training webcast up over adding REST support to a WCF application.&#160; This video builds on top of the Understanding REST video.&#160; If you have a decent understanding of REST, you don’t necessarily need to watch that video first, but in it I define the 4 tenets [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/screencast1thumb.png"><img title="screencast1-thumb" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 0px 5px; border-left: 0px; border-bottom: 0px" height="128" alt="screencast1-thumb" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/screencast1thumb-thumb.png" width="128" align="right" border="0" /></a> I have a new 10 min 12 sec training webcast up over adding REST support to a WCF application.&#160; This video builds on top of the <a href="http://www.danrigsby.com/blog/index.php/2008/09/11/new-webcast-understanding-rest/">Understanding REST video</a>.&#160; If you have a decent understanding of REST, you don’t necessarily need to watch that video first, but in it I define the 4 tenets of REST which this video applies to WCF. This video also complements a recent presentation I have been giving called “<a href="http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/">RESTing on the Web with WCF</a>”.</p>
<p>So what are you waiting for?&#160; Head on over and check out the webcast!</p>
<p>The video available through <a href="http://www.jupitermedia.com">JupiterMedia</a> and can be viewed at <a href="http://www.internet.com/video/">http://www.internet.com/video/</a>. Just look for the &quot;Developer Video&quot; titled &quot;REST Support in WCF&quot;.</p>
<p>Direct links are:    <br /><a title="http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1815668056" href="http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1815668056">http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1815668056</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/&amp;bgcolor=FF8000&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/09/25/new-webcast-rest-support-in-wcf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using IContextChannel Extensions to Store Custom Data</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 20:04:00 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/</guid>
		<description><![CDATA[Wcf offers numerous extensibility points.&#160; The best example of this is how the REST extensions were added in .Net 3.5.&#160; Nothing was rewritten.&#160; The entire Web Programming Model was built on the Wcf extensibility points and provided as an additional library that could be included in your projects(System.ServiceModel.Web).&#160; We have looked at a couple of [...]]]></description>
			<content:encoded><![CDATA[<p>Wcf offers numerous extensibility points.&#160; The best example of this is how the REST extensions were added in .Net 3.5.&#160; Nothing was rewritten.&#160; The entire Web Programming Model was built on the Wcf extensibility points and provided as an additional library that could be included in your projects(System.ServiceModel.Web).&#160; We have looked at a couple of the extensions points on this blog in the past (<a href="http://www.danrigsby.com/blog/index.php/2008/08/20/observableservicehost-an-instancecontext-creation-aware-wcf-servicehost/">ObservableServiceHost</a> &amp; <a href="http://www.danrigsby.com/blog/index.php/2008/05/24/extending-wcf-instancecontext-to-store-custom-state/">Extending InstanceContext</a>). Another extension point is an <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.icontextchannel.aspx">IContextChannel</a> Extension. This type of extension can be used for many things.&#160; Like any other <a href="http://msdn.microsoft.com/en-us/library/ms586703.aspx">IExtension</a>, there is an Attach method and Detach methods that are called when working with the target object.&#160; In our case, the target object is an IContextChannel.&#160; You can use this to “extend” the incoming channel and “attach” information to it. This extended data isn’t available to the clients and is stored on the server only.&#160; You could use this to store a variety of data. </p>
<p>In our example, we are going to store a custom connection Id on the channel in the service.&#160; Usually you can uniquely identify a client by the SessionId, but occasionally you may want to use your own custom Id or just store some additional information on the channel.</p>
<h2>Creating an IContextChannel Extension</h2>
<h3>ConnectionIdExtension</h3>
<p>The extension itself is very straight forward.&#160; We can either store the ConnectionId in the object before it is attached to the channel, or if the ConnectionId is null, then a new one will be created when it is attached to the Channel.</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ConnectionIdExtension : IExtension&lt;IContextChannel&gt;
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> m_ConnectionId = String.Empty;

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ConnectionId
    {
        get
        {
            <span style="color: #0000ff">return</span> m_ConnectionId;
        }
        set
        {
            m_ConnectionId = <span style="color: #0000ff">value</span>;
        }
    }

    <span style="color: #0000ff">public</span> ConnectionIdExtension()
    {
    }

    <span style="color: #0000ff">public</span> ConnectionIdExtension(
        <span style="color: #0000ff">string</span> connectionId)
    {
        m_ConnectionId = connectionId;
    }

    <span style="color: #cc6633">#region</span> IExtension&lt;IContextChannel&gt; Members
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Attach(IContextChannel owner)
    {
        <span style="color: #008000">// If there is no connectionId, create one</span>
        <span style="color: #0000ff">if</span> (String.IsNullOrEmpty(m_ConnectionId))
        {
            m_ConnectionId = Guid.NewGuid().ToString();
        }
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Detach(IContextChannel owner)
    {
    }
    <span style="color: #cc6633">#endregion</span> IExtension&lt;IContextChannel&gt; Members
}</pre>
</div>
<h3>Attaching ConnectionIdExtension in an Operation</h3>
<p>For our example service, we just have a method that returns the ConnectionId.&#160; It firsts checks the extensions collection on the channel to see if there is a ConnectionIdExtension applied. If there isn’t, a new one is created and added to the extensions collection.</p>
<h3></h3>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyService : IMyService
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> GetConnectionId()
    {
        IContextChannel channel = OperationContext.Current.Channel;

        ConnectionIdExtension connectionIdExtension =
            channel.Extensions.Find&lt;ConnectionIdExtension&gt;();

        <span style="color: #0000ff">if</span> (connectionIdExtension == <span style="color: #0000ff">null</span>)
        {
            connectionIdExtension = <span style="color: #0000ff">new</span> ConnectionIdExtension();
            channel.Extensions.Add(connectionIdExtension);
        }

        <span style="color: #0000ff">return</span> connectionIdExtension.ConnectionId;
    }
}</pre>
</div>
<h3>Calling the Service and Observing the Behavior</h3>
<p>If you use the WcfTestClient to test this method, you should see the same ConnectionId being used for each and every call from the client.&#160; However, if you select the <a href="http://www.danrigsby.com/blog/index.php/2008/08/21/wcftestclient-change-in-visual-studio-2008-sp1/">“Start a new proxy” option</a> on the client, then the Id will be different with each call.&#160; This option tells the client to create a new proxy instance with each call, so there is a new connection being used.</p>
<p><a href="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/context.png"><img title="context" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="368" alt="context" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/context-thumb.png" width="473" border="0" /></a> </p>
<p>You can download a complete sample of the code discussed in this article here: <a href="http://www.danrigsby.com/Files/Rigsby.ChannelContext.zip">http://www.danrigsby.com/Files/Rigsby.ChannelContext.zip</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/&amp;bgcolor=FF8000&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/09/21/using-icontextchannel-extensions-to-store-custom-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RESTing on the Web with Wcf</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 20:51:48 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/</guid>
		<description><![CDATA[I had the opportunity to speak the Kentucky Day of .Net yesterday. I gave my Agile Project Management with Scrum talk and got to premier my newest presentation on REST entitled RESTing on the Web with Wcf.&#160; Both talks went very well.&#160; The REST presentation was a blast to do.&#160; I got a number of [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to speak the <a href="http://kydayof.net/default.aspx">Kentucky Day of .Net</a> yesterday. I gave my <a href="http://www.danrigsby.com/files/Presentations/AgileMgmtWithScrum.zip">Agile Project Management with Scrum</a> talk and got to premier my newest presentation on REST entitled <a href="http://www.danrigsby.com/files/Presentations/RestingOnTheWeb.zip">RESTing on the Web with Wcf</a>.&#160; Both talks went very well.&#160; The REST presentation was a blast to do.&#160; I got a number of excellent comments afterwards, and I think it may be my new favorite talk to give.&#160; I want to give a “Thank You” to the organizers of such a great event,&#160; and I want to thank all of the people who made it out. If you attended any of my talks, please pass on any feedback about what I could do better or what I did right.&#160; I am always looking for ways to improve and to help convey the material to others. And, if you are interested in REST, know a lot about it, or are giving other presentations on REST, I would appreciate any comments you may have on my slides.</p>
<p>To catch any of my future talks, you can always check out my speaking engagements here: <a title="Speaking Engagements" href="http://www.danrigsby.com/blog/index.php/speakings/">Speaking Engagements</a>. I am currently promoting 4 main talks this season (September – November). If you are interested in hearing any of these, <strong><em>please</em></strong> send me an email, talk to your user group leaders, or comment on this thread.&#160; I enjoy every opportunity to speak, share, and talk with my peers. <em>I do still have materials for a session on Heroes {Community} Launch (Visual Studio 2008 and .Net 3.5), if there is any interest in this still.</em></p>
<table cellspacing="0" cellpadding="2" width="100%" border="1">
<tbody>
<tr>
<td valign="top" width="30%"><a href="http://www.danrigsby.com/files/Presentations/RestingOnTheWeb.zip">RESTing on the Web with Wcf</a></td>
<td valign="top" width="70%">.Net 3.5 ushered in a new Web Programming Model for Wcf which extended the library to support REST, Json, and Syndication.&#160; This session will dive into what this new model has to offer and how you can use it to extend the reach and scalability of your applications.</td>
</tr>
<tr>
<td valign="top" width="30%">Duplexing WCF in the Enterprise (Don’t call us, we’ll call you!)</td>
<td valign="top" width="70%">Duplexing in Wcf is the act of having the service call back to its clients.&#160; This topic rarely gets touched on, but is crucial for complex enterprise applications.&#160; This session explains duplexing and introduces the Publish/Subscribe server model as an example of how this can be used in an enterprise level application.</td>
</tr>
<tr>
<td valign="top" width="30%"><a href="http://www.danrigsby.com/files/Presentations/IntroductionToWcf.zip">Introduction to Wcf</a></td>
<td valign="top" width="70%">Windows Communication Foundation is Microsoft’s &quot;next-generation&quot; programming platform and runtime system for building, configuring and deploying network-distributed services. It was initially released with .Net 3.0, but there are many people who have yet to dive into this technology. This is an introduction session that will guide you through the basics of Wcf, why you should move to Wcf, and how you can move your existing applications to Wcf.</td>
</tr>
<tr>
<td valign="top" width="30%"><a href="http://www.danrigsby.com/files/Presentations/AgileMgmtWithScrum.zip">Agile Project Management with Scrum</a></td>
<td valign="top" width="70%">An introduction to Agile Project Management with Scrum. We will go over the Agile Manifesto and dive into the core principles that make up Scrum and how it can be used to help your development processes.</td>
</tr>
</tbody>
</table>
<p><em>Expect new presentations for Winter (December – February).&#160; I am anticipating upcoming talks on the following: <a href="http://jquery.com/">JQuery</a>, <a href="http://en.wikipedia.org/wiki/Windows_Live_Core">Live Mesh API</a>, <a href="http://www.microsoft.com/soa/products/oslo.aspx">Oslo</a>, and <a href="http://www.danrigsby.com/blog/index.php/2008/08/18/microsoft-pdc-2008/">PDC 2008</a>.</em></p>
<p><em></em></p>
<p><a href="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/dsc03064.jpg"><img title="DSC03064" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 5px 0px 0px; border-right-width: 0px" height="184" alt="DSC03064" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/09/dsc03064-thumb.jpg" width="244" align="left" border="0" /></a>As a <em>treat</em>, here is a picture of “RESTifarian Dan the Pig” taken at the closing ceremony of <a href="http://kydayof.net/default.aspx">Kentucky Day of .Net</a>.&#160; I didn’t wear both of these items at the same time during my talks, but those who attended would understand each of these props. </p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/&amp;bgcolor=FF8000&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/09/07/resting-on-the-web-with-wcf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Async Operations in Wcf: Handling Exceptions</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 18:59:00 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=411</guid>
		<description><![CDATA[Async Operations in Wcf Series:       Part 1: Event Based Model       Part 2: IAsyncResult Model (Client-Side)       Part 3: IAsyncResult Model (Server-Side)       Part 4: Canceling Operations       Part [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Async Operations in Wcf Series:</strong>       <br />Part 1: <a href="http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model/">Event Based Model</a>       <br />Part 2: <a href="http://www.danrigsby.com/blog/index.php/2008/03/20/async-operations-in-wcf-iasyncresult-model-client-side/">IAsyncResult Model (Client-Side)</a>       <br />Part 3: <a href="http://www.danrigsby.com/blog/index.php/2008/03/26/async-operations-in-wcf-iasyncresult-model-server-side/">IAsyncResult Model (Server-Side)</a>       <br />Part 4: <a href="http://www.danrigsby.com/blog/index.php/2008/03/30/async-operations-in-wcf-canceling-operations/">Canceling Operations</a>       <br />Part 5: <a href="http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/">Handling Exceptions</a></p>
</blockquote>
<p><em>This is the fifth in a five part series on </em><a href="http://msdn2.microsoft.com/en-us/library/ms734701.aspx"><em>asynchronous operations in Wcf</em></a><em>.&#160; If you are new to this series, you might want to read the first section of </em><a href="http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model/"><em>part one</em></a><em> to get an introduction to this post: </em><a href="http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model/"><em>http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model/</em></a></p>
<h2>What happens when there is an exception</h2>
<p>We have covered the ins and outs of working with asynchronous operations.&#160; But what happens in the unexpected happens?&#160; What happens if an <a href="http://msdn.microsoft.com/en-us/library/system.exception.aspx">Exception</a> or <a href="http://msdn.microsoft.com/en-us/library/ms576199.aspx">FaultException</a> occurs on the server? There are basically two situations:</p>
<ol>
<li>The exception is thrown before the thread is queued or before the thread context switch has happened.&#160; This would still be on the users’s initiated thread. </li>
<li>The exception is thrown during the actual asynchronous operation on a different thread. This would be on some internal thread in the WCF service. </li>
</ol>
<p>In situation 1, this acts like any other service call.&#160; Since the exception occurred on the same thread the client initiated, it is simply propagated back to the client like any other exception.</p>
<p>In situation 2, the exception occurs on a separate thread, so the client isn’t directly notified. Instead the server caches the exception and signals the client that the operation is complete.&#160; The client in turn will call its end operation completion method which will throw the exception back to the client.&#160; Keep in mind though, that if the exception is fatal to the service and beyond recovery, it can crash the service process itself.</p>
<h2>Exception example</h2>
<p>An example of this can be shown with a simple service that calculates a square root.&#160; In the call on the server, we have it wait 20 seconds before returning the result.&#160; However, if we throw an exception before the wait period, the client will get signaled immediately.&#160; </p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> GetSquareRoot(<span style="color: #0000ff">double</span> <span style="color: #0000ff">value</span>)
{
    <span style="color: #008000">// Throwing exception here causes the server to signal to </span>
    <span style="color: #008000">// the client that the operation is complete and the exception</span>
    <span style="color: #008000">// is rethrown on the client</span>
    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> FaultException(<span style="color: #006080">&quot;Fault before queuing thread.&quot;</span>);

    Thread.Sleep(20000); <span style="color: #008000">// Wait 20 seconds</span>

    <span style="color: #0000ff">return</span> Math.Sqrt(<span style="color: #0000ff">value</span>);
}</pre>
</div>
<div>&#160;</div>
</p>
</p>
</p>
</p>
</p>
</p>
<p><img title="AsyncException" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="317" alt="AsyncException" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/08/asyncexception.png" width="675" border="0" /> </p>
<p>You can download this full example here: <a href="http://www.danrigsby.com/Files/Rigsby.WcfAsyncOperationExceptions.zip">http://www.danrigsby.com/Files/Rigsby.WcfAsyncOperationExceptions.zip</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/&amp;bgcolor=FF8000&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/28/async-operations-in-wcf-handling-exceptions/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Changing the default Clock Skew in WCF</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/26/changing-the-default-clock-skew-in-wcf/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/26/changing-the-default-clock-skew-in-wcf/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 18:01:12 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=394</guid>
		<description><![CDATA[It is possible that if you use a WCF binding that has a security binding element, you may encounter the the following exception:
The security timestamp is invalid because its creation time (&#8216;8/262008 1:45:51 PM&#8217;) is in the future. Current time is &#8216;8/26/2006 1:40:01 PM&#8217; and allowed clock skew is &#8216;00:05:00&#8242;.
The exception is of type TimeStampHasCreationTimeInFuture [...]]]></description>
			<content:encoded><![CDATA[<p>It is possible that if you use a WCF binding that has a security binding element, you may encounter the the following exception:</p>
<blockquote><p>The security timestamp is invalid because its creation time (&#8216;8/262008 1:45:51 PM&#8217;) is in the future. Current time is &#8216;8/26/2006 1:40:01 PM&#8217; and allowed clock skew is &#8216;00:05:00&#8242;.</p></blockquote>
<p><em>The exception is of type </em><a href="http://msdn.microsoft.com/en-us/library/aa738641.aspx"><em>TimeStampHasCreationTimeInFuture</em></a><em> which is defined as: “The security timestamp is invalid because its creation time is in the future. Current time is specified and allowed clock skew is specified.”</em></p>
<p>This occurs when the difference of the client/server clocks is larger than the allowed value (the default is 5 minutes).  There two possible solutions to this issue:</p>
<ol>
<li>Set the clocks on the client and server such that they are more in sync.  (You can use “net time” to help you accomplish this.)</li>
<li>Increase the allows clock skew time on the binding.</li>
</ol>
<p>Option one is by far the easiest, but isn’t always possible depending on your network nor is to the most bulletproof since other clients could have the same issue. Option two isn’t as easy as it sounds though.  There is no setting you can change on your current bindings to set the max clock skew.  Instead, you need to resort to a <a href="http://msdn.microsoft.com/en-us/library/aa347793.aspx">custom binding</a>.</p>
<p>Here is an example of a custom binding created in a configuration file with the max clock skew set to 15 minutes instead of the default 5 minutes:</p>
<div>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">system.serviceModel</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">services</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">service</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MyService"</span> <span style="color: #ff0000;">behaviorConfiguration</span><span style="color: #0000ff;">="MyBehavior"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">endpoint</span> <span style="color: #ff0000;">address</span> <span style="color: #0000ff;">=""</span> <span style="color: #ff0000;">binding</span><span style="color: #0000ff;">="MyCustomBinding"</span> <span style="color: #ff0000;">contract</span><span style="color: #0000ff;">="<strong>IMyService</strong>"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">service</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">services</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">behaviors</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">serviceBehaviors</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">behavior</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MyBehavior"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">serviceMetadata</span> <span style="color: #ff0000;">httpGetEnabled</span><span style="color: #0000ff;">="True"</span><span style="color: #0000ff;">/&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">behavior</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">serviceBehaviors</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">behaviors</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bindings</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">customBinding</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">binding</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="MyCustomBinding"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">security</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">localClientSettings</span> <span style="color: #ff0000;">maxClockSkew</span><span style="color: #0000ff;">="00:15:00"</span> <span style="color: #0000ff;">/&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">localServiceSettings</span> <span style="color: #ff0000;">maxClockSkew</span><span style="color: #0000ff;">="00:15:00"</span> <span style="color: #0000ff;">/&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">secureConversationBootstrap</span> <span style="color: #0000ff;">/&gt;</span>
                <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">security</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">textMessageEncoding</span> <span style="color: #0000ff;">/&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">httpTransport</span> <span style="color: #0000ff;">/&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">binding</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">customBinding</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bindings</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">system.serviceModel</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<div> </div>
<div>You can do the same in code.  However you can build a custom binding by extending any existing binding.</div>
<div> </div>
<div>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;">// Create custom binding by extending any existing binding.</span>
System.ServiceModel.Channels.CustomBinding myCustomBinding =
    <span style="color: #0000ff;">new</span> System.ServiceModel.Channels.CustomBinding(binding);

<span style="color: #008000;">// Find the security binding element</span>
System.ServiceModel.Channels.SecurityBindingElement security =
    myCustomBinding.Elements.Find&lt;SecurityBindingElement&gt;();

<span style="color: #008000;">// Change the clock skew for service and client</span>
<span style="color: #0000ff;">if</span> (security != <span style="color: #0000ff;">null</span>)
{
    security.LocalServiceSettings.MaxClockSkew = TimeSpan.FromMinutes(15);
    security.LocalClientSettings.MaxClockSkew = TimeSpan.FromMinutes(15);
}</pre>
</div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.danrigsby.com/blog/?p=394"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.danrigsby.com/blog/?p=394&amp;bgcolor=FF8000&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" border="0" alt="DotNetKicks Image" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/26/changing-the-default-clock-skew-in-wcf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WcfTestClient Change in Visual Studio 2008 SP1</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/21/wcftestclient-change-in-visual-studio-2008-sp1/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/21/wcftestclient-change-in-visual-studio-2008-sp1/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 01:01:28 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/08/21/wcftestclient-change-in-visual-studio-2008-sp1/</guid>
		<description><![CDATA[In the previous version of the WcfTestClient, every call to the server would result in a new instance of the proxy being created.&#160; So, if you wanted to do any testing based on sessions, you had to use another tool.&#160; In SP1, Microsoft has updated the WcfTestClient with a check box named “Start a new [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous version of the <a href="http://msdn.microsoft.com/en-us/library/bb552364.aspx">WcfTestClient</a>, every call to the server would result in a <a href="http://msdn.microsoft.com/en-us/magazine/cc163289.aspx">new instance of the proxy being created</a>.&#160; So, if you wanted to do any testing based on sessions, you had to use another tool.&#160; In SP1, Microsoft has updated the WcfTestClient with a check box named “Start a new proxy” which defines whether or not to create a new proxy client with that call.&#160; The default value is to reuse the same proxy instance.</p>
<p><a href="http://www.danrigsby.com/blog/wp-content/uploads/2008/08/wcftestclient2.png"><img title="wcftestclient2" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="317" alt="wcftestclient2" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/08/wcftestclient2-thumb.png" width="644" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/21/wcftestclient-change-in-visual-studio-2008-sp1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ObservableServiceHost &#8211; An InstanceContext creation aware WCF ServiceHost</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/20/observableservicehost-an-instancecontext-creation-aware-wcf-servicehost/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/20/observableservicehost-an-instancecontext-creation-aware-wcf-servicehost/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 13:30:00 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=322</guid>
		<description><![CDATA[

On the MSDN Forums for WCF recently, an enhancement request was proposed to add an event to the ServiceHost class that is fired when a new instance of the service is created.&#160; The proposed event would look something like this:
&#160;

ServiceHost host = new ServiceHost(typeof(MyService));
host.Open();
host.InstanceCreated += new EventHandler&#60;InstanceEventArgs&#62;(host_InstanceCreated);

&#160;
It was decided that this may not be the [...]]]></description>
			<content:encoded><![CDATA[</p>
<h2></h2>
<div>On the <a href="http://forums.microsoft.com/Forums/ShowForum.aspx?ForumID=118&amp;SiteID=1">MSDN Forums for WCF</a> recently, an enhancement request <a href="http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3763134&amp;SiteID=1">was proposed</a> to add an event to the <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx">ServiceHost</a> class that is fired when a new instance of the service is created.&#160; The proposed event would look something like this:</div>
<div>&#160;</div>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">ServiceHost host = <span style="color: #0000ff">new</span> ServiceHost(<span style="color: #0000ff">typeof</span>(MyService));
host.Open();
host.InstanceCreated += <span style="color: #0000ff">new</span> EventHandler&lt;InstanceEventArgs&gt;(host_InstanceCreated);</pre>
</div>
<div>&#160;</div>
<div>It was decided that this may not be the best enhancement request since this could be done today with the extension mechanisms built into WCF.&#160; The usefulness of the event is also dependent on the type of <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">InstanceContextMode</a> used:</div>
<ol>
<li><strong>PerCall</strong>: Every call made to the endpoints would create a new service instance.&#160; This could be useful. </li>
<li><strong>PerSession</strong>: Every new client connection would create a new service instance for the life of the client’s session. This could be very useful. </li>
<li><strong>Single</strong>: A service instance is created during the first call.&#160; This is fairly useless since it is called when the single instance of the service starts, but not again. </li>
</ol>
<p>However, I accepted the challenge to come up with an implementation of this extension.&#160; What I came up with is called ObservableServiceHost.&#160; This is a custom ServiceHost that you can use instead of the default <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx">System.ServiceModel.ServiceHost</a>.&#160; The solution requires only 4 files:</p>
<h2>1. InstanceEventArgs</h2>
<p>A simple <a href="http://msdn.microsoft.com/en-us/library/system.eventargs.aspx">EventArgs</a> class is needed as a container for the <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">InstanceContext</a>.</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> InstanceEventArgs : EventArgs
{
    <span style="color: #0000ff">public</span> InstanceContext InstanceContext
    {
        get;
        set;
    }

    <span style="color: #0000ff">public</span> InstanceEventArgs()
    {
    }

    <span style="color: #0000ff">public</span> InstanceEventArgs(
        InstanceContext instanceContext)
    {
        InstanceContext = instanceContext;
    }
}</pre>
</div>
<div>&#160;</div>
<h2>2. ObservableServiceHost</h2>
<p>ObservableServiceHost extends the functionality of a <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx">ServiceHost</a>.&#160; This class consists of the following:</p>
<ul>
<li>A new event called InstanceCreated which uses the InstanceEventArgs. </li>
<li>Re-implemented constructors of the base <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx">ServiceHost</a> class. </li>
<li>When the ServiceHost is Opened, a custom <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.aspx">IEndpointBehavior</a> is added to each of the endpoints used at the time the service is opened.&#160; Endpoints which implement <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.imetadataexchange.aspx">IMetadataExchange</a> are be ignored though since the event doesn’t need to be fired a new metadata instance is created. <em>(You could potentially modify this to add a different behaviors for different service interfaces</em>.) </li>
</ul>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ObservableServiceHost : ServiceHost
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">event</span> EventHandler&lt;InstanceEventArgs&gt; InstanceCreated;

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">internal</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> OnInstanceCreated(
        InstanceContext instanceContext)
    {
        EventHandler&lt;InstanceEventArgs&gt; handler = InstanceCreated;
        <span style="color: #0000ff">if</span> (handler != <span style="color: #0000ff">null</span>)
        {
            handler(<span style="color: #0000ff">this</span>, <span style="color: #0000ff">new</span> InstanceEventArgs(instanceContext));
        }
    }

    <span style="color: #0000ff">public</span> ObservableServiceHost(
        <span style="color: #0000ff">object</span> singletonInstance,
        <span style="color: #0000ff">params</span> Uri[] baseAddresses) : <span style="color: #0000ff">base</span>(singletonInstance, baseAddresses)
    {
    }

    <span style="color: #0000ff">public</span> ObservableServiceHost(
        Type serviceType,
        <span style="color: #0000ff">params</span> Uri[] baseAddresses) : <span style="color: #0000ff">base</span>(serviceType, baseAddresses)
    {
    }

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnOpening()
    {
        AddInstanceContextInitializers();

        <span style="color: #0000ff">base</span>.OnOpening();
    }

    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> AddInstanceContextInitializers()
    {
        <span style="color: #0000ff">foreach</span> (ServiceEndpoint endpoint <span style="color: #0000ff">in</span> <span style="color: #0000ff">this</span>.Description.Endpoints)
        {
            <span style="color: #0000ff">if</span> (endpoint.Contract.ContractType != <span style="color: #0000ff">typeof</span>(IMetadataExchange))
            {
                endpoint.Behaviors.Add(<span style="color: #0000ff">new</span> InstanceCreationEndpointBehavior(<span style="color: #0000ff">this</span>));
            }
        }
    }
}</pre>
</div>
<h2>3. InstanceCreationInitializer</h2>
<p>This is a basic implementation of <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstancecontextinitializer.aspx">IInstanceContextInitializer</a> which can be used to inspect the <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">InstanceContext</a> when an instance is created.&#160; This class consists of the following:</p>
<ul>
<li>A constructor that takes in a reference to the ObservableServiceHost. </li>
<li>An implementation of <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstancecontextinitializer.initialize.aspx">IInstanceContextInitializer.Initialize</a> which fires the InstanceCreated event on the referenced ObservableServiceHost when the <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">InstanceContext</a> is created. </li>
</ul>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> InstanceCreationInitializer : IInstanceContextInitializer
{
    <span style="color: #0000ff">private</span> ObservableServiceHost m_ObservableServiceHost;

    <span style="color: #cc6633">#region</span> IInstanceContextInitializer Members
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize(
        InstanceContext instanceContext,
        Message message)
    {
        <span style="color: #0000ff">if</span> (m_ObservableServiceHost != <span style="color: #0000ff">null</span>)
        {
            m_ObservableServiceHost.OnInstanceCreated(instanceContext);
        }
    }
    <span style="color: #cc6633">#endregion</span> IInstanceContextInitializer Member

    <span style="color: #0000ff">public</span> InstanceCreationInitializer(
        ObservableServiceHost observableServiceHost)
    {
        m_ObservableServiceHost = observableServiceHost;
    }
}</pre>
</div>
<h2>4. InstanceCreationEndpointBehavior</h2>
<p>This class implements <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.aspx">IEndpointBehavior</a> which is used to extend the runtime behavior of an endpoint and add our InstanceCreationInitializer to the endpoint dispatcher.&#160; It is this behavior that we add to each of the endpoints when the ObservableServiceHost opens. </p>
<p>This class consists of the following:</p>
<ul>
<li>A constructor that takes in a reference to the ObservableServiceHost and creates an instance of the InstanceCreationInitializer. </li>
<li>An implementation of <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.applydispatchbehavior.aspx">IEndpointBehavior.ApplyDispatchBehavior</a><u><font color="#669966"></font></u> which adds our InstanceCreationInitializer to the endpoint dispatchers <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.dispatchruntime.instancecontextinitializers.aspx">InstanceContextInitializers</a> collection.&#160; This will cause our InstanceCreationInitializer to be run when a new instance is created.&#160; The InstanceCreationInitializer&#160; in turn calls our InstanceCreated event on the ObservableServiceHost. </li>
</ul>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> InstanceCreationEndpointBehavior : IEndpointBehavior
{
    <span style="color: #0000ff">private</span> InstanceCreationInitializer m_InstanceCreationInitializer;

    <span style="color: #cc6633">#region</span> IEndpointBehavior Members

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.InstanceContextInitializers.Add(
            m_InstanceCreationInitializer);
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Validate(ServiceEndpoint endpoint)
    {
    }
    <span style="color: #cc6633">#endregion</span> IEndpointBehavior Members

    <span style="color: #0000ff">public</span> InstanceCreationEndpointBehavior(
        ObservableServiceHost observableServiceHost)
    {
        m_InstanceCreationInitializer = <span style="color: #0000ff">new</span> InstanceCreationInitializer(observableServiceHost);
    }

}</pre>
</div>
<h2>Example Service</h2>
<p>Included in the code for this article is an example of how to use this library.&#160; You can take any new or existing service and use it with the ObservableServiceHost, then just attach to the InstanceCreated event:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> Program
{
    <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)
    {
        ObservableServiceHost serviceHost = <span style="color: #0000ff">new</span> ObservableServiceHost(
            <span style="color: #0000ff">typeof</span>(MyService));
        serviceHost.Open();
        serviceHost.InstanceCreated += <span style="color: #0000ff">new</span> EventHandler&lt;InstanceEventArgs&gt;(serviceHost_InstanceCreated);

        Console.WriteLine(<span style="color: #006080">&quot;Press any key to exit application...&quot;</span>);
        Console.WriteLine();
        Console.ReadLine();

        serviceHost.Close();
    }

    <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> serviceHost_InstanceCreated(<span style="color: #0000ff">object</span> sender, InstanceEventArgs e)
    {
        Console.WriteLine(<span style="color: #006080">&quot;New InstanceContext created&quot;</span>);
    }
}</pre>
</div>
<div>&#160;</div>
<div>For this sample, we are just outputting a string every time a new <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">IntanceContext</a> is created (You could have this do more meaningful work). Now use the <a href="http://msdn.microsoft.com/en-us/library/bb552364.aspx">WcfTestClient</a> or any other custom client to hit the endpoint (http://localhost:8080/MyService/).&#160; When operations are run on the service, the “New InstanceContext created” message is written to the console:</div>
<p><img title="TestServer" height="225" alt="TestServer" src="http://www.danrigsby.com/blog/wp-content/uploads/2008/08/testserver.gif" width="429" border="0" /></p>
<p>You can play around with the <a href="http://www.danrigsby.com/blog/index.php/2008/05/23/understanding-instancecontext-in-wcf/">InstanceContextMode</a> on the Service to see what happens when you use it with PerCall, PerSession, or Single.&#160; The line of code to look for is:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]</pre>
</div>
<p>&#160;</p>
<p>I hope others find this class useful or at least have learned more about the extensibility that WCF has to offer. You can download the complete library and sample server here: <a href="http://www.danrigsby.com/Files/Rigsby.ServiceHostCreation.zip">http://www.danrigsby.com/Files/Rigsby.ServiceHostCreation.zip</a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.danrigsby.com%2fblog%2findex.php%2f2008%2f08%2f20%2fobservableservicehost-an-instancecontext-creation-aware-wcf-servicehost%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.danrigsby.com%2fblog%2findex.php%2f2008%2f08%2f20%2fobservableservicehost-an-instancecontext-creation-aware-wcf-servicehost%2f&amp;bgcolor=FF9900" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/20/observableservicehost-an-instancecontext-creation-aware-wcf-servicehost/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What happened to the WCF Extensions for Visual Studio 2005</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/18/what-happened-to-the-wcf-extensions-for-visual-studio-2005/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/18/what-happened-to-the-wcf-extensions-for-visual-studio-2005/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 02:31:54 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/08/18/what-happened-to-the-wcf-extensions-for-visual-studio-2005/</guid>
		<description><![CDATA[Back in the day before Visual Studio 2008 was released, there was a set of extensions you could install on Visual Studio 2005 to give you WPF, WF, and WCF templates, and other tools to work with .Net Framework 3.0. This set of extensions made it up to CTP 3 in November 2006.&#160; However, when [...]]]></description>
			<content:encoded><![CDATA[<p>Back in the day before Visual Studio 2008 was released, there was a set of extensions you could install on Visual Studio 2005 to give you WPF, WF, and WCF templates, and other tools to work with .Net Framework 3.0. This set of extensions made it up to CTP 3 in November 2006.&#160; However, when Visual Studio 2008 came out, these extensions never got any further attention.&#160; So they never made it out of the Community Technology Preview cycle.&#160; Microsoft wants everyone to upgrade to Visual Studio 2008.</p>
<p>You can still get the <em>Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation)</em> from here (for now):&#160; <a title="http://www.microsoft.com/downloads/details.aspx?familyid=5D61409E-1FA3-48CF-8023-E8F38E709BA6&amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?familyid=5D61409E-1FA3-48CF-8023-E8F38E709BA6&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?familyid=5D61409E-1FA3-48CF-8023-E8F38E709BA6&amp;displaylang=en</a></p>
<p>However, the official link for <em>Visual Studio 2005 extensions for.NET Framework 3.0 (WCF &amp; WPF): </em><a title="http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&amp;displaylang=en</a> has been removed for many months.</p>
<p>So why is the link broken?&#160; There have been a <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3695753&amp;SiteID=1">f</a><a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3632776&amp;SiteID=1">e</a><a href="http://www.eggheadcafe.com/software/aspnet/32635041/where-to-get-wcf-for-visu.aspx">w</a> posts on the MSDN Forums asking where these extensions went.&#160; The basic answer is that, according to Microsoft, Visual Studio 2005 was never meant to be used to develop WCF, WF, and WPF.&#160; Visual Studio 2008 is the correct tool to use, and on <strong>June 30th 2008</strong>, <strong>the</strong> <strong>license</strong> <strong>for this CTP</strong> <strong>expired</strong> (see <a href="http://blogs.msdn.com/acangialosi/archive/2008/06/27/vs-2005-extensions-for-net-framework-3-0-wpf-wcf-ctp-is-coming-off-the-ms-download-center.aspx">this post</a>).</p>
<p>While you cant get&#160; <em>Visual Studio 2005 extensions for.NET Framework 3.0 (WCF &amp; WPF) </em>from the official Microsoft site, you can still find it at these locations:</p>
<ul>
<li><a title="http://www.download.com/The-Visual-Studio-2005-extensions-for-NET-Framework-3-0-WCF-WPF-November-2006-CTP/3000-10253_4-10727672.html" href="http://www.download.com/The-Visual-Studio-2005-extensions-for-NET-Framework-3-0-WCF-WPF-November-2006-CTP/3000-10253_4-10727672.html">http://www.download.com/The-Visual-Studio-2005-extensions-for-NET-Framework-3-0-WCF-WPF-November-2006-CTP/3000-10253_4-10727672.html</a> </li>
<li><a href="http://downloads.zdnet.co.uk/0,1000000375,39328336s,00.htm">http://downloads.zdnet.co.uk/0,1000000375,39328336s,00.htm</a> </li>
</ul>
<p>However, due to licensing concerns, these links may go down over time too.&#160; So, if you need these extensions, you will want to grab them soon.&#160; But if you are doing any development in .Net Framework 3.0, you need to move to Visual Studio 2008. </p>
<p>Some companies out there will have issues with this, but you can still use .Net 3.0 technologies in Visual Studio 2005.&#160; It’s just the <em>tools</em> that are no longer supported.</p>
<p> <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.danrigsby.com%2fblog%2findex.php%2f2008%2f08%2f18%2fwhat-happened-to-the-wcf-extensions-for-visual-studio-2005%2f"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.danrigsby.com%2fblog%2findex.php%2f2008%2f08%2f18%2fwhat-happened-to-the-wcf-extensions-for-visual-studio-2005%2f&amp;bgcolor=FF9900" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/18/what-happened-to-the-wcf-extensions-for-visual-studio-2005/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Webcast: Simple WCF Services</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/08/14/new-webcast-simple-wcf-services/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/08/14/new-webcast-simple-wcf-services/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 00:20:21 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[Wcf]]></category>
		<category><![CDATA[Webcast]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/index.php/2008/08/14/new-webcast-simple-wcf-services/</guid>
		<description><![CDATA[I have a new 7 min 3 sec training webcast out over creating WCF services and hosting them in IIS.&#160; This is short, entry level WCF video designed to assist those moving over from webservices or who want a simple start into WCF. 
The video is available through JupiterMedia and can be viewed at http://www.internet.com/video/. [...]]]></description>
			<content:encoded><![CDATA[<p>I have a new 7 min 3 sec training webcast out over creating WCF services and hosting them in IIS.&#160; This is short, entry level WCF video designed to assist those moving over from webservices or who want a simple start into WCF. </p>
<p>The video is available through <a href="http://www.jupitermedia.com/">JupiterMedia</a> and can be viewed at <a href="http://www.internet.com/video/">http://www.internet.com/video/</a>. Just look for the &quot;Developer Video&quot; titled &quot;Simple WCF Services&quot;.&#160; Direct link is:</p>
<p><a title="http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1727929931" href="http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1727929931">http://link.brightcove.com/services/link/bcpid1431564240/bclid1433966034/bctid1727929931</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/08/14/new-webcast-simple-wcf-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>REST and Max URL Size</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/06/17/rest-and-max-url-size/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/06/17/rest-and-max-url-size/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 18:47:00 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=244</guid>
		<description><![CDATA[I have heard many people say that the definition of REST is to just put everything in the URL.&#160; This isn’t true.&#160; One of the tenants of REST is to “embrace” the URL and use it as much as possible, but moving information to POST data is just as important and often necessary.&#160; I can [...]]]></description>
			<content:encoded><![CDATA[<p>I have heard many people say that the definition of REST is to just put everything in the URL.&#160; This isn’t true.&#160; One of the tenants of REST is to “embrace” the URL and use it as much as possible, but moving information to POST data is just as important and often necessary.&#160; I can understand how this can be misunderstood.&#160; Most REST interfaces do take in all values in into the URL to make it easier to navigate the data.&#160; Having to build a POST package can be tedious at times.&#160; So, what if you do want to send larger data to the service?&#160; When should you decide to move away from a pure URL approach and start using POST data in the header?</p>
<p><strong>Browser and WebServer Limits: </strong><em></em><a title="http://www.boutell.com/newfaq/misc/urllength.html" href="http://www.boutell.com/newfaq/misc/urllength.html"><em>http://www.boutell.com/newfaq/misc/urllength.html</em></a></p>
<ul>
<li><strong>Internet Explorer:</strong> <a href="http://support.microsoft.com/kb/208427">2,083 characters</a>, with no more than 2,048 characters in the path portion of the URL </li>
<li><strong>Firefox:</strong> 65,536 characters show up, but longer URLs do still work even up past 100,000 </li>
<li><strong>Safari:</strong> &gt; 80,000 characters </li>
<li><strong>Opera:</strong> &gt; 190,000 characters </li>
<li><strong>IIS:</strong> 16,384 characters, but is configurable </li>
<li><strong>Apache:</strong> 4,000 characters </li>
</ul>
<p>Different things can happen when you go beyond these URL limits depending on the browser and web servers. It could be a “413 (Entity Too Large)” error, “414 (Request-URI Too Long)” error, the URL could be truncated, or it may just work.&#160; However since different things can happen and your service could be consumed by any source, you have to assume the worst could happen.</p>
<p>By looking at just these numbers, it seems you would be pretty safe with a URL that gets up to 2,000 characters.&#160; However, lets look at the W3C standards on this.&#160; The HTTP specification doesn’t really set a size for the length of a URL, but it does recommend being cautious when going above 255. According to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.2">Hypertext Transfer Protocol &#8211; HTTP/1.1 section 3.2</a>:</p>
<blockquote><p>Servers ought to be cautious about depending on URI lengths above 255 bytes, because some older client or proxy implementations might not properly support these lengths.</p>
</blockquote>
<p>So for the sake of a REST interface you <em>should</em> avoid setting up an interface where there is any possibility of a URL getting beyond 255.&#160; If you are using a GET verb to request a resource via the URL you should be pretty safe.&#160; However POSTing or PUTing an insert or update via a URL may not be the way to go unless the data size is very small.</p>
<p>The most important thing is to keep your interfaces <strong>consistent</strong>.&#160; You don’t want one operation that allows an insert via the URL and another through POST data.&#160; This just makes things confusing and less discoverable.&#160; Especially since REST has no way to <a href="http://www.danrigsby.com/blog/index.php/2008/05/29/rest-services-and-metadata-endpoints-in-wcf/">publish metadata</a> about the operations.&#160;&#160; You could follow standards such as these:</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="143"><strong>CRUD Operation</strong></td>
<td valign="top" width="128"><strong>HTTP Verb</strong></td>
<td valign="top" width="127"><strong>Data Location</strong></td>
</tr>
<tr>
<td valign="top" width="143">Create</td>
<td valign="top" width="128">POST or PUT</td>
<td valign="top" width="127">POST Data</td>
</tr>
<tr>
<td valign="top" width="143">Read</td>
<td valign="top" width="128">GET</td>
<td valign="top" width="127">URL</td>
</tr>
<tr>
<td valign="top" width="143">Update</td>
<td valign="top" width="128">POST or PUT</td>
<td valign="top" width="127">POST Data</td>
</tr>
<tr>
<td valign="top" width="143">Delete</td>
<td valign="top" width="129">DELETE</td>
<td valign="top" width="127">URL</td>
</tr>
</tbody>
</table>
<p>In general this follows “<a href="http://lesscode.org/2006/03/19/high-rest/">High REST</a>” which is the more pure REST.&#160; This dictates that the HTTP Verbs define the methods to act on the resource. </p>
<p>Ultimately it’s up to you how you implement the REST interface, but stay consistent, be careful about URL limits, and try to follow industry standards.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/06/17/rest-and-max-url-size/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

