<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Michael Maddox - Software Development - WCF</title>
    <link>http://www.capprime.com/software_development_weblog/</link>
    <description>Best Practices, Knowlegde Base Type Articles, General Tech Opinions, etc.</description>
    <language>en-us</language>
    <copyright>Michael Maddox</copyright>
    <lastBuildDate>Wed, 24 Jun 2009 13:49:23 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>Michael.P.Maddox@gmail.com</managingEditor>
    <webMaster>Michael.P.Maddox@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.capprime.com/software_development_weblog/Trackback.aspx?guid=c958b038-afe9-4c72-97d9-5caff6169893</trackback:ping>
      <pingback:server>http://www.capprime.com/software_development_weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.capprime.com/software_development_weblog/PermaLink,guid,c958b038-afe9-4c72-97d9-5caff6169893.aspx</pingback:target>
      <dc:creator>Michael Maddox</dc:creator>
      <wfw:comment>http://www.capprime.com/software_development_weblog/CommentView,guid,c958b038-afe9-4c72-97d9-5caff6169893.aspx</wfw:comment>
      <wfw:commentRss>http://www.capprime.com/software_development_weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=c958b038-afe9-4c72-97d9-5caff6169893</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
First, create the application setup described in my <a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx">WCF
- Establishing a sample app baseline</a> blog entry.
</p>
        <p>
We will then modify the client program so that it encounters some errors while communicating
with the WCF Service.
</p>
        <p>
Next, add a new method to ConsoleApp1:
</p>
        <p>
        private static string GenerateStringOfCertainLength(int
stringLength)<br />
        {<br />
            string returnValue
= String.Empty;<br />
            int numTens = stringLength
/ 10;<br />
            int remainder =
stringLength % 10;
</p>
        <p>
            for (int counter
= 0; counter &lt; numTens; counter++)<br />
            {<br />
               
returnValue += "0123456789";<br />
            }<br />
            for (int counter
= 0; counter &lt; remainder; counter++)<br />
            {<br />
               
returnValue += counter.ToString();<br />
            }
</p>
        <p>
            return returnValue;<br />
        }
</p>
        <p>
Replace the contents of the try block in ConsoleApp1 with:
</p>
        <p>
               
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();<br />
               
ServiceReference1.CompositeType oneCompositeType = new ServiceReference1.CompositeType();<br />
               
oneCompositeType.StringValue = GenerateStringOfCertainLength(8193);<br />
               
oneCompositeType = oneService1Client.GetDataUsingDataContract(oneCompositeType);
</p>
        <p>
Ctrl-F5 (Debug -&gt; Start Without Debugging)
</p>
        <p>
This may crash Cassini (the VS Web Server) and will result in the following exception:
</p>
        <p>
--<br />
oneException=[System.ServiceModel.FaultException: The formatter threw an exception
while trying to deserialize the message: There was an error while trying to deserialize
parameter http://tempuri.org/:composite. The InnerException message was 'There was
an error deserializing the object of type WcfApp1.CompositeType. The maximum string
content length quota (8192) has been exceeded while reading XML data. This quota may
be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader. Line 1, position 8652.'.  Please see
InnerException for more details.
</p>
        <p>
Server stack trace:<br />
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime
operation, ProxyRpc&amp; rpc)<br />
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)<br />
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)<br />
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)<br />
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
</p>
        <p>
Exception rethrown at [0]:<br />
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)<br />
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;
msgData, Int32 type)<br />
   at ConsoleApp1.ServiceReference1.IService1.GetDataUsingDataContract(CompositeType
composite)<br />
   at ConsoleApp1.ServiceReference1.Service1Client.GetDataUsingDataContract(CompositeType
composite) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Service References\ServiceReference1\Reference.cs:line
120<br />
   at ConsoleApp1.Program.Main(String[] args) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Program.cs:line
17]<br />
--
</p>
        <p>
          <br />
If we send 50000 instead of 8193, we get a different exception:
</p>
        <p>
--<br />
oneException=[System.ServiceModel.ProtocolException: The remote server returned an
unexpected response: (400) Bad Request. ---&gt; System.Net.WebException: The remote
server returned an error: (400) Bad Request.<br />
   at System.Net.HttpWebRequest.GetResponse()<br />
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)<br />
   --- End of inner exception stack trace ---
