<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET</title>
        <link>http://jamescbender.com/bendersblog/category/4.aspx</link>
        <description>.NET</description>
        <language>en-US</language>
        <copyright>James Bender</copyright>
        <managingEditor>james.c.bender@live.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>Pimp Your WCF Runtime - Episode One</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/05/06/pimp-your-wcf-runtime---episode-one.aspx</link>
            <description>&lt;p&gt;If you have ever made any effort in learning WCF, you have probably seen some variation of this:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        host = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;HelloWCF&lt;/span&gt;), &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost:8080/HelloWCF"&lt;/span&gt;));
        host.Description.Endpoints[0].Behaviors.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"message"&lt;/span&gt;));
        host.Description.Behaviors.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyInstanceProvider&lt;/span&gt;());

        host.Opening += host_Opening;
        host.Opened += host_Opened;
        host.Faulted += host_Faulted;

        host.Open();

        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"Service is available."&lt;/span&gt;);

        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadKey();
    }
    &lt;span style="color: blue"&gt;finally
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(host != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"Closing service..."&lt;/span&gt;);
            host.Close();
        }
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This hosting code, or some variant of it, appears in almost every book on WCF, usually in a console application. It demonstrates creating an instance of ServiceHost for a service implementation (in this case HelloWCF) with a specified URI. A couple of behaviors are added at runtime and we wire up some custom event handlers. All in all, for WCF, nothing complicated.&lt;/p&gt;

&lt;p&gt;But what if you want to host your service in IIS? Since it comes with Windows Activation Service (WAS), IIS7 is an ideal environment to host your WCF service. So how do we get the same ability to customize our runtime in IIS?&lt;/p&gt;

&lt;p&gt;It's actually pretty easy.&lt;/p&gt;

&lt;h2&gt;System.ServiceModel.Activation to the rescue!&lt;/h2&gt;

&lt;p&gt;IIS uses an instance of a ServiceHostFactory to create a runtime for your hosted service. If you do not provide an implementation for this class, IIS will provide you with a default implementation.&lt;/p&gt;

