<?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>Damian&#039;s Blog &#187; WCF</title>
	<atom:link href="http://damianblog.com/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://damianblog.com</link>
	<description>.NET from Geneva, Switzerland</description>
	<lastBuildDate>Fri, 30 Sep 2011 23:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Calling WCF Services from Excel VBA clients using the WCF Service Moniker</title>
		<link>http://damianblog.com/2009/07/05/excel-wcf/</link>
		<comments>http://damianblog.com/2009/07/05/excel-wcf/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 16:25:48 +0000</pubDate>
		<dc:creator>dmehers</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://damianblog.com/2009/07/05/excel-wcf/</guid>
		<description><![CDATA[Did you know there is a way to invoke simple Windows Communication Foundation (WCF) Services from Excel VBA without installing anything other than the .NET Framework on the Excel machine? There are many different kinds of monikers, but the one that interests us is the Service moniker that can be used to access WCF Services: [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know there is a way to invoke simple Windows Communication Foundation (WCF) Services from Excel VBA <strong>without installing anything</strong> other than the .NET Framework on the Excel machine?</p>
<p>There are many different kinds of monikers, but the one that interests us is the Service moniker that can be used to access WCF Services:</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b1ffa1c4-e872-4233-b565-4cddd7ce03b5" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #0000FF;">Set</span><span style="color: #000000;"> someObject </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">GetObject</span><span style="color: #000000;">(“service:Binding Information”)</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
<p>In this blog post I’m going to show how you how to go about using WCF Services from Excel VBA, step by step, from beginning to end, and explain how to debug the rather obtuse messages that Excel displays when you’ve misconfigured something.</p>
<h2>The WCF Service</h2>
<p>The WCF Interface you expose can not be particularly complex if you want to use the Service Moniker – I’ve found it best to keep top primitive types, and arrays of primitive types.&#160; Data Contracts seem to be a no-go.</p>
<p>Start by creating a new WCF Service using File|New|Project and selecting WCF Service Library: </p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb.png" width="478" height="286" /></a> </p>
<p>Double-click on IService1.cs in the Solution Explorer, and replace the default contents with this simple interface:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b639b7d1-cb49-4989-8b5b-755c4828df09" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #0000FF;">using</span><span style="color: #000000;"> System;
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.ServiceModel;

</span><span style="color: #0000FF;">namespace</span><span style="color: #000000;"> WcfService1
{
    [ServiceContract]
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> IService1
    {
        [OperationContract]
        </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> GetData(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> value);

        [OperationContract]
        </span><span style="color: #0000FF;">object</span><span style="color: #000000;">[] GetSomeObjects();
    }
}
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Next replace the contents of the service implementation, Service1.cs, with the following:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:533a07e2-ec84-4e89-8c46-a97fa219fbfc" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #0000FF;">using</span><span style="color: #000000;"> System;

</span><span style="color: #0000FF;">namespace</span><span style="color: #000000;"> WcfService1
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> Service1 : IService1
    {
        </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> GetData(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> value)
        {
            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;">.Format(</span><span style="color: #800000;">"</span><span style="color: #800000;">You entered: {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, value);
        }

        </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">object</span><span style="color: #000000;">[] GetSomeObjects()
        {
            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> </span><span style="color: #0000FF;">object</span><span style="color: #000000;">[] { </span><span style="color: #800000;">"</span><span style="color: #800000;">String</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800080;">123</span><span style="color: #000000;">, </span><span style="color: #800080;">44.55</span><span style="color: #000000;">, DateTime.Now };
        }
    }
}
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<h2>Configuring the WCF Service</h2>
<p>By default the WCF Service is configured to use HTTP as the transport protocol.&#160; I generally switch it to use TCP, because I am operating within a corporate intranet, and HTTP seems like overkill.</p>
<p>You’ll also find that by default your WCF Service exposes two endpoints.&#160; The first exposes as you’d expect, the IService1 interface you defined above.&#160; The second exposes Metadata about your service, which the Service Moniker uses to know what operations are available on your service.&#160; You’ll need both.</p>
<p>Right-click on the App.config file in the Solution Explorer, and select Edit WCF Configuration:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_3.png" width="260" height="213" /></a> </p>
<p>Switch the first endpoint to use TCP:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_4.png" width="455" height="299" /></a></p>
<p>Also change the second to use mexTcpBinding:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_5.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_5.png" width="459" height="302" /></a> </p>
<p>Change the base address that the service will use, to use a TCP address instead of a HTTP Address by selecting the Host node on the left hand tree, and then selecting the base address and clicking on Edit, and changing the text to be <a title="net.tcp://localhost:7891/Test/WcfService1/Service1/" href="net.tcp://localhost:7891/Test/WcfService1/Service1/">net.tcp://localhost:7891/Test/WcfService1/Service1/</a></p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_6.png" width="490" height="324" /></a> </p>
<p>Finally, because you are using TCP instead of HTTP, change the MetataData service to not expect to expose the metadata via HTTP, by changing HttpGetEnabled to False under Advanced|Service Behaviours…</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_7.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_7.png" width="476" height="355" /></a> </p>
<p>Save the changes and exit the WCF Editor.&#160; Your App.config should look like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c6f16c1a-272a-4329-a488-f9db25a11c9f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #0000FF;">&lt;?</span><span style="color: #FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color: #0000FF;">?&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">configuration</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">system.web</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">compilation </span><span style="color: #FF0000;">debug</span><span style="color: #0000FF;">="true"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">system.web</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">system.serviceModel</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">services</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">service </span><span style="color: #FF0000;">behaviorConfiguration</span><span style="color: #0000FF;">="WcfService1.Service1Behavior"</span><span style="color: #FF0000;">
        name</span><span style="color: #0000FF;">="WcfService1.Service1"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </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;">="netTcpBinding"</span><span style="color: #FF0000;"> bindingConfiguration</span><span style="color: #0000FF;">=""</span><span style="color: #FF0000;">
          contract</span><span style="color: #0000FF;">="WcfService1.IService1"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">identity</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">dns </span><span style="color: #FF0000;">value</span><span style="color: #0000FF;">="localhost"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">identity</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">endpoint</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">endpoint </span><span style="color: #FF0000;">address</span><span style="color: #0000FF;">="mex"</span><span style="color: #FF0000;"> binding</span><span style="color: #0000FF;">="mexTcpBinding"</span><span style="color: #FF0000;"> bindingConfiguration</span><span style="color: #0000FF;">=""</span><span style="color: #FF0000;">
          contract</span><span style="color: #0000FF;">="IMetadataExchange"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">host</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">baseAddresses</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">add </span><span style="color: #FF0000;">baseAddress</span><span style="color: #0000FF;">="net.tcp://localhost:7891/Test/WcfService1/Service1/"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">baseAddresses</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">host</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">service</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">services</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">behaviors</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">serviceBehaviors</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">behavior </span><span style="color: #FF0000;">name</span><span style="color: #0000FF;">="WcfService1.Service1Behavior"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">serviceMetadata </span><span style="color: #FF0000;">httpGetEnabled</span><span style="color: #0000FF;">="False"</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
          </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">serviceDebug </span><span style="color: #FF0000;">includeExceptionDetailInFaults</span><span style="color: #0000FF;">="False"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">behavior</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">serviceBehaviors</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">behaviors</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">system.serviceModel</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">configuration</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>To start running your service you can hit Ctrl-F5 in Visual Studio.&#160; This will start a test service host, and a test client.&#160; We will ignore the client, but you can check that the service host has started by clicking on the message in the notification area:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_8.png" width="170" height="72" /></a> </p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_9.png" width="505" height="380" /></a> </p>
<p>Eventually you’ll want to host your service somewhere else, such as a Windows Service.</p>
<h2></h2>
<h2>The Excel VBA Client</h2>
</p>
</p>
</p>
<p>Start Excel, and then hit Alt-F11 to enter the VBA Code editor.&#160; We’ll invoke the WCF Service whenever someone clicks on a cell in the first sheet.&#160; Double-click on Sheet1 on the top left hand side, and then select Worksheet from the drop down of objects:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_10.png" width="512" height="181" /></a> </p>
<p>The Service Moniker needs to know the address of the MetataData Service, the address of the service itself, binding information, and information about the contract.</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4c493c4c-7a30-4ef4-8e2a-06122b61fb94" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Microsoft Sans Serif;font-size:11.25"><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> addr </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">
addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">service:mexAddress=""net.tcp://localhost:7891/Test/WcfService1/Service1/Mex"",</span><span style="color: #800000;">"</span><span style="color: #000000;">
addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> addr </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">address=""net.tcp://localhost:7891/Test/WcfService1/Service1/"",</span><span style="color: #800000;">"</span><span style="color: #000000;">
addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> addr </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">contract=""IService1"", contractNamespace=""http://tempuri.org/"",</span><span style="color: #800000;">"</span><span style="color: #000000;">
addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> addr </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">binding=""NetTcpBinding_IService1"", bindingNamespace=""http://tempuri.org/""</span><span style="color: #800000;">"</span><span style="color: #000000;">
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Once you’ve built up the Service Moniker string, you can use GetObject to resolve it, and then invoke a method on it:</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b1cfd3ca-613f-4661-9439-f477b3e14528" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;;font-family:Microsoft Sans Serif;font-size:11.25"><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> service1 </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">
</span><span style="color: #0000FF;">Set</span><span style="color: #000000;"> service1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">GetObject</span><span style="color: #000000;">(addr)

</span><span style="color: #0000FF;">MsgBox</span><span style="color: #000000;"> service1.GetData(</span><span style="color: #800080;">12</span><span style="color: #000000;">)</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The VBA Editor should look like this:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_11.png" width="495" height="192" /></a> </p>
<p>I’ve set a breakpoint on the first line of code by clicking where the red circle is above.</p>
<p>If you now click on a cell in your sheet, and let the VBA code run, you should see this:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_12.png" width="158" height="158" /></a> </p>
<p>The VBA code has called through to the WCF Service and invoked a method on it, and the service has returned a string.</p>
</p>
</p>
<p>Change the code to invoke the second method, and step through in the debugger, and add a Watch on the result:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_13.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_13.png" width="499" height="258" /></a> </p>
<h3>Whats up with the Tempuri?</h3>
<p>You’ll have noticed that the moniker string references a couple of weird Tempuri namespaces.&#160; These are the defaults, but you can alter them.&#160; This can be useful if you have a single service exposing a couple of interfaces and you only want to work with one of them.&#160; For example your service may expose a complex interface for WPF clients, and a simple interface for a Excel client.&#160; If you give the simple interface a different namespace than the complex interface, you’ll be able to access it from Excel VBA.</p>
<p>To change the contract namespace, edit your interface code:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61b0ac94-93af-4d4f-9ca2-358e4489474c" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #0000FF;">using</span><span style="color: #000000;"> System;
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.ServiceModel;

</span><span style="color: #0000FF;">namespace</span><span style="color: #000000;"> WcfService1
{
    [ServiceContract(Namespace</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">http://damianblog.com/</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> IService1
    {
        [OperationContract]
        </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> GetData(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> value);

        [OperationContract]
        </span><span style="color: #0000FF;">object</span><span style="color: #000000;">[] GetSomeObjects();
    }
}
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Then your service moniker can be changed accordingly:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb124575-9273-4e77-9ecf-54fb98536a56" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12"><span style="color: #000000;">addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> addr </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">contract=""IService1"", contractNamespace=""http://damianblog.com/"",</span><span style="color: #800000;">"</span><span style="color: #000000;">
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<h3>When it doesn’t work</h3>
<p>The Excel message you get when the GetObject doesn’t work properly is not exactly detailed:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_14.png" width="385" height="218" /></a> </p>
<p>There is however a way of getting more detailed information.&#160; You can attach your debugger to Excel, and then see the underlying managed .NET exception.</p>
<p>To attach your debugger to Excel in Visual Studio use Debug|Attach to Process and then select Excel, and click Attach:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_14.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_15.png" width="489" height="337" /></a> </p>
</p>
<p>Next, make sure the debugger stops as soon as an exception occurs by clicking on Debug|Exceptions and check the checkbox to be interrupted when an exception is thrown:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_15.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_16.png" width="512" height="261" /></a> </p>
<p><strong>Don’t forget that you have done this.&#160; If you leave it checked then later on when debugging unrelated code you may get thoroughly confused to have the debugger break inside framework code that throws and then handles an exception which is normally invisible to you.</strong></p>
<p>You’ll also need to tell the debugger to break when code other than your own code throws an exception.&#160; Do this by clicking on Tools|Options and then going to the Debugging section, and uncheck the Enable Just My Code checkbox:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_17.png" width="527" height="308" /></a> </p>
<p>Now run your VBA code as usual, and when you get the error the Visual Studio Debugger will break with more detailed error information. So if I change my moniker string to use an invalid interface name the Excel VBA error is:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_17.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_18.png" width="385" height="218" /></a> </p>
<p>Whereas Visual Studio actually tells you what the error is:</p>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_19.png" width="456" height="273" /></a> </p>
<h2></h2>
</p>
</p>
<h3>The maximum message size quote for incoming messages has been exceeded</h3>
<p><a href="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_19.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.atadore.com/images/CallingWCFServicesfromExcelVBAusingtheWC_DA0D/image_thumb_20.png" width="385" height="218" /></a></p>
<p>You’ll hit this error if your .NET code returns too much data to the Excel client.&#160; You might be able to resolve it by setting the MaxRecivedMessageSize property in an Excel.exe.config file in the same folder as Excel.exe.&#160; They way I have handled it is to move from using the Service Moniker mechanism, and to create a managed Excel AddIn that sets the maximum message size programmatically.</p>
<h2>Summary</h2>
<p>The WCF Service Moniker offers an excellent way for you to invoke simple WCF Interfaces, where small quantities of data are exchanged, without installing anything at all on the Excel Client machine.</p>
<p>In the above examples the service moniker created by the VBA code used the “localhost” address for the service, but if your service is running on another machine you can specify that machine’s address: the service can run anywhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://damianblog.com/2009/07/05/excel-wcf/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Dealing with WCF Error: Cannot create a service reference with namespace X because the name is already in use by an existing service reference, folder or file</title>
		<link>http://damianblog.com/2008/12/04/cannot-create-service-reference/</link>
		<comments>http://damianblog.com/2008/12/04/cannot-create-service-reference/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 11:07:56 +0000</pubDate>
		<dc:creator>dmehers</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://damianblog.com/2008/12/04/cannot-create-service-reference/</guid>
		<description><![CDATA[If you get the above error when trying to add a WCF service reference that you have previously deleted then try: deleting the Service References.DeviceService.Reference.cs.dll file you'll find under obj\Debug\TempPE.&#160; deleting the service folder you'll still find under Service References on disk, even though you deleted the reference in Visual Studio. commenting out all references [...]]]></description>
			<content:encoded><![CDATA[<p>If you get the above error when trying to add a WCF service reference that you have previously deleted then try:</p>
<ul>
<li> deleting the <font face="Consolas">Service References.DeviceService.Reference.cs.dll</font> file you'll find under <font face="consol">obj\Debug\TempPE.&#160; </font></li>
<li>deleting the service folder you'll still find under <font face="Consolas">Service References </font>on disk, even though you deleted the reference in Visual Studio.</li>
<li>commenting out all references to the service's namespace in your code</li>
<li>rebuilding</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://damianblog.com/2008/12/04/cannot-create-service-reference/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WCF REST Services: Setting the response format based on request&#8217;s expected type</title>
		<link>http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/</link>
		<comments>http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 06:42:27 +0000</pubDate>
		<dc:creator>dmehers</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/</guid>
		<description><![CDATA[I just attended the Microsoft PDC in LA.&#160; One of the many excellent sessions was a pre-conference on WCF, part of which was presented by Ron Jacobs. Ron did a fantastic job of explaining WCF REST Services and the WCF REST Starter Kit. One of the examples he showed from the WCF REST Starter kit [...]]]></description>
			<content:encoded><![CDATA[<p>I just attended the Microsoft PDC in LA.&#160; One of the many excellent sessions was a pre-conference on WCF, part of which was presented by <a href="http://blogs.msdn.com/rjacobs/">Ron Jacobs</a>. Ron did a fantastic job of explaining WCF REST Services and the WCF REST Starter Kit.</p>
<p>One of the examples he showed from the WCF REST Starter kit was an example where the response type (JSON or XML) is dynamically set based on the HTTP request's requested content type in the &quot;Accepts&quot; HTTP Header.</p>
<p>That example works by switching between two different operation implementations (methods).</p>
<p>I liked the idea of using the requested content type to automatically return JSON or XML depending on the requested content type, but I wasn't so keen on having to implement two methods.</p>
<p>I thought I'd try to get a similar thing working but using a <strong>single operation implementation</strong> which is called no matter whether or JSON or XML are requested.</p>
<h3>The DynamicResponseType attribute</h3>
<p>This is how it works.&#160; You decorate your method with an additional DynamicResponseType attribute which I have defined:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 2000px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; 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">
<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: white; border-bottom-style: none"><span style="color: #606060">   1:</span> [ServiceContract]</pre>
<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: #606060">   2:</span> [ServiceBehavior(IncludeExceptionDetailInFaults = <span style="color: #0000ff">true</span>, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   3:</span> [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]</pre>
<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: #606060">   4:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Service1</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   5:</span> {</pre>
<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: #606060">   6:</span>     [OperationContract]</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   7:</span>     [WebGet(UriTemplate = <span style="color: #006080">&quot;GetData?param1={i}&amp;param2={s}&quot;</span>)]</pre>
<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: #606060">   8:</span>     <em>[DynamicResponseType]</em></pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">public</span> SampleResponseBody GetData(<span style="color: #0000ff">int</span> i, <span style="color: #0000ff">string</span> s)</pre>
<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: #606060">  10:</span>     {</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  11:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> SampleResponseBody() { </pre>
<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: #606060">  12:</span>             Name = <span style="color: #006080">&quot;Test&quot;</span>,</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             Value = s, </pre>
<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: #606060">  14:</span>             Time = DateTime.Now.ToShortTimeString() </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  15:</span>         };</pre>
<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: #606060">  16:</span>     }</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  17:</span> }</pre>
<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: #606060">  18:</span>&#160; </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  19:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SampleResponseBody</pre>
<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: #606060">  20:</span> {</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>
<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: #606060">  22:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Value { get; set; }</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> IntValue { get; set; }</pre>
<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: #606060">  24:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Time { get; set; }</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  25:</span> }</pre>
</p></div>
</div>
<h3>An example client</h3>
<p>Then when the client requests a specific type (XML or JSON), it is served automatically.</p>
<p>Below I have a pure HTML/Javascript client with two buttons, each of which call the same&#160; GetWebRequest function when they are clicked but passing a different requested content type as a parameter.&#160; The GetWebRequest function issues an HTTP request to the WCF operation I showed above.</p>
<p>The first button says it wants JSON, and the second XML.&#160; This is done by setting the &quot;Accept&quot; request header:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 2000px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; 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">
<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: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span></pre>
<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: #606060">   2:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;form1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;</span></pre>
<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: #606060">   4:</span>     </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">input</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;button&quot;</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">=&quot;Click to request JSON&quot;</span> </pre>
<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: #606060">   6:</span>         <span style="color: #ff0000">onclick</span><span style="color: #0000ff">=&quot;GetWebRequest('application/json');&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   7:</span>     </pre>
<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: #606060">   8:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">input</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;button&quot;</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">=&quot;Click to request XML&quot;</span> </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   9:</span>         <span style="color: #ff0000">onclick</span><span style="color: #0000ff">=&quot;GetWebRequest('application/xml');&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<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: #606060">  10:</span>     </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;</span></pre>
<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: #606060">  12:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:ScriptManager</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;ScriptManager1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:ScriptManager</span><span style="color: #0000ff">&gt;</span></pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span></pre>
<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: #606060">  14:</span>&#160; </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  15:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">script</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;text/javascript&quot;</span><span style="color: #0000ff">&gt;</span>
<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: white; border-bottom-style: none"><span style="color: #606060">   1:</span>&#160; </pre>
<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: #606060">   2:</span>   <span style="color: #0000ff">function</span> GetWebRequest(acceptType) {  </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">var</span> wRequest = <span style="color: #0000ff">new</span> Sys.Net.WebRequest();</pre>
<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: #606060">   4:</span>     <em>wRequest.get_headers()[<span style="color: #006080">&quot;Accept&quot;</span>] = acceptType;</em></pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">var</span> url = <span style="color: #006080">&quot;/Service1.svc/GetData?param1=12&amp;param2=&quot;</span>;</pre>
<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: #606060">   6:</span>     wRequest.set_url(url + <span style="color: #0000ff">new</span> Date());</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   7:</span>     wRequest.set_httpVerb(<span style="color: #006080">&quot;GET&quot;</span>);</pre>
<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: #606060">   8:</span>     wRequest.add_completed(OnWebRequestCompleted);</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   9:</span>     wRequest.invoke();</pre>
<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: #606060">  10:</span>   }</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<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: #606060">  12:</span>   <span style="color: #0000ff">function</span> OnWebRequestCompleted(executor, eventArgs) {</pre>
<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: white; border-bottom-style: none"><span style="color: #606060">  13:</span>     alert(executor.get_responseData());</pre>
<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: #606060">  14:</span>   }</pre>
<p><span style="color: #0000ff">&lt;/</span><span style="color: #800000">script</span><span style="color: #0000ff">&gt;</span></pre>
<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: #606060">  16:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p>This is the form that gets displayed initially:</p>
<p><a href="http://damianblog.com/wp-content/uploads/2008/10/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="202" alt="image" src="http://damianblog.com/wp-content/uploads/2008/10/image-thumb.png" width="651" border="0" /></a></p>
<p>When you click on the first button we request JSON from the WCF Service operation: </p>
<p><a href="http://damianblog.com/wp-content/uploads/2008/10/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="163" alt="image" src="http://damianblog.com/wp-content/uploads/2008/10/image-thumb1.png" width="470" border="0" /></a></p>
<p>When you click on the second button we request XML <strong>from the same WCF Service operation</strong>: </p>
<p><a href="http://damianblog.com/wp-content/uploads/2008/10/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="221" alt="image" src="http://damianblog.com/wp-content/uploads/2008/10/image-thumb2.png" width="491" border="0" /></a> </p>
<h3>How it works.</h3>
<p>I've created my own&#160; WCF ServiceHostFactory which I wire up in the SVC file:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; 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">
<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: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="background-color: #ffff00">&lt;%@ ServiceHost Language=&quot;C#&quot; </pre>
<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: #606060">   2:</span>     Debug=&quot;true&quot; </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   3:</span>     Service=&quot;WcfService2.Service1&quot; </pre>
<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: #606060">   4:</span>     CodeBehind=&quot;Service1.svc.cs&quot; </pre>
<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: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     Factory=&quot;DamianBlog.ServiceHostFactory2Ex&quot; %&gt;</span></pre>
</p></div>
</div>
<p align="left">In my ServiceHostFactory2Ex class I ensure that my own WebServiceHost class gets created.</p>
<p align="left">Then in my own WebServiceHost I ensure that my own WebHttpBehavior replaces the standard one.</p>
<p align="left">Next in my own WebHttpBehavior I override the GetReplyDispatchFormatter method and return my own IDispatchMessageFormatter.</p>
<p align="left">In my own IDispatchMessageFormatter I implement the SerializeReply method and then use a JSON formatter or XML formatter depending on the &quot;Accepts&quot; HTTP request header which I pick up from the OperationContext.Current.RequestContext.RequestMessage.</p>
<p align="left">The full source is available for download here <a href="http://damianblog.com/WCFDynamicResponseDemo.zip">http://damianblog.com/WCFDynamicResponseDemo.zip</a>. </p>
<p align="left">Rob Jacobs blogs at <a href="http://blogs.msdn.com/rjacobs/">http://blogs.msdn.com/rjacobs/</a></p>
<p align="left">
]]></content:encoded>
			<wfw:commentRss>http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