</p>
        <p>
Server stack trace:<br />
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest
request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)<br />
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)<br />
   at System.ServiceModel.Channels.RequestChannel.Request(Message message,
TimeSpan timeout)<br />
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel
channel, Message message, TimeSpan timeout, MaskingMode maskingMode)<br />
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message
message, TimeSpan timeout, MaskingMode maskingMode)<br />
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message
message, TimeSpan timeout)<br />
   at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message
message, TimeSpan timeout)<br />
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
message, TimeSpan timeout)<br />
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)<br />
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)<br />
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)<br />
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
</p>
        <p>
Exception rethrown at [0]:<br />
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)<br />
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;
msgData, Int32 type)<br />
   at ConsoleApp1.ServiceReference1.IService1.GetDataUsingDataContract(CompositeType
composite)<br />
   at ConsoleApp1.ServiceReference1.Service1Client.GetDataUsingDataContract(CompositeType
composite) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Service References\ServiceReference1\Reference.cs:line
120<br />
   at ConsoleApp1.Program.Main(String[] args) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Program.cs:line
17]<br />
--
</p>
        <p>
          <br />
The out of the box wizard generated service has a couple of limitations in string
lengths that it will support.
</p>
        <p>
These are essentially client side errors and can't be usefully debugged on the server
side or using tools like <a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx">Fiddler</a>.
</p>
        <p>
Let's go back to the 8193 character string and solve that issue first.  We can
do this by modifying the web.config for the WCF web service and the app.config for
the console client application.
</p>
        <p>
There is a tool in Visual Studio that gives us a property editor for WCF configuration. 
You may or may not find it preferable to use the tool over modifying the xml files
directly, but that is how I am going to describe the changes we need to make.
</p>
        <p>
If you right click on the web.config in the Solution Explorer and don't see a menu
option called "Edit WCF Configuration", do the following to get it added:
</p>
        <p>
  In Visual Studio 2008, go to the tools menu, then select "WCF Service
Configuration Editor"<br />
    File -&gt; Exit
</p>
        <p>
Now, right click web.config in Solution Explorer -&gt; Edit WCF Configuration
</p>
        <p>
  Select Bindings -&gt; New Binding Configuration...<br />
    Please select a binding type: wsHttpBinding<br />
  Select Bindings -&gt; NewBinding0 (wsHttpBinding)<br />
    Set Configuration -&gt; Name to: CustomWsHttpBindingConfiguration<br />
    Set ReaderQuotas Properties -&gt; MaxStringContentLength to: 9000
</p>
        <p>
  Select Services -&gt; WcfApp1.Service1 -&gt; Endpoints -&gt; (Empty Name)
[the first one - not mexHttpBinding]<br />
    Select Endpoint Properties -&gt; Binding Configuration: CustomWsHttpBindingConfiguration
</p>
        <p>
  File -&gt; Save<br />
  File -&gt; Exit
</p>
        <p>
Any time we update the web.config file, we should update the service reference, even
though in this case it is likely unnecessary:
</p>
        <p>
  Solution Explorer<br />
    Right Click ConsoleApp1 -&gt; Service References -&gt; ServiceReference1<br />
      Update Service Reference<br />
      OK
</p>
        <p>
We need to make the same change on the client side as we made on the server side:
</p>
        <p>
  Right Click app.config in Solution Explorer -&gt; Edit WCF Configuration<br />
    Select Bindings -&gt; WSHttpBinding_IService1 (wsHttpBinding)<br />
      Set ReaderQuotas Properties -&gt; MaxStringContentLength
to: 9000<br />
    Save &amp; Exit
</p>
        <p>
          <br />
If we run the application now using 8193, the exception is gone.  So, it is now
clear how to send strings larger than 8192 characters to a WCF web service (the exception
helped point us in the right direction).
</p>
        <p>
Now, let's go back to the 50000 scenario.  The above changes are not enough to
fix it as we are now exceeding a different character limit.
</p>
        <p>
Right click web.config in Solution Explorer -&gt; Edit WCF Configuration<br />
  Select Bindings -&gt; CustomWsHttpBindingConfiguration (wsHttpBinding)<br />
    Set General -&gt; MaxReceivedMessageSize to: 75000<br />
    Set ReaderQuotas Properties -&gt; MaxStringContentLength to: 50000<br />
  Save &amp; Exit