&lt;p&gt;If we want to use are own hosting logic, all we have to do is create our own host class which inherits from ServiceHostFactory like so:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomHost &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ServiceHostFactory
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHostBase &lt;/span&gt;CreateServiceHost(&lt;span style="color: blue"&gt;string &lt;/span&gt;constructorString, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;serviceType = 
            &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"{0}, {1}"&lt;/span&gt;, constructorString,
            constructorString.Substring(0, constructorString.IndexOf(&lt;span style="color: #a31515"&gt;"."&lt;/span&gt;)));

        &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;t =&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.GetType(serviceType);
        
        &lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost&lt;/span&gt;(t, baseAddresses);

        &lt;span style="color: blue"&gt;return &lt;/span&gt;host;
    }

    &lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;CreateServiceHost(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost&lt;/span&gt;(serviceType, baseAddresses);

        &lt;span style="color: blue"&gt;return &lt;/span&gt;host;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;When we do this we need to override both implementations of CreateServiceHost, which is the method IIS calls to create the runtime for you service. For both versions we get a service type and an array of base addresses. The only real difference between the two is that the version that takes the string as the first argument is taking the service type information as a string as opposed to a .NET Type. It's an easy matter to take this string and create an instance of a Type object, in which case we can refactor the code to this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomHost &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ServiceHostFactory
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHostBase &lt;/span&gt;CreateServiceHost(&lt;span style="color: blue"&gt;string &lt;/span&gt;constructorString, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;serviceType = 
            &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"{0}, {1}"&lt;/span&gt;, constructorString,
            constructorString.Substring(0, constructorString.IndexOf(&lt;span style="color: #a31515"&gt;"."&lt;/span&gt;)));

        &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;t =&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.GetType(serviceType);
        
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.CreateServiceHost(t, baseAddresses);
    }

    &lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;CreateServiceHost(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost&lt;/span&gt;(serviceType, baseAddresses);

        &lt;span style="color: blue"&gt;return &lt;/span&gt;host;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I think this is a better way to handle this as you are able to keep all the specialized host creation logic to the second method. For example, lets add the events from the first example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomHost &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ServiceHostFactory
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHostBase &lt;/span&gt;CreateServiceHost(&lt;span style="color: blue"&gt;string &lt;/span&gt;constructorString, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;serviceType = 
            &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"{0}, {1}"&lt;/span&gt;, constructorString,
            constructorString.Substring(0, constructorString.IndexOf(&lt;span style="color: #a31515"&gt;"."&lt;/span&gt;)));

        &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;t =&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.GetType(serviceType);
        
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.CreateServiceHost(t, baseAddresses);
    }

    &lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;CreateServiceHost(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost&lt;/span&gt;(serviceType, baseAddresses);

        host.Opening += host_Opening;
        host.Opened += host_Opened;
        host.Faulted += host_Faulted;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;host;
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;host_Faulted(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        &lt;span style="color: green"&gt;// Some logic...
    &lt;/span&gt;}

    &lt;span style="color: blue"&gt;void &lt;/span&gt;host_Opened(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        &lt;span style="color: green"&gt;// Some logic...
    &lt;/span&gt;}

    &lt;span style="color: blue"&gt;void &lt;/span&gt;host_Opening(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        &lt;span style="color: green"&gt;// Some logic...
    &lt;/span&gt;}
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;See, nothing to it!&lt;/p&gt;

&lt;p&gt;All that's left is hook this up to our SVC file.&lt;/p&gt;

&lt;p&gt;Unfortunately in Visual Studio 2008 it can be a little difficult to edit the SVC file. Double clicking it brings up the code behind file and not the SVC file. To get to that file, right click the file in the Solution Explorer and select "Open with..." Select "XML Editor" from the list and you should see something that looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;%&lt;span style="color: #a31515"&gt;@ &lt;/span&gt;&lt;span style="color: red"&gt;ServiceHost Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;C#&lt;/span&gt;" &lt;span style="color: red"&gt;Debug&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;true&lt;/span&gt;" &lt;span style="color: red"&gt;Service&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CustomHostExample.Service1&lt;/span&gt;" &lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Service1.svc.cs&lt;/span&gt;" %&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To add our service host, we just add a Factory attribute that identifies our service host:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;%&lt;span style="color: #a31515"&gt;@ &lt;/span&gt;&lt;span style="color: red"&gt;ServiceHost Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;C#&lt;/span&gt;" &lt;span style="color: red"&gt;Debug&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;true&lt;/span&gt;" &lt;span style="color: red"&gt;Service&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CustomHostExample.Service1&lt;/span&gt;" 
    &lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Service1.svc.cs&lt;/span&gt;" &lt;span style="color: red"&gt;Factory&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CustomHostExample.MyCustomHost&lt;/span&gt;" %&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Save the SVC file and that's it! You just customized your IIS runtime environment for you WCF service.&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/17.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/05/06/pimp-your-wcf-runtime---episode-one.aspx</guid>
            <pubDate>Wed, 07 May 2008 01:32:09 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/17.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/05/06/pimp-your-wcf-runtime---episode-one.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/17.aspx</wfw:commentRss>
        </item>
        <item>
            <title>It's Your Fault!</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/04/17/its-your-fault.aspx</link>
            <description>&lt;p&gt;In talking to people about WCF, I've noticed that a lot of people, new to WCF and not, have not realized that WCF has the ability to send rich fault information back to the client by using a Fault Contract. It's very easy. In fact, you can do it in four steps...&lt;/p&gt;  &lt;h3&gt;Step 1: Admit your faults&lt;/h3&gt;  &lt;p&gt;The first thing we need to do is determine what kind of information we want to return back to the client and create a data contract that contains that information. You may have several kinds of fault classes, and you should so that you can return specific types of information for specific faults. Kinda like why there isn't just one Exception class in .NET. Although these are data contracts, you should not use them for anything other than sending faults back to the client. Here's a sample fault class:&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;DataContract&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFault
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;private string &lt;/span&gt;_message = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty;

        &lt;span style="color: blue"&gt;public &lt;/span&gt;MyFault(&lt;span style="color: blue"&gt;string &lt;/span&gt;message)
        {
            _message = message;
        }

        [&lt;span style="color: #2b91af"&gt;DataMember&lt;/span&gt;]
        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Message
        {
            &lt;span style="color: blue"&gt;get
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;return &lt;/span&gt;_message;
            }
            &lt;span style="color: blue"&gt;set
            &lt;/span&gt;{
                _message = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
            }
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Like any .NET class, you can create a fault class the inherits from another .NET class, BUT that class also needs to be a data contract and be a known type.&lt;/p&gt;

&lt;h3&gt;Step 2: Find where you may go wrong&lt;/h3&gt;

&lt;p&gt;Next, we need to let WCF know where these faults might be coming from. This is done at the operation in the service contract. You simply use a FaultContract attribute which lets WCF know what kind of possible fault class to expect:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;]
&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IFaultyService
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;]
    [&lt;span style="color: #2b91af"&gt;FaultContract&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MyFault&lt;/span&gt;))]
    &lt;span style="color: blue"&gt;void &lt;/span&gt;ThrowMeAnError();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You can specify as many faults for the operation as you like, simply add more FaultContract attributes:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;]
&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IFaultyService
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;]
    [&lt;span style="color: #2b91af"&gt;FaultContract&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MyFault&lt;/span&gt;))]
    [&lt;span style="color: #2b91af"&gt;FaultContract&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MyOtherFault&lt;/span&gt;))]
    [&lt;span style="color: #2b91af"&gt;FaultContract&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;YetAnotherFault&lt;/span&gt;))]
    &lt;span style="color: blue"&gt;void &lt;/span&gt;ThrowMeAnError();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h3&gt;Step 3: The throw...&lt;/h3&gt;

