title: System.Net.WebException: The remote server returned an error: NotFound. ---> silverlight file post to WCF Rest Service
body: <p>i am trying to post an image to wcf rest service from silverlight 4 but i am getting an exception when i am taking resonse from the request, the exception is </p>
  
  <p>System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
     at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
     at System.Net.Browser.BrowserHttpWebRequest.&lt;>c_<em>DisplayClassa.b</em>_9(Object sendState)
     at System.Net.Browser.AsyncHelper.&lt;>c_<em>DisplayClass4.b</em>_1(Object sendState)
     --- End of inner exception stack trace ---
     at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
     at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     at Admin.Popups.AddCategory.OnImageGetResponseCompleted(IAsyncResult ar)}</p>
  
  <p>all of my other functions on the service is working fine even this one when i try to access it from android, i already had a look and tried the silverlight cross domain policy but i dont think its applicable, my code is below</p>
  
  <pre><code>private void UpLoadImage()
          {
              string URL = Utilities.GetServiceUrl() + "AddCategoryImage?CategoryId=2&amp;ContentType=image&amp;apikey=" + Utilities.GetApiKey();
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
              request.Method = "POST";
              request.ContentType = "text/plain";
              request.ContentLength = SelectedFile.Length;
  
              request.BeginGetRequestStream(OnImageGetRequestStreamCompleted, request);
          }
  
          private void OnImageGetRequestStreamCompleted(IAsyncResult ar)
          {
              try
              {
                  if (SelectedFile != null)
                  {
                      HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
  
                      using (Stream postStream = request.EndGetRequestStream(ar))
                      {
                          using (Stream source = SelectedFile.OpenRead())
                          {
                              byte[] buffer = new byte[2048];
                              int bytesRead;
                              while ((bytesRead = source.Read(buffer, 0, buffer.Length)) &gt; 0)
                              {
                                  postStream.Write(buffer, 0, bytesRead);
                              }
                          }
  
                     }
  
                      request.BeginGetResponse(new AsyncCallback(OnImageGetResponseCompleted), request);
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
  
          private void OnImageGetResponseCompleted(IAsyncResult ar)
          {
              try
              {
                  HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
                  HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
                  using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                  {
                      string result = sr.ReadToEnd();
                  }
              }
              catch (Exception ex)
              {
                  this.Dispatcher.BeginInvoke(delegate()
                          {
                              MessageBox.Show(ex.Message);
                          });
              }
          }
  </code></pre>
  
  <p>the wcf function is </p>
  
  <pre><code>[OperationContract]
          [WebInvoke(Method = "POST", UriTemplate = "AddCategoryImage?CategoryId={CategoryId}&amp;ContentType={ContentType}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
          bool AddCategoryImage(int CategoryId, string ContentType, Stream Image);
  </code></pre>
  
  <p>and function implementation is </p>
  
  <pre><code> public static bool AddCategoryImage(int CategoryId, string ContentType,Stream Image)
          {
              SqlConnection sqlCon = new SqlConnection(Params.GetConnectionString());
              SqlCommand sqlCmd = new SqlCommand("UPDATE Categories SET Cat_Img = @Cat_Img,Cat_Img_Type = @Cat_Img_Type WHERE Cat_ID = @Cat_ID", sqlCon);
              sqlCmd.Parameters.Add("@Cat_Img", SqlDbType.Image).Value = Utility.ConvertStreamToByteArray(Image);
              sqlCmd.Parameters.Add("@Cat_Img_Type", SqlDbType.NVarChar, 32).Value = ContentType;
              sqlCmd.Parameters.Add("@Cat_ID", SqlDbType.Int).Value = CategoryId;
  
              try
              {
                  sqlCon.Open();
  
                  return sqlCmd.ExecuteNonQuery() &gt; 0 ? true : false;
              }
              catch (Exception ex)
              {
                  Error.CreateError(ex.Message, "Category.cs", "AddCategoryImg(int,string,Stream)");
              }
              finally
              {
                  sqlCon.Close();
              }
  
              return false;
          }
  </code></pre>