</p>
        <p>
Right Click app.config in Solution Explorer -&gt; Edit WCF Configuration<br />
  Select Bindings -&gt; WSHttpBinding_IService1 (wsHttpBinding)<br />
    Set General -&gt; MaxReceivedMessageSize to: 75000<br />
    Set ReaderQuotas Properties -&gt; MaxStringContentLength to: 50000<br />
  Save &amp; Exit
</p>
        <p>
Again, just for best practice reasons, we update the service reference:
</p>
        <p>
  Solution Explorer<br />
    Right Click ConsoleApp1 -&gt; Service References -&gt; ServiceReference1<br />
      Update Service Reference<br />
      OK
</p>
        <p>
How did I come up with those numbers?  Trial and error.  The value for MaxStringContentLength
logically matches the length of the string we are trying to send.  MaxReceivedMessageSize
seems to include extra characters, but it's not clear to me what exactly those extra
characters are.
</p>
        <p>
If you increase the values of these two config settings on the server, but not the
client, the client starts giving useful error messages (like: The maximum message
size quota for incoming messages (70000) has been exceeded.) instead of the mostly
useless "(400) Bad Request." which basically means that the server rejected your request
and is pretty much unwilling to tell you why.
</p>
        <p>
Please note that increasing the size of these config settings makes your web service
more vulnerable to <a href="http://msdn.microsoft.com/en-us/library/ms733135.aspx">denial
of service attacks</a>.
</p>
        <p>
It's pretty easy to exceed the wizard generated values however if you are, for example,
trying to send the contents of a text file as a string parameter to a WCF method.
</p>
        <p>
So, now you know how to configure WCF to allow sending larger strings back and forth.
</p>
        <img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=c958b038-afe9-4c72-97d9-5caff6169893" />
      </body>
      <title>WCF - Fixing client side string length exceptions</title>
      <guid isPermaLink="false">http://www.capprime.com/software_development_weblog/PermaLink,guid,c958b038-afe9-4c72-97d9-5caff6169893.aspx</guid>
      <link>http://www.capprime.com/software_development_weblog/2009/06/24/WCFFixingClientSideStringLengthExceptions.aspx</link>
      <pubDate>Wed, 24 Jun 2009 13:49:23 GMT</pubDate>
      <description>&lt;p&gt;