&lt;p&gt;Throwing faults from a service is a little different than throwing them from C# or VB code. We need to enclose our fault class in a FaultException&amp;lt;T&amp;gt;, which represents a SOAP exception. This is just as easy as the other steps:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FaultException&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyFault&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyFault&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"This is a fault"&lt;/span&gt;), &lt;span style="color: #a31515"&gt;"Demonstration"&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The second argument, in this case "Demonstration" is the reason for the fault. And yes, "reason" is the attribute name of the fault too. This can either be a string or a FaultReason object which allows you to send more detailed information about the exception.&lt;/p&gt;

&lt;h3&gt;Step 4: ... and the catch&lt;/h3&gt;

&lt;p&gt;If you've done everything correctly, when you generate a reference to your service, in addition to the normal proxy information, you should have a class representing the data contract you created for your fault class. Catching this exception on the client side is similar to throwing it on the server side. It will come wrapped in a FaultException&amp;lt;T&amp;gt; and you will need to specify that to catch it correctly:&lt;/p&gt;

&lt;pre class="code"&gt;.
.
.
&lt;span style="color: blue"&gt;catch&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;FaultException&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyFault&lt;/span&gt;&amp;gt; itsMyFault)
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"MyFault was caught"&lt;/span&gt;);
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;@"Message: {0}"&lt;/span&gt;, itsMyFault.Detail.Message));
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;@"Reason: {0}"&lt;/span&gt;, itsMyFault.Reason));
}
.
.
.&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, getting the reason is pretty straight forward. Our message, and any other field me might have added, will be accessible as a member of the Detail child object of our fault.&lt;/p&gt;

&lt;p&gt;That's it. See, nothing to it.&lt;/p&gt;

&lt;p&gt;Actually, this rabbit hole does go a little deeper, but this should be enough to get you all started with capturing rich error information from your services.&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/15.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/04/17/its-your-fault.aspx</guid>
            <pubDate>Thu, 17 Apr 2008 23:52:06 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/15.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/04/17/its-your-fault.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/15.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Extending WCF using the IInstanceProvider and IServiceBehavior Interfaces</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/04/09/extending-wcf-using-the-iinstanceprovider-and-iservicebehavior-interfaces.aspx</link>
            <description>&lt;p&gt;Recently, meaning yesterday, two of my colleagues, &lt;a target="_blank" href="http://krisscott.net/Default.aspx"&gt;Kriss Scott&lt;/a&gt; and &lt;a target="_blank" href="http://programwith.net/"&gt;Matt Casto&lt;/a&gt; ran into an issue with one of WCF services.&lt;/p&gt;
