views:

761

answers:

4

I have a Silverlight 2 app that sends a byte array to a Silverlight-enabled WCF service. However, (unless I try to upload a .txt file) the service's SaveFile() method is never reached and I get an error: "The remote server returned an error: NotFound"

Am I missing something really obvious? Why can't I upload .doc files? Why only .txt?

UPDATE: I've tried attaching debugger for CLR exceptions, but that didn't help. I am still not able to see any errors besides the 404. Here's my code:

in Page.xaml.cs:

OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShowDialog().Value)
{
  byte[] fileContent = new byte[dlg.File.Length];

  using (FileStream fs = dlg.File.OpenRead())
  {
    fs.Read(fileContent, 0, Convert.ToInt32(fs.Length));
    fs.Close();
  }

  Service1.Service1Client srv = new SL1.Service1.Service1Client();

  srv.SaveFileCompleted += (sender1, e1) =>
  {
    foo.Text = "Uploaded!";
  };

  srv.SaveFileAsync(dlg.File.Name, fileContent);
}

in Service1.svc.cs:

[OperationContract]
public string SaveFile(string fileName, byte[] fileContent)
{
  string ret = String.Empty;

  try
  {
    string target = @"c:\debug123\" + fileName;

    if (File.Exists(target))
    {
      File.Delete(target);
    }

    File.WriteAllBytes(target, fileContent);

    ret = "OK";
  }
  catch (Exception ex)
  {
    ret = ex.ToString();
  }

  return ret;
}

Anyone see anything wrong with this?

+1  A: 

I'm not sure what the specific issue is, but I can tell you that the uploads are definitely not limited to .txt files - I've uploaded 10MB WMV files without problem. Perhaps there's something with the encoding on the service side that's causing the issue?

Jon Galloway
+1  A: 

There is no such limitation.

Attach a debugger to the service and check which exception is raised. To do this, you need to set Visual Studio to break on exceptions when they are thown (as opposed to unhandled). Do this in the Debug->Exceptions dialog by checking the left checkbox on the "Common Language Runtime Exceptions" row.

By attaching a debugger to the service (Debug->Attach to process), you will be able to see the real error. The NotFound error is a generic error - the real error is visible only on the service side.

Sander
Thanks, I'll give that a shot and report back.
Kon
I tried to have VS break on CLR exceptions, but that didn't change anything - still getting the 'not found' error message. Very strange... if I upload *.txt all works fine and hits the breakpoint in the web service method. But doesn't like .doc, .jpg, or .mp3. :(
Kon
+2  A: 

FYI, I figured out my problem and this article solved it. "By default the largest message that can be sent to a service from the client is 8124 bytes." So I had to increase the limit via the bindings config settings.

But now my main issue is how to get an appropriate error message, instead of the 404. I'll have to research that some more, but hope someone else can come up with a good answer (unfortunately breaking on CLR exceptions didn't help me).

UPDATE: Upon further reading of the same article, I discovered that the WCF Service Configuration Utility that is included in the Windows SDK can help determine the underlying problem.

Kon