First, create the application setup described in my &lt;a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx"&gt;WCF
- Establishing a sample app baseline&lt;/a&gt;&amp;nbsp;blog entry.
&lt;/p&gt;
&lt;p&gt;
We will then modify the client program so that it encounters some errors while communicating
with the WCF Service.
&lt;/p&gt;
&lt;p&gt;
Next, add a new method to ConsoleApp1:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static string GenerateStringOfCertainLength(int
stringLength)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string returnValue
= String.Empty;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int numTens = stringLength
/ 10;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int remainder =
stringLength % 10;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int counter
= 0; counter &amp;lt; numTens; counter++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
returnValue += "0123456789";&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int counter
= 0; counter &amp;lt; remainder; counter++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
returnValue += counter.ToString();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return returnValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p&gt;
Replace the contents of the try block in ConsoleApp1 with:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ServiceReference1.CompositeType oneCompositeType = new ServiceReference1.CompositeType();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
oneCompositeType.StringValue = GenerateStringOfCertainLength(8193);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
oneCompositeType = oneService1Client.GetDataUsingDataContract(oneCompositeType);
&lt;/p&gt;
&lt;p&gt;
Ctrl-F5 (Debug -&amp;gt; Start Without Debugging)
&lt;/p&gt;
&lt;p&gt;
This may crash Cassini (the VS Web Server) and will result in the following exception:
&lt;/p&gt;
&lt;p&gt;
--&lt;br&gt;
oneException=[System.ServiceModel.FaultException: The formatter threw an exception
while trying to deserialize the message: There was an error while trying to deserialize
parameter http://tempuri.org/:composite. The InnerException message was 'There was
an error deserializing the object of type WcfApp1.CompositeType. The maximum string
content length quota (8192) has been exceeded while reading XML data. This quota may
be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader. Line 1, position 8652.'.&amp;nbsp; Please see
InnerException for more details.
&lt;/p&gt;
&lt;p&gt;
Server stack trace:&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime
operation, ProxyRpc&amp;amp; rpc)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
&lt;/p&gt;
&lt;p&gt;
Exception rethrown at [0]:&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;amp;
msgData, Int32 type)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.ServiceReference1.IService1.GetDataUsingDataContract(CompositeType
composite)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.ServiceReference1.Service1Client.GetDataUsingDataContract(CompositeType
composite) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Service References\ServiceReference1\Reference.cs:line
120&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.Program.Main(String[] args) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Program.cs:line
17]&lt;br&gt;
--
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
If we send 50000 instead of 8193, we get a different exception:
&lt;/p&gt;
&lt;p&gt;
--&lt;br&gt;
oneException=[System.ServiceModel.ProtocolException: The remote server returned an
unexpected response: (400) Bad Request. ---&amp;gt; System.Net.WebException: The remote
server returned an error: (400) Bad Request.&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.Net.HttpWebRequest.GetResponse()&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; --- End of inner exception stack trace ---
&lt;/p&gt;
&lt;p&gt;
Server stack trace:&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest
request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.RequestChannel.Request(Message message,
TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel
channel, Message message, TimeSpan timeout, MaskingMode maskingMode)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message
message, TimeSpan timeout, MaskingMode maskingMode)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message
message, TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message
message, TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
message, TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean
oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
&lt;/p&gt;
&lt;p&gt;
Exception rethrown at [0]:&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;amp;
msgData, Int32 type)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.ServiceReference1.IService1.GetDataUsingDataContract(CompositeType
composite)&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.ServiceReference1.Service1Client.GetDataUsingDataContract(CompositeType
composite) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Service References\ServiceReference1\Reference.cs:line
120&lt;br&gt;
&amp;nbsp;&amp;nbsp; at ConsoleApp1.Program.Main(String[] args) in C:\dev\Prototyping\WCF\WcfApp1\ConsoleApp1\Program.cs:line
17]&lt;br&gt;
--
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
The out of the box wizard generated service has a couple of limitations in string
lengths that it will support.
&lt;/p&gt;
&lt;p&gt;
These are essentially client side errors and can't be usefully debugged on the server
side or using tools like &lt;a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx"&gt;Fiddler&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Let's go back to the 8193 character string and solve that issue first.&amp;nbsp; We can
do this by modifying the web.config for the WCF web service and the app.config for
the console client application.
&lt;/p&gt;
&lt;p&gt;
There is a tool in Visual Studio that gives us a property editor for WCF configuration.&amp;nbsp;
You may or may not find it preferable to use the tool over modifying the xml files
directly, but that is how I am going to describe the changes we need to make.
&lt;/p&gt;
&lt;p&gt;
If you right click on the web.config in the Solution Explorer and don't see a menu
option called "Edit WCF Configuration", do the following to get it added:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;In Visual Studio 2008, go to the tools menu, then select "WCF Service
Configuration Editor"&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;File -&amp;gt; Exit
&lt;/p&gt;
&lt;p&gt;
Now, right click web.config in Solution Explorer -&amp;gt; Edit WCF Configuration
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;Select Bindings -&amp;gt; New Binding Configuration...&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Please select a binding type: wsHttpBinding&lt;br&gt;
&amp;nbsp;&amp;nbsp;Select Bindings -&amp;gt; NewBinding0 (wsHttpBinding)&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Set Configuration -&amp;gt; Name to: CustomWsHttpBindingConfiguration&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;Set ReaderQuotas Properties -&amp;gt; MaxStringContentLength to: 9000
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;Select Services -&amp;gt; WcfApp1.Service1 -&amp;gt; Endpoints -&amp;gt; (Empty Name)
[the first one - not mexHttpBinding]&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Select Endpoint Properties -&amp;gt; Binding Configuration: CustomWsHttpBindingConfiguration
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;File -&amp;gt; Save&lt;br&gt;
&amp;nbsp;&amp;nbsp;File -&amp;gt; Exit
&lt;/p&gt;
&lt;p&gt;
Any time we update the web.config file, we should update the service reference, even
though in this case it is likely unnecessary:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp; Solution Explorer&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Right Click ConsoleApp1 -&amp;gt; Service References -&amp;gt; ServiceReference1&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;Update Service Reference&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OK
&lt;/p&gt;
&lt;p&gt;
We need to make the same change on the client side as we made on the server side:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;Right Click app.config in Solution Explorer -&amp;gt; Edit WCF Configuration&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Select Bindings -&amp;gt; WSHttpBinding_IService1 (wsHttpBinding)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set ReaderQuotas Properties -&amp;gt; MaxStringContentLength
to: 9000&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;Save &amp;amp; Exit
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
If we run the application now using 8193, the exception is gone.&amp;nbsp; So, it is now
clear how to send strings larger than 8192 characters to a WCF web service (the exception
helped point us in the right direction).
&lt;/p&gt;
&lt;p&gt;
Now, let's go back to the 50000 scenario.&amp;nbsp; The above changes are not enough to
fix it as we are now exceeding a different character limit.
&lt;/p&gt;
&lt;p&gt;
Right click web.config in Solution Explorer -&amp;gt; Edit WCF Configuration&lt;br&gt;
&amp;nbsp;&amp;nbsp;Select Bindings -&amp;gt; CustomWsHttpBindingConfiguration (wsHttpBinding)&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Set General -&amp;gt; MaxReceivedMessageSize to: 75000&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;Set ReaderQuotas Properties -&amp;gt; MaxStringContentLength to: 50000&lt;br&gt;
&amp;nbsp;&amp;nbsp;Save &amp;amp; Exit
&lt;/p&gt;
&lt;p&gt;
Right Click app.config in Solution Explorer -&amp;gt; Edit WCF Configuration&lt;br&gt;
&amp;nbsp; Select Bindings -&amp;gt; WSHttpBinding_IService1 (wsHttpBinding)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Set General -&amp;gt; MaxReceivedMessageSize to: 75000&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;Set ReaderQuotas Properties -&amp;gt; MaxStringContentLength to: 50000&lt;br&gt;
&amp;nbsp;&amp;nbsp;Save &amp;amp; Exit
&lt;/p&gt;
&lt;p&gt;
Again, just for best practice reasons, we update the service reference:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;Solution Explorer&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;Right Click ConsoleApp1 -&amp;gt; Service References -&amp;gt; ServiceReference1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Update Service Reference&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;OK
&lt;/p&gt;
&lt;p&gt;
How did I come up with those numbers?&amp;nbsp; Trial and error.&amp;nbsp; The value for MaxStringContentLength
logically matches the length of the string we are trying to send.&amp;nbsp; MaxReceivedMessageSize
seems to include extra characters, but it's not clear to me what exactly those extra
characters are.
&lt;/p&gt;
&lt;p&gt;
If you increase the values of these two config settings on the server, but not the
client, the client starts giving useful error messages (like: The maximum message
size quota for incoming messages (70000) has been exceeded.) instead of the mostly
useless "(400) Bad Request." which basically means that the server rejected your request
and is pretty much unwilling to tell you why.
&lt;/p&gt;
&lt;p&gt;
Please note that increasing the size of these config settings makes your web service
more vulnerable to &lt;a href="http://msdn.microsoft.com/en-us/library/ms733135.aspx"&gt;denial
of service attacks&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
It's pretty easy to exceed the wizard generated values however if you are, for example,
trying to send the contents of a text file as a string parameter to a WCF method.
&lt;/p&gt;
&lt;p&gt;
So, now you know how to configure WCF to allow sending larger strings back and forth.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=c958b038-afe9-4c72-97d9-5caff6169893" /&gt;</description>
      <comments>http://www.capprime.com/software_development_weblog/CommentView,guid,c958b038-afe9-4c72-97d9-5caff6169893.aspx</comments>
      <category>Knowledge Base</category>
      <category>Visual Studio 2008</category>
      <category>WCF</category>
      <category>Web Services</category>
    </item>
    <item>
      <trackback:ping>http://www.capprime.com/software_development_weblog/Trackback.aspx?guid=fc7d69c2-b692-4012-b679-63d7d79c2cb5</trackback:ping>
      <pingback:server>http://www.capprime.com/software_development_weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx</pingback:target>
      <dc:creator>Michael Maddox</dc:creator>
      <wfw:comment>http://www.capprime.com/software_development_weblog/CommentView,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx</wfw:comment>
      <wfw:commentRss>http://www.capprime.com/software_development_weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=fc7d69c2-b692-4012-b679-63d7d79c2cb5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <strong>
            <em>First</em>
          </strong>, create the application setup described in my <a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx">WCF