&lt;p&gt;To make a long story short, they needed to do some policy injection, which one or both of them will probably blog about at some point. The difficulty was getting access to the constructor of the WCF service object.&lt;/p&gt;
&lt;p&gt;Luckily the ability to create custom behaviors in WCF makes this relatively easy.&lt;/p&gt;
&lt;p&gt;The first step was to choose what type of functionality the behavior needed to provide. Since we are looking to effect the way the service object is created, IInstanceProvider was the obvious choice. Kriss and Matt also wanted this behavior to effect all calls to this service object, so IServiceBehavior was also to be used.&lt;/p&gt;
&lt;h2&gt;What's different?&lt;/h2&gt;
&lt;p&gt;In my last series of posts on behaviors, I created a behavior based on the IDispatchMessageInspector which, hence the name, attached to (and effected) the behavior of the message dispatcher in the WCF runtime. If you remember from my previous posts, the Dispatchers are responsible for passing messages around the runtime and controlling service runtime attributes (channel dispatcher is connected to the endpoint dispatcher, the endpoint dispatcher is connected to the operation dispatcher, the operation dispatcher is connected to the operation). This was implemented as an IEndpointBehavior which meant that it was applied to a specific endpoint. So, if a message came through the endpoint we applied the behavior to, the behavior logic would execute. If a message come into the same operation of the same service but through a different endpoint the behavior would not execute.&lt;/p&gt;
&lt;p&gt;We "attached" our behavior (in code) thusly...&lt;/p&gt;
&lt;p&gt;In the hosting code, before we opened the host, we added an instance of the behavior to the behavior collection for our endpoint:&lt;/p&gt;
&lt;pre class="code"&gt;host.Description.Endpoints[0].Behaviors.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;"message"&lt;/span&gt;));&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This caused the runtime to call the ApplyDispatchBehavior of our custom behavior object:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="COLOR: #2b91af"&gt;ServiceEndpoint &lt;/span&gt;endpoint, &lt;span style="COLOR: #2b91af"&gt;EndpointDispatcher &lt;/span&gt;endpointDispatcher)
{
    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This was the code actually responsible for adding our custom logic to the correct place in the WCF runtime, in this case the MessageInspectors collection for the EndpointDispatcher passed in from the WCF runtime. If this code wasn't here, the behavior would never have actually been implemented in our service.&lt;/p&gt;
&lt;p&gt;But that was IDispatchMessageInspector and IEndpointBehavior.&lt;/p&gt;
&lt;h2&gt;IInstanceProvider and IServiceBehavior are different animals.&lt;/h2&gt;
&lt;p&gt;For starters, your implementation of IInstanceProvider has to be attached to the endpoint dispatchers instance provider. Unlike the message inspector, there can only be one instance provider per endpoint. &lt;/p&gt;
&lt;p&gt;But there are a couple little "wrinkles" in this whole thing. Remember when I said that Matt and Kriss wanted to implement this at the service level and not the endpoint level? That means they need to use IServiceBehavior, not IEndpointBehavior. Well, like IEndpointBehavior, IServiceBehavior has an implementation of the ApplyDispatchBehavior:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="COLOR: #2b91af"&gt;ServiceDescription &lt;/span&gt;serviceDescription, 
    &lt;span style="COLOR: #2b91af"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase)
{

}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;One thing you may notice is that unlike the endpoint behaviors version of ApplyDispatchBehavior, we get references to ServiceDescription and ServiceHostBase passed in; not ServiceEndpoint or EndpointDispatcher. But, we need to attach this behavior to an endpoint. Here's how we do it:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="COLOR: #2b91af"&gt;ServiceDescription &lt;/span&gt;serviceDescription, 
    &lt;span style="COLOR: #2b91af"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase)
{
    &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;ChannelDispatcherBase &lt;/span&gt;channelDispatcherBase &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;serviceHostBase.ChannelDispatchers)
    {
        &lt;span style="COLOR: #2b91af"&gt;ChannelDispatcher &lt;/span&gt;channelDispatcher = channelDispatcherBase &lt;span style="COLOR: blue"&gt;as &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ChannelDispatcher&lt;/span&gt;;
        &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(channelDispatcher != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)
        {
            &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;EndpointDispatcher &lt;/span&gt;endpoint &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;channelDispatcher.Endpoints)
            {
                endpoint.DispatchRuntime.InstanceProvider = &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;;
            }
        }
    }
}&lt;/pre&gt;
&lt;p&gt;Service host base gives us access to the collection of channel dispatchers, which in turn gives us a collection of endpoint dispatchers. From here it's a simple matter to iterate through each collection and attach the behavior as need.&lt;/p&gt;
&lt;p&gt;This is the easy example. I'm just attaching the behavior to all the endpoints in all the channel dispatchers for this service. If I wanted to I could examine each endpoint and make some kind of determination about what kind of action I wanted to take on that endpoint. In this case we didn't need to.&lt;/p&gt;
&lt;p&gt;From here it's a simple matter to add it to our runtime from code (remember to do this BEFORE you call host.Open()):&lt;/p&gt;
&lt;pre class="code"&gt;host.Description.Behaviors.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;MyInstanceProvider&lt;/span&gt;());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Or from configuration:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;service &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;Sample.HelloWCF&lt;/span&gt;" &lt;span style="COLOR: red"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MyInstanceProviderBehavior&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MyInstanceProviderBehavior&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;MyInstanceProvider&lt;/span&gt;&lt;span style="COLOR: blue"&gt;/&amp;gt;
&lt;/span&gt;&lt;span style="COLOR: blue"&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;extensions&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviorExtensions&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;add &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MyInstanceProvider&lt;/span&gt;"
             &lt;span style="COLOR: red"&gt;type&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;CustomBehaviorSample.InstanceProviderExtensionElement, Behavior, &lt;/span&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;/&amp;gt;
&lt;/span&gt;&lt;span style="COLOR: blue"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviorExtensions&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;extensions&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Remember, to add this behavior via configuration, you'll need a class based on BehaviorExtensionElement to do the "heavy lifting." I covered this &lt;a target="_blank" href="http://jamescbender.com/bendersblog/archive/2008/03/01/making-wcf-quotbehavequot---part-three.aspx"&gt;here&lt;/a&gt; and a bit more &lt;a target="_blank" href="http://jamescbender.com/bendersblog/archive/2008/04/02/making-wcf-quotbehavequot---part-four.aspx"&gt;here&lt;/a&gt;. This time around our implementation is much simpler since we aren't passing any information in at configuration and we are starting with our behavior extension element as a separate class:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;InstanceProviderExtensionElement &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;BehaviorExtensionElement
&lt;/span&gt;{
    &lt;span style="COLOR: blue"&gt;protected override object &lt;/span&gt;CreateBehavior()
    {
        &lt;span style="COLOR: blue"&gt;return new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;MyInstanceProvider&lt;/span&gt;();
    }

    &lt;span style="COLOR: blue"&gt;public override &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;Type &lt;/span&gt;BehaviorType
    {
        &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return typeof &lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;MyInstanceProvider&lt;/span&gt;); }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The last step is to implement the methods from IInstanceProvider:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public object &lt;/span&gt;GetInstance(&lt;span style="COLOR: #2b91af"&gt;InstanceContext &lt;/span&gt;instanceContext)
 {
     &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;GetInstance(instanceContext, &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;);
 }

 &lt;span style="COLOR: blue"&gt;public object &lt;/span&gt;GetInstance(&lt;span style="COLOR: #2b91af"&gt;InstanceContext &lt;/span&gt;instanceContext, &lt;span style="COLOR: #2b91af"&gt;Message &lt;/span&gt;message)
 {
     &lt;span style="COLOR: blue"&gt;return new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;HelloWCF&lt;/span&gt;();
 }

 &lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;ReleaseInstance(&lt;span style="COLOR: #2b91af"&gt;InstanceContext &lt;/span&gt;instanceContext, &lt;span style="COLOR: blue"&gt;object &lt;/span&gt;instance)
 {
     &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;
 }&lt;/pre&gt;
