UploadClient "NotFound" error

Nov 22, 2013 at 7:14 PM
Hello, I've been working on a Silverlight 5 project for a while, now my client wants to have the ability to upload and download files so I searched the web and found Interlink but I'm unable to make it work, when I call UploadClient.UploadDataAsync the DownloadComplete event is called with a "NotFound" error.
This is my web.config:
<appSettings>
                <add key="UploadHandler" value="MedicBase.Interlink.UploadHandler, MedicBase" />
        <add key="DownloadHandler" value="MedicBase.Interlink.DownloadHandler, MedicBase" />
    </appSettings>
<system.webServer>
       <handlers>
     <add name="FileDownload" verb="GET,POST" path="FileDownload.ashx" type="HSS.Interlink.Web.FileDownloadAsync, HSS.Interlink.Web" />
     <add name="FileUpload" verb="GET,POST" path="FileUpload.ashx" type="HSS.Interlink.Web.FileUploadAsync, HSS.Interlink.Web" />
</handlers>
    </system.webServer>
The code I'm using lo upload files is like this:
On my ViewModel constructor I have this:
//HSSInterlink
            string url = Application.Current.Host.Source.GetComponents(UriComponents.Scheme |
                                                                       UriComponents.HostAndPort,
                                                                       UriFormat.Unescaped);
            this.uploader = new UploadClient();
            this.uploader.Initialize(url + "/FileUpload.ashx", "UploadHandler", false);
            this.uploader.UploadCompleted += uploader_UploadCompleted;
and this is the code I have to upload the selected file:
AttachmentData data = (AttachmentData)op.UserState;
                string metadata = JsonConvert.SerializeObject(new AttachmentModel
                    {
                        EntityId = ((ConsultationAppointmentModel)CurrentAppointmentView.CurrentItem).ConsultationAppointmentId,
                        FileName = data.docName,
                        FileDescription = "Descripción de prueba",
                        Id = Guid.NewGuid(),
                        Kind = AttachmentKind.ConsultationAttachment,
                        TmpFileName = data.tempDocName
                    });
                Uploader.UploadDataAsync(data.documentContent, data.docName, metadata);
Thank you very much in advance.
Coordinator
Nov 23, 2013 at 6:08 PM
I'm trying to review your code but not sure what pieces are running where.

Do you have an UploadHandler implemented t receive your file?
Nov 25, 2013 at 12:43 PM
Thanks for your response. Yes, I have an Uploadhandler implemented but execution is not getting there, I'm receiving the error right after I call UploaddataAsync. I've put break points all over the upload handler and none is hit. This is the code for my upload handler:
public class UploadHandler : BaseUploadHandler
    {
        private static string tempPath = Path.GetTempPath();

        public string GetFileName()
        {
            return Path.Combine(tempPath, this.FileName);
        }

        public override Response AppendToFile(byte[] buffer)
        {
            string file = GetFileName();
            bool error = false;
            if (File.Exists(file))
            {
                try
                {
                    using (FileStream stream = File.Open(file, FileMode.Append))
                    {
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex, "UploadHandler -> AppendToFile " + this.FileName);
                    error = true;
                }
                return !error ? Response.Success : Response.FatalError;
            }
            else
            {
                return Response.FileDoesNotExist;
            }
        }

        public override void CancelUpload()
        {
            try
            {
                if (File.Exists(GetFileName()))
                {
                    File.Delete(GetFileName());
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "UploadHandler -> Cancel " + GetFileName());
            }
        }

        public override bool CheckFileExists()
        {
            return File.Exists(GetFileName());
        }

        public override void CreateNewFile()
        {
            if (File.Exists(GetFileName()))
            {
                File.Delete(GetFileName());
            }
            File.Create(GetFileName()).Close();
        }

        public override void OnError(ref Exception ex)
        {
            Logger.Log(ex, "UploadHandler -> " + this.FileName);
            try
            {
                if (File.Exists(GetFileName()))
                {
                    File.Delete(GetFileName());
                }
            }
            catch { }
        }

        public override string UploadComplete()
        {
            AttachmentModel model = JsonConvert.DeserializeObject<AttachmentModel>(this.Metadata);
            using (MedicDbContext db = new MedicDbContext())
            {
                switch (model.Kind)
                {
                    case AttachmentKind.ConsultationAttachment:
                        ConsultationAttachmentEntity entity =
                            db.ConsultationAttachmentEntities.Add(new ConsultationAttachmentEntity
                                {
                                    AppointmentId = model.EntityId,
                                    FileDescription = model.FileDescription,
                                    FileName = model.FileName,
                                    Id = model.Id
                                });
                        entity.Content = new AttachmentContentEntity(File.ReadAllBytes(GetFileName()));
                        break;
                    case AttachmentKind.PatientAttachment:
                        break;
                    default:
                        throw new NotImplementedException("Unknown attachment kind");
                        break;
                }

                db.SaveChanges();

            }

            return AppointmentDataResources.UploadAttachmentSuccess;
        }

        public override bool IsAuthorized()
        {
            if (this.Context.User.Identity.IsAuthenticated)
            {
                if (this.Context.User.IsInRole(MedicBase.Constants.Roles.Constants.Administrator) ||
                    this.Context.User.IsInRole(MedicBase.Constants.Roles.Constants.ClinicAdministrator) ||
                    this.Context.User.IsInRole(MedicBase.Constants.Roles.Constants.MedicalTechnician) ||
                    this.Context.User.IsInRole(MedicBase.Constants.Roles.Constants.Physician))
                {
                    return true;
                }
            }
            return false;
        }
    }
Coordinator
Nov 25, 2013 at 6:42 PM
If you are running this from the vs ide you need to either debug using IIS express or include the handlers section of the web config in the system.web - system.webserver is for IIS integrated pipeline mode only.
Nov 26, 2013 at 12:54 PM
Thanks again for your answer. I'm currently using VS2012 and IIS Express.
I'm not sure if the whole config file would be of any help if so, please let me know.
Coordinator
Dec 2, 2013 at 6:39 PM
to confirm, you're using v3.0?

also, what is your client platform?
Dec 3, 2013 at 8:59 PM
Yes, I'm using v3.0 and my client platform is Silverlight5 (on IE9 and IE10), I´m also using other handlers like Elmah.
Coordinator
Dec 4, 2013 at 1:06 PM
OK, I updated the test project to include a "no-ui" main page to mimic your scenario. I've tested in both VS2012 and VS2013 and it worked as expected. The only piece I couldn't replicate is your custom 'AttachmentData' data object and serialization so maybe that's the culprit.

Take a look at the update source code and maybe tweak the test project to perform your custom serialization and let me know.
Dec 5, 2013 at 12:21 PM
Ok, thanks a lot