- Establishing a sample app baseline</a> blog entry.<br />
We will then modify that simple application so that it's traffic will show up in <a href="http://www.fiddler2.com/fiddler2/">Fiddler2</a>.
</p>
        <p>
          <strong>
            <em>Second</em>
          </strong>, install and startup Fiddler.  It is probably
worth watching the QuickStart video if you haven't already.<br />
After you install and run Fiddler, and then run ConsoleApp1, you'll notice that the
WCF traffic doesn't show up in Fiddler.  There are multiple ways to enable that,
but we are only going to cover one simple one right now.
</p>
        <p>
          <strong>
            <em>Third</em>
          </strong>, modify the client application so that Fiddler will
display it's traffic.
</p>
        <p>
After this line of code in ConsoleApp1 Program.cs Main:
</p>
        <p>
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();
</p>
        <p>
Add:<br />
               
oneService1Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(<br />
                   
new Uri(oneService1Client.Endpoint.Address.Uri.ToString().Replace("localhost", "127.0.0.1.")),<br />
                   
oneService1Client.Endpoint.Address.Identity,<br />
                   
oneService1Client.Endpoint.Address.Headers);
</p>
        <p>
This essentially pushes all the network traffic into a place where Fiddler can see
it (the period after the 1 is critical, it's not a typo!).
</p>
        <p>
Ctrl-F5 (Debug -&gt; Start Without Debugging)
</p>
        <p>
You should now see traffic showing up in Fiddler.
</p>
        <p>
If you shut down Fiddler, the application will still work fine (but if you used "ipv4.fiddler"
instead of "127.0.0.1.", you would get an exception like: [EndpointNotFoundException:
There was no endpoint listening at http://ipv4.fiddler:1100/Service1.svc that could
accept the message.]).
</p>
        <p>
At this point, for readability/testability purposes, I would encourage you to modify
the web.config for the WCF service and replace the wsHttpBinding with the basicHttpBinding. 
This will make it much more clear what is going on.
</p>
        <p>
If you modify the WCF web.config, you have to update the reference in the client (which
will generally regenerate the client's app.config to match):
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApp1 -&gt; Service References -&gt; ServiceReference1 -&gt;
Update Service Reference
</p>
        <p>
(This is basically needed anytime the WCF application changes it's public interface.)
</p>
        <img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=fc7d69c2-b692-4012-b679-63d7d79c2cb5" />
      </body>
      <title>WCF - Using Fiddler to watch console app to WCF traffic</title>
      <guid isPermaLink="false">http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx</guid>
      <link>http://www.capprime.com/software_development_weblog/2009/06/09/WCFUsingFiddlerToWatchConsoleAppToWCFTraffic.aspx</link>
      <pubDate>Tue, 09 Jun 2009 18:53:45 GMT</pubDate>
      <description>&lt;p&gt;
&lt;strong&gt;&lt;em&gt;First&lt;/em&gt;&lt;/strong&gt;, create the application setup described in my &lt;a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx"&gt;WCF
- Establishing a sample app baseline&lt;/a&gt;&amp;nbsp;blog entry.&lt;br&gt;
We will then modify that simple application so that it's traffic will show up in &lt;a href="http://www.fiddler2.com/fiddler2/"&gt;Fiddler2&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;em&gt;Second&lt;/em&gt;&lt;/strong&gt;, install and startup Fiddler.&amp;nbsp; It is probably
worth watching the QuickStart video if you haven't already.&lt;br&gt;
After you install and run Fiddler, and then run ConsoleApp1, you'll notice that the
WCF traffic doesn't show up in Fiddler.&amp;nbsp; There are multiple ways to enable that,
but we are only going to cover one simple one right now.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;em&gt;Third&lt;/em&gt;&lt;/strong&gt;, modify the client application so that Fiddler will
display it's traffic.
&lt;/p&gt;
&lt;p&gt;
After this line of code in ConsoleApp1 Program.cs Main:
&lt;/p&gt;
&lt;p&gt;
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();
&lt;/p&gt;
&lt;p&gt;
Add:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
oneService1Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
new Uri(oneService1Client.Endpoint.Address.Uri.ToString().Replace("localhost", "127.0.0.1.")),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
oneService1Client.Endpoint.Address.Identity,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
oneService1Client.Endpoint.Address.Headers);
&lt;/p&gt;
&lt;p&gt;
This essentially pushes all the network traffic into a place where Fiddler can see
it (the period after the 1 is critical, it's not a typo!).
&lt;/p&gt;
&lt;p&gt;
Ctrl-F5 (Debug -&amp;gt; Start Without Debugging)
&lt;/p&gt;
&lt;p&gt;
You should now see traffic showing up in Fiddler.
&lt;/p&gt;
&lt;p&gt;
If you shut down Fiddler, the application will still work fine (but if you used "ipv4.fiddler"
instead of "127.0.0.1.", you would get an exception like: [EndpointNotFoundException:
There was no endpoint listening at http://ipv4.fiddler:1100/Service1.svc that could
accept the message.]).
&lt;/p&gt;
&lt;p&gt;
At this point, for readability/testability purposes, I would encourage you to modify
the web.config for the WCF service and replace the wsHttpBinding with the basicHttpBinding.&amp;nbsp;
This will make it much more clear what is going on.
&lt;/p&gt;
&lt;p&gt;
If you modify the WCF web.config, you have to update the reference in the client (which
will generally regenerate the client's app.config to match):
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApp1 -&amp;gt; Service References -&amp;gt; ServiceReference1 -&amp;gt;
Update Service Reference
&lt;/p&gt;
&lt;p&gt;
(This is basically needed anytime the WCF application changes it's public interface.)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=fc7d69c2-b692-4012-b679-63d7d79c2cb5" /&gt;</description>
      <comments>http://www.capprime.com/software_development_weblog/CommentView,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx</comments>
      <category>Knowledge Base</category>
      <category>Visual Studio 2008</category>
      <category>WCF</category>
      <category>Web Services</category>
    </item>
    <item>
      <trackback:ping>http://www.capprime.com/software_development_weblog/Trackback.aspx?guid=bdd1a64b-7f93-4085-a4d6-b92e2341d9f8</trackback:ping>
      <pingback:server>http://www.capprime.com/software_development_weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx</pingback:target>
      <dc:creator>Michael Maddox</dc:creator>
      <wfw:comment>http://www.capprime.com/software_development_weblog/CommentView,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx</wfw:comment>
      <wfw:commentRss>http://www.capprime.com/software_development_weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=bdd1a64b-7f93-4085-a4d6-b92e2341d9f8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We are going to run the WCF application wizard and then create a client program that
establishes basic communication with that WCF Service.
</p>
        <p>
We will be using Visual Studio 2008 with Service Pack 1
</p>
        <p>
File -&gt; New -&gt; Project<br />
 Visual C# -&gt; Web -&gt; WCF Service Application<br />
  WcfApp1 -&gt; OK<br />
Solution Explorer<br />
 Right Click Solution -&gt; Add -&gt; New Project<br />
  Visual C# -&gt; Windows -&gt; Console Application<br />
   ConsoleApp1 -&gt; OK
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApp1 -&gt; References -&gt; Add Service Reference...<br />
  Discover<br />
  OK
</p>
        <p>
If adding the service reference fails, try to run WcfApp1 (Debug -&gt; Start Without
Debugging) and click on "Service1.svc" first.  For whatever reason, the WCF service
doesn't always "auto-start" when it should.  I end up using this "workaround"
quite often.
</p>
        <p>
Add the following to the Main method in Program.cs in ConsoleApp1:
</p>
        <p>
            try<br />
            {<br />
               
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();<br />
               
string serviceOutput = oneService1Client.GetData(-1);<br />
               
Console.WriteLine("serviceOutput=[" + serviceOutput + "]");<br />
            }<br />
            catch (Exception
oneException)<br />
            {<br />
               
Console.WriteLine("oneException=[" + oneException.ToString() + "]");<br />
               
Environment.Exit(-1);<br />
            }
</p>
        <p>
          <br />
Solution Explorer<br />
 Right Click ConsoleApp1 -&gt; Set as StartUp Project
</p>
        <p>
Ctrl-F5 (Debug -&gt; Start Without Debugging)
</p>
        <p>
Program output should be:
</p>
        <p>
--<br />
serviceOutput=[You entered: -1]<br />
Press any key to continue . . .<br />
--
</p>
        <p>
We now have a basic WCF application with a basic client that calls it.
</p>
        <p>
One very helpful tip once the basics are running is to get Fiddler up and running
to be able to watch WCF traffic in case things go downhill after you make some changes.
</p>
        <p>
Click <a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx">here</a> for
instructions on how to do that.
</p>
        <img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=bdd1a64b-7f93-4085-a4d6-b92e2341d9f8" />
      </body>
      <title>WCF - Establishing a sample app baseline</title>
      <guid isPermaLink="false">http://www.capprime.com/software_development_weblog/PermaLink,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx</guid>
      <link>http://www.capprime.com/software_development_weblog/2009/06/09/WCFEstablishingASampleAppBaseline.aspx</link>
      <pubDate>Tue, 09 Jun 2009 18:46:17 GMT</pubDate>
      <description>&lt;p&gt;
We are going to run the WCF application wizard and then create a client program that
establishes basic communication with that WCF Service.
&lt;/p&gt;
&lt;p&gt;
We will be using Visual Studio 2008 with Service Pack 1
&lt;/p&gt;
&lt;p&gt;
File -&amp;gt; New -&amp;gt; Project&lt;br&gt;
&amp;nbsp;Visual C# -&amp;gt; Web -&amp;gt; WCF Service Application&lt;br&gt;
&amp;nbsp;&amp;nbsp;WcfApp1 -&amp;gt; OK&lt;br&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click Solution -&amp;gt; Add -&amp;gt; New Project&lt;br&gt;
&amp;nbsp;&amp;nbsp;Visual C# -&amp;gt; Windows -&amp;gt; Console Application&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;ConsoleApp1 -&amp;gt; OK
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApp1 -&amp;gt; References -&amp;gt; Add Service Reference...&lt;br&gt;
&amp;nbsp;&amp;nbsp;Discover&lt;br&gt;
&amp;nbsp;&amp;nbsp;OK
&lt;/p&gt;
&lt;p&gt;
If adding the service reference fails, try to run WcfApp1 (Debug -&amp;gt; Start Without
Debugging) and click on "Service1.svc" first.&amp;nbsp; For whatever reason, the WCF service
doesn't always "auto-start" when it should.&amp;nbsp; I end up using this "workaround"
quite often.
&lt;/p&gt;
&lt;p&gt;
Add the following to the Main method in Program.cs in ConsoleApp1:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ServiceReference1.Service1Client oneService1Client = new ServiceReference1.Service1Client();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
string serviceOutput = oneService1Client.GetData(-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Console.WriteLine("serviceOutput=[" + serviceOutput + "]");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception
oneException)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Console.WriteLine("oneException=[" + oneException.ToString() + "]");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Environment.Exit(-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApp1 -&amp;gt; Set as StartUp Project
&lt;/p&gt;
&lt;p&gt;
Ctrl-F5 (Debug -&amp;gt; Start Without Debugging)
&lt;/p&gt;
&lt;p&gt;
Program output should be:
&lt;/p&gt;
&lt;p&gt;
--&lt;br&gt;
serviceOutput=[You entered: -1]&lt;br&gt;
Press any key to continue . . .&lt;br&gt;
--
&lt;/p&gt;
&lt;p&gt;
We now have a basic WCF application with a basic client that calls it.
&lt;/p&gt;
&lt;p&gt;
One very helpful tip once the basics are running is to get Fiddler up and running
to be able to watch WCF traffic in case things go downhill after you make some changes.
&lt;/p&gt;
&lt;p&gt;
Click &lt;a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,fc7d69c2-b692-4012-b679-63d7d79c2cb5.aspx"&gt;here&lt;/a&gt; for
instructions on how to do that.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=bdd1a64b-7f93-4085-a4d6-b92e2341d9f8" /&gt;</description>
      <comments>http://www.capprime.com/software_development_weblog/CommentView,guid,bdd1a64b-7f93-4085-a4d6-b92e2341d9f8.aspx</comments>
      <category>Visual Studio 2008</category>
      <category>WCF</category>
      <category>Web Services</category>
    </item>
  </channel>
</rss>