&lt;p&gt;As you see, this is relatively trivial. We have two overrides of the GetInstance method; one that takes a message (in case we want to inspect the message before deciding what kind of object to return) and one that doesn't. We also have a ReleaseInstance method to allow us to perform any additional cleanup that is needed.&lt;/p&gt;
&lt;p&gt;So, that's all there is too it! If you have any questions feel free to leave a comment or ping me on twitter.&lt;/p&gt;
&lt;p&gt;Later!&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/14.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/04/09/extending-wcf-using-the-iinstanceprovider-and-iservicebehavior-interfaces.aspx</guid>
            <pubDate>Wed, 09 Apr 2008 15:15:39 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/14.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/04/09/extending-wcf-using-the-iinstanceprovider-and-iservicebehavior-interfaces.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/14.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Making WCF &amp;quot;Behave&amp;quot; - Part Four</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/04/02/making-wcf-quotbehavequot---part-four.aspx</link>
            <description>&lt;p&gt;Over the past month or so I've demonstrated how to create a custom behavior for WCF. I explained how behaviors fit into the WCF stack, created one based on the Message Dispatcher and then added it to the service via the configuration file.&lt;/p&gt;  &lt;p&gt;The last piece is enabling our behavior to take property information from the configuration file.&lt;/p&gt;  &lt;p&gt;Currently, our behavior intercepts the output message a replaces it with some text that has been hard coded into the behavior class. This is great, but how often do we hard code data like that in the code and never have to change it? That's right; almost never.&lt;/p&gt;  &lt;p&gt;The plan then is to make some alteration so our behavior so that it can accept the message to insert from our configuration file:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;CustomBehaviorElement &lt;/span&gt;&lt;span style="color: red"&gt;customMessage&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;This is a test message&lt;/span&gt;"&lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In &lt;a href="http://jamescbender.com/bendersblog/archive/2008/03/01/making-wcf-quotbehavequot---part-three.aspx" target="_blank"&gt;part three&lt;/a&gt; I changed our class to implement the BehaviorExtensionElement so that it could be added to the service runtime via configuration. I did this for the sake of simplicity, but in general this is not a good idea. You will want to keep the behavior logic separate from the logic that allows it to be managed via the configuration. You'll see why in a few minutes.&lt;/p&gt;

&lt;p&gt;This also gives me an opportunity to back up a little and explain what BehaviorExtensionElement does. Last time when we set up the behavior to be added via configuration, we changed the class to inherit from BehaviorExtensionElement and we referenced the behavior in the configuration like such (remember, the "type" information has to be on one line, no breaks please):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CustomBehaviorElement&lt;/span&gt;" 
    &lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CustomBehaviorSample.MyCustomMessageFormatter, Behavior, 
        Version=1.0.0.0, 
        Culture=neutral, 
        PublicKeyToken=null&lt;/span&gt;" &lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The WCF runtime looked in the behavior assembly for a class that derives from BehaviorExtensionElement named MyCustomMessageFormatter. This trick is, it doesn't necessarily expect that class to be the behavior. It merely expects the class to know how to create the behavior.&lt;/p&gt;

&lt;p&gt;This abstraction presents us with some interesting possibilities. For example, with the behavior code in a separate class we have the ability to influence how the behavior is created in different circumstances by creating different classes that implement BehaviorExtensionElement.&lt;/p&gt;

&lt;p&gt;Some key benefits of this separation of duties is the ability to pass runtime information to the behavior via the configuration and to control how the actual behavior class is created.&lt;/p&gt;

&lt;p&gt;The first step in making our change is to add a member variable to hold the message we are going to be sending. Simply stated....&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private string &lt;/span&gt;_message;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The next step is creating  (or if you already have one, changing) the constructor for the behavior class (MyCustomMessageFormatter) to take a string from the config, which just so happens to be our message:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;MyCustomMessageFormatter(&lt;span style="color: blue"&gt;string &lt;/span&gt;serviceMessage)
            : &lt;span style="color: blue"&gt;base&lt;/span&gt;()
        {
            &lt;span style="color: blue"&gt;this&lt;/span&gt;._message = serviceMessage;
        }&lt;/pre&gt;

