title: Sending byte array to WCF Service
body: <p>Hello I tried all solutions from stackoverflow.com but it didn't work for me.</p>
  
  <p>Here is my code</p>
  
  <p><b>WCF Service</b>:</p>
  
  <pre><code>public void SendImage(byte[] img, string filename)
  {
      MemoryStream memoryStream = new MemoryStream(img);
      FileStream stream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + filename, FileMode.Create);
      memoryStream.CopyTo(stream);
      stream.Close();
  }
  </code></pre>
  
  <p><b>Interface:</b></p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "sendImage")]
  void SendImage(byte[] img, string filename);
  </code></pre>
  
  <p>Here is my <b>Web.Config</b>:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
      &lt;system.diagnostics&gt;
          &lt;sources&gt;
              &lt;source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true" &gt;
                  &lt;listeners&gt;
                      &lt;add name="xml"/&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
              &lt;source name="System.ServiceModel.MessageLogging"&gt;
                  &lt;listeners&gt;
                      &lt;add name="xml"/&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
              &lt;source name="myUserTraceSource"
                  switchValue="Information, ActivityTracing"&gt;
                  &lt;listeners&gt;
                      &lt;add name="xml"/&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
          &lt;/sources&gt;
          &lt;sharedListeners&gt;
              &lt;add name="xml"
                  type="System.Diagnostics.XmlWriterTraceListener"
                  initializeData="Traces.svclog" /&gt;
          &lt;/sharedListeners&gt;
      &lt;/system.diagnostics&gt;
      &lt;system.web&gt;
          &lt;compilation debug="true" targetFramework="4.0" /&gt;
      &lt;/system.web&gt;
      &lt;system.serviceModel&gt;
          &lt;diagnostics wmiProviderEnabled="true"&gt;
              &lt;messageLogging
                  logEntireMessage="true"
                  logMalformedMessages="true"
                  logMessagesAtServiceLevel="true"
                  logMessagesAtTransportLevel="true"
                  maxMessagesToLog="3000"
                  /&gt;
          &lt;/diagnostics&gt;
              &lt;services&gt;
                  &lt;service name="WcfService1.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"&gt;
                      &lt;endpoint address="" binding="basicHttpBinding" contract="WcfService1.IRestServiceImpl" behaviorConfiguration="web"&gt;
  
                      &lt;/endpoint&gt;
                  &lt;/service&gt;
              &lt;/services&gt;
              &lt;bindings&gt;
                  &lt;!-- 
                      Following is the expanded configuration section for a BasicHttpBinding.
                      Each property is configured with the default value.
                      See the TransportSecurity, and MessageSecurity samples in the
                      Basic directory to learn how to configure these features.
                  --&gt;
                  &lt;basicHttpBinding&gt;
                      &lt;binding name="Binding1" hostNameComparisonMode="StrongWildcard"
                          receiveTimeout="00:10:00"
                          sendTimeout="00:10:00"
                          openTimeout="00:10:00"
                          closeTimeout="00:10:00"
                          maxReceivedMessageSize="2147483647"
                          maxBufferSize="2147483647"
                          transferMode="Streamed"
                          messageEncoding="Text"
                          bypassProxyOnLocal="false"
                          useDefaultWebProxy="true"
                          maxBufferPoolSize="2147483647"&gt;
                      &lt;readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
                      &lt;security mode="None" /&gt;
                  &lt;/binding&gt;
              &lt;/basicHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;behaviors&gt;
              &lt;serviceBehaviors&gt;
                  &lt;behavior name="ServiceBehaviour"&gt;
                      &lt;dataContractSerializer maxItemsInObjectGraph="2147483647"/&gt;
                      &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt;
                      &lt;serviceMetadata httpGetEnabled="true"/&gt;
                      &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
                      &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
                  &lt;/behavior&gt;
              &lt;/serviceBehaviors&gt;
              &lt;endpointBehaviors&gt;
                  &lt;behavior name="web"&gt;
                      &lt;dataContractSerializer maxItemsInObjectGraph="2147483647"/&gt;    
                  &lt;/behavior&gt;
              &lt;/endpointBehaviors&gt;
          &lt;/behaviors&gt;
          &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
      &lt;/system.serviceModel&gt;
      &lt;system.webServer&gt;
          &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
      &lt;/system.webServer&gt;
      &lt;system.web&gt;
          &lt;httpRuntime maxRequestLength="2097151" /&gt;
      &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>And my <b>app.config</b>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;system.serviceModel&gt;
          &lt;client&gt;
              &lt;endpoint address="http://localhost:55008/RestServiceImpl.svc"
                  binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding_IRestServiceImpl"
                  contract="ServiceReference1.IRestServiceImpl"      name="BasicHttpBinding_IRestServiceImpl"/&gt;
          &lt;/client&gt;
          &lt;bindings&gt;
              &lt;basicHttpBinding&gt;
                  &lt;binding name="BasicHttpBinding_IRestServiceImpl"
                      hostNameComparisonMode="StrongWildcard"
                      receiveTimeout="00:10:00"
                      sendTimeout="00:10:00"
                      openTimeout="00:10:00"
                      closeTimeout="00:10:00"
                      maxReceivedMessageSize="2147483647"
                      maxBufferSize="2147483647"
                      transferMode="StreamedResponse"
                      messageEncoding="Text"
                      bypassProxyOnLocal="false"
                      useDefaultWebProxy="true"
                      maxBufferPoolSize="2147483647"&gt;
                      &lt;readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
                      &lt;security mode="None" /&gt;
                  &lt;/binding&gt;
              &lt;/basicHttpBinding&gt;
          &lt;/bindings&gt;
      &lt;/system.serviceModel&gt;
      &lt;system.web&gt;
          &lt;httpRuntime maxRequestLength="2097151" /&gt;
      &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My question is why I get:</p>
  
  <blockquote>
    <p>System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (413) Request Entity Too Large. ---> System.Net.WebException: The remote server returned an error: (413) Request Entity Too Large.
      at System.Net.HttpWebRequest.GetResponse()
      at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
      --- End of inner exception stack trace ---</p>
    
    <p>Server stack trace: 
      at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory 1 factory, WebException responseException, ChannelBinding channelBinding)
      at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
      at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
      at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
      at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
      at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
      at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)</p>
    
    <p>Exception rethrown at [0]: 
      at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
      at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
      at WpfApplication1.ServiceReference1.IRestServiceImpl.SendImage(Byte[] img, String filename)
      at WpfApplication1.ServiceReference1.RestServiceImplClient.SendImage(Byte[] img, String filename) in C:\Users\lukgol\documents\visual studio 2010\Projects\WcfService1\WpfApplication1\Service References\ServiceReference1\Reference.cs:line 120
      at WpfApplication1.MainWindow.buttonSendToServer_Click(Object sender, RoutedEventArgs e) in C:\Users\lukgol\documents\visual studio 2010\Projects\WcfService1\WpfApplication1\MainWindow.xaml.cs:line 97</p>
  </blockquote>
  
  <p>When I run this code:</p>
  
  <pre><code>private void buttonSendToServer_Click(object sender, RoutedEventArgs e)
  {
      imageCtrl.Source = null;
      ServiceReference1.RestServiceImplClient service = new ServiceReference1.RestServiceImplClient();
      foreach (MyBitmap bit in listBitmap)
      {
          string s;
          try
          {
              service.SendImage(bit.GetBytes(), bit.FileName);
          }
          catch (ProtocolException ex)
          {
              s = ex.ToString();
          }
      }
      service.Close();
  }
  </code></pre>