&lt;p&gt;You can have any initialization code in this constructor you want. Need four different parameters for your constructor? Not a problem. Want to make some database calls to perform initialization? You can do that too. Want to delegate to some other class or service? Just write the code.&lt;/p&gt;

&lt;p&gt;A change also needs to be made to the actual code that inserts the new message. Instead of using the hard coded message, it will now use the value that is being passed in to the constructor:&lt;/p&gt;

&lt;pre class="code"&gt;xmlDictionary.WriteString(&lt;span style="color: blue"&gt;this&lt;/span&gt;._message);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now we have to make a new class that derives from BehaviorExtensionElement. We'll go ahead and call this BehaviorExtensionElementSample. Once we've done that we need to change the behavior class (MyCustomMessageFormatter) and remove it's inheritance reference to this class. In doing this, we will need to move the BehaviorType property and the CreateBehavior method we previously created in out behavior to the new class:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BehaviorExtensionElementSample &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;BehaviorExtensionElement
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;BehaviorType
    {
        &lt;span style="color: blue"&gt;get 
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;);
        }
    }

    &lt;span style="color: blue"&gt;protected override object &lt;/span&gt;CreateBehavior()
    {
        &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.CustomMessage);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, a change was also made to the CreateBehavior method that calls the constructor of the Behavior class. It's going to pass in a string that will be the message text from the config file. But it doesn't even need to do that. You have a lot of flexibility at this point in what kind of behavior gets created. For example, you could use the incoming configuration information to determine what kind of behavior to create (Strategy pattern anyone?) including not creating a behavior at all.&lt;/p&gt;

&lt;p&gt;Again, the WCF runtime doesn't necessarily expect the class that inherits from BehaviorExtensionElement to provide the behavior logic, but it does expect it to be able to handle the creation of the behavior.&lt;/p&gt;

&lt;p&gt;So now that we know how the information gets from BehaviorExtensionElement to our behavior class, how does the information get from the config file to the BehaviorExtensionElement?&lt;/p&gt;

&lt;p&gt;To answer that, we need to learn a little bit about some of the classes in the System.Configuration namespace. Specifically ConfigurationProperty and ConfigurationPropertyCollection.&lt;/p&gt;

&lt;p&gt;To our BehaviorExtensionElementSample, we add the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ConfigurationPropertyCollection &lt;/span&gt;_prop = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

&lt;span style="color: blue"&gt;protected override &lt;/span&gt;System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationPropertyCollection &lt;/span&gt;Properties
{
    &lt;span style="color: blue"&gt;get
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;._prop == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: blue"&gt;this&lt;/span&gt;._prop = &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationPropertyCollection&lt;/span&gt;();
            &lt;span style="color: blue"&gt;this&lt;/span&gt;._prop.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"customMessage"&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;), 
                &lt;span style="color: #a31515"&gt;"Default Message"&lt;/span&gt;, System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationPropertyOptions&lt;/span&gt;.IsRequired));
        }
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;._prop;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;A ConfigurationProperty is an abstraction of configuration information, like the kind we have in our config files. It stores the value along with various meta-data like type, default value, type conversion rules and a description of the property. The ConfigurationProperyCollection is (surprise!) a collection of these ConfigurationProperty objects. By adding this code, we are overriding the implementation in ConfigurationElement, which BehaviorExtensionElement inherits from.&lt;/p&gt;

&lt;p&gt;So, in the code above our class derived from BehaviorExtensionElement creates a private instance of the ConfigurationPropertyCollection and exposes a protected property called "Properties." Within our override of "Properties" we need to create ConfigurationProperty elements for out custom attributes. We only have one, the "customMessage" so that's all we are creating here. A base class will call this property on our implementation, and over write it's instance of the ConfigurationProperyCollection with ours.&lt;/p&gt;

&lt;p&gt;Now that we've got to get our custom message in and out of our BehaviorExtensionElement. We add this code to our BehaviorExtensionElementSample:&lt;/p&gt;

&lt;pre class="code"&gt;[System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"customMessage"&lt;/span&gt;, DefaultValue = &lt;span style="color: #a31515"&gt;"Default message."&lt;/span&gt;,
    IsRequired = &lt;span style="color: blue"&gt;true&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public string &lt;/span&gt;CustomMessage
{
    &lt;span style="color: blue"&gt;get
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)&lt;span style="color: blue"&gt;base&lt;/span&gt;[&lt;span style="color: #a31515"&gt;"customMessage"&lt;/span&gt;];               
    }
    &lt;span style="color: blue"&gt;set
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;base&lt;/span&gt;[&lt;span style="color: #a31515"&gt;"customMessage"&lt;/span&gt;] = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The first thing you'll notice is the ConfigurationProperty attribute. This mirrors the information we used to create our ConfigurationProperty element for our ConfigurationPropertyCollection above. You'll notice the name "customMessage" is the same identifier that we used to identify our value in the config file. This is very important because it tells the WCF runtime how to map the information from the config file to a property on our class. When the runtime collects the configuration information from the config file and creates an instance of our extension element, it will populate all of our properties automatically for us.&lt;/p&gt;

&lt;p&gt;The rest of this is pretty simple "store a value, return a value" type code; storing the incoming value in the ConfigurationPropertyCollection we created ("Properties" is our base classes default indexer property) and returning a value (in our case, cast to a string).&lt;/p&gt;

&lt;p&gt;So, to tie all this fun configuration stuff back into our behavior, the code that actually creates the behavior class:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected override object &lt;/span&gt;CreateBehavior()
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.CustomMessage);            
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Passes in our message to our behavior:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private string &lt;/span&gt;_message;

&lt;span style="color: blue"&gt;public &lt;/span&gt;MyCustomMessageFormatter(&lt;span style="color: blue"&gt;string &lt;/span&gt;serviceMessage)
    : &lt;span style="color: blue"&gt;base&lt;/span&gt;()
{
    &lt;span style="color: blue"&gt;this&lt;/span&gt;._message = serviceMessage;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Which in turn, injects it into our outgoing message:&lt;/p&gt;

&lt;pre class="code"&gt;xmlDictionary.WriteString(&lt;span style="color: blue"&gt;this&lt;/span&gt;._message);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;See! It was, uh... easy? :)&lt;/p&gt;

&lt;p&gt;In reality, this may seem complicated for something as simple as passing an initialization value into a class. But when you think about it, WCF is a communications framework whose mission to support flexibility and extensibility.  The leveraging of the ConfigurationElement and ConfigurationProperty classes allows us to leverage existing functionality in the .NET framework and the separation of duties between our behavior code and the BehaviorExtensionElement class gives us complete control over how things happen in the runtime. At any point along the way we had the ability to examine what is going on and customize our functionality based on any number of conditions. As complex as this may seem (and once you do a few, it's actually not too tough) I promise it's easier than trying to achieve this same level of configurable customization in ASMX or .NET Remoting!&lt;/p&gt;

&lt;p&gt;Over the next few months I'll post some content on how to create/use the other types of behaviors. For now feel free to download and play with the code.&lt;/p&gt;
&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-de4aebf19948423f.skydrive.live.com/embedrowdetail.aspx/Public/Code%20Samples/WCF%20Custom%20Behavior/CustomBehaviorSample.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;

&lt;p&gt;See ya later!&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/12.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/04/02/making-wcf-quotbehavequot---part-four.aspx</guid>
            <pubDate>Wed, 02 Apr 2008 17:27:57 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/12.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/04/02/making-wcf-quotbehavequot---part-four.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/12.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Making WCF &amp;quot;Behave&amp;quot; - Part Three</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/03/01/making-wcf-quotbehavequot---part-three.aspx</link>
            <description>&lt;p&gt;When last we left our weary adventures, they had created a custom WCF Behavior based on the IDispatchMessageInspector that when attached to an endpoint of a WCF service intercepted the outgoing message and replaced it with it's own nefarious text (&lt;a href="http://benders-blog.blogspot.com/2008/02/making-wcf-part-two.html"&gt;see here&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Their plan was brilliant! Brilliant that is, except for one minor detail.&lt;/p&gt;
&lt;p&gt;You see, in their haste our motley crew had simply attached the behavior to the endpoint at runtime when the service host was initiated. They quickly realized that the real power of WCF is the ability to change behaviors via configuration. This would require a little more work...&lt;/p&gt;
&lt;p&gt;Now, back to our story...&lt;/p&gt;
&lt;p&gt;In order to be able to add our custom behavior through configuration, we need to make sure the WCF Service Model knows it exists. We do this by having our behavior inherit from System.ServiceModel.Configuration.BeahviorExtensionElement. This gives us an abstract property called "BehaviorType" which tells the service model the type of our custom Behavior, and an abstract method "CreateBehavior" which actually returns an instance of our custom behavior. &lt;/p&gt;
&lt;p&gt;For this example I've chosen to implement this in my actual custom behavior class. And yes, if that was the way you do it all the time the service model should be smart enough to get the type and call a default constructor on our custom behavior. But the reason for this base class, and the methods it exposes, is that you could implement all of this in a class other than your actual behavior. This allows a lot of power in the way that custom behaviors are created, and I will cover strategies for this in another post.&lt;/p&gt;
&lt;p&gt;Anyway...&lt;/p&gt;
&lt;p&gt;If we add this to the custom behavior from last time, we get:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;MyCustomMessageFormatter &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;BehaviorExtensionElement&lt;/span&gt;,
    &lt;span style="COLOR: #2b91af"&gt;IDispatchMessageInspector&lt;/span&gt;, 
    &lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: #2b91af"&gt;IEndpointBehavior
&lt;/span&gt;{
    &lt;span style="COLOR: blue"&gt;...&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&lt;/span&gt;&lt;font size="4"&gt;    &lt;span style="COLOR: blue"&gt;public override &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;Type &lt;/span&gt;BehaviorType
    {
        &lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;get 
        &lt;/span&gt;{ 
            &lt;span style="COLOR: blue"&gt;return typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;);
        }
    }

    &lt;span style="COLOR: blue"&gt;protected override object &lt;/span&gt;CreateBehavior()
    {
        &lt;span style="COLOR: blue"&gt;return new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;MyCustomMessageFormatter&lt;/span&gt;();
    }
}    &lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;The next step is to let your application know about the behavior in the configuration file. This is done by adding the and "extensions" node to the configuration:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;extensions&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviorExtensions&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;add &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;CustomBehaviorElement&lt;/span&gt;"
             &lt;span style="COLOR: red"&gt;type&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;CustomBehaviorSample.MyCustomMessageFormatter, 
			Behavior, Version=1.0.0.0, 
			Culture=neutral, PublicKeyToken=null&lt;/span&gt;" &lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviorExtensions&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;extensions&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;This tells WCF where to find the bits for your custom behavior. The name you assign here will be used when we create the behavior configuration. The type attribute is the full name of the class with all the rest of the type information WCF needs to locate and load it. In your config file is it CRUCIAL that this all be on one, non broken line. I broke it here so it would be easier to read. Also remember that spelling counts. I spent about 25 minutes trying to figure out why this didn't work for me the first time, only to discover that I had misspelled the word "neutral." &lt;/p&gt;
&lt;p&gt;So far, so good. The next step is creating the behavior configuration. &lt;/p&gt;
&lt;p&gt;Do you remember last time when I said there were two type of behaviors? Service and Endpoint Behaviors. Well, trust me, I said it. &lt;/p&gt;
&lt;p&gt;Anyway, since Service and Endpoint configurations need to be separated in our config file, this is where we need that information again. For the record, we created an Endpoint behavior, so our behavior configuration will look like this:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;endpointBehaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MyCustomEndpointBehavior&lt;/span&gt;"&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;CustomBehaviorElement&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;/&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;endpointBehaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&lt;font face="Trebuchet MS" color="#000000"&gt;Let's contrast that with a behavior configuration for an out-of-the-box behavior:&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior &lt;/span&gt;&lt;span style="COLOR: red"&gt;name&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MEXBehaviour&lt;/span&gt;"&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;serviceMetadata &lt;/span&gt;&lt;span style="COLOR: red"&gt;httpGetEnabled&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;true&lt;/span&gt;"&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;/&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behavior&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;                
    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;/font&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;Aside from the fact that one of these is an Endpoint behavior and one is a Service behavior, there aren't a lot of differences. We set a name so that we can reference the behavior elsewhere in our configuration. In our WCF-supplied MEX behavior we have a pre-defined configuration tag which supplies the configuration information for us. In the configuration for our custom behavior, we are doing the same thing, except that the behavior tag that we are using was defined by when we added the behavior type information in the behaviorExtensions section of the configuration. &lt;/p&gt;
&lt;p&gt;The other difference we see is that we are setting a property of the MEX behavior; httpGetEnabled. We have the ability to add our own custom properties and we'll do this in the next post.&lt;/p&gt;
&lt;p&gt;OK, we're almost there!&lt;/p&gt;
&lt;p&gt;All that's left is to wire up are endpoint behavior configuration:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;font size="4"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;endpoint &lt;/span&gt;&lt;span style="COLOR: red"&gt;address&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;HelloWCF&lt;/span&gt;" &lt;span style="COLOR: red"&gt;binding&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;wsHttpBinding&lt;/span&gt;" 
&lt;span style="COLOR: red"&gt;	contract&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;CustomBehaviorSample.IHelloWCF&lt;/span&gt;" 
          &lt;span style="COLOR: red"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;MyCustomEndpointBehavior&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;And that's pretty much it. If we run it, we get the result we expect:&lt;/p&gt;
&lt;p&gt; &lt;img style="WIDTH: 426px; HEIGHT: 162px" height="162" alt="See, it worked!" width="401" src="http://jamescbender.com/bendersblog/images/blog/haha.png" /&gt;&lt;/p&gt;
&lt;p&gt;Easy peasy! But, that message is hard coded into our behavior, and I don't really like that. We'll fix that in the next post...&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/10.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/03/01/making-wcf-quotbehavequot---part-three.aspx</guid>
            <pubDate>Sat, 01 Mar 2008 15:29:03 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/10.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/03/01/making-wcf-quotbehavequot---part-three.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/10.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>