upload error: (417) Expectation Failed.

Nov 2, 2013 at 10:29 PM
Hi Maria & Alex,

thank You for providing this nice net wrapper. I'm using it to create a simple command-line tool. I want to be able to upload all my photos in one go. The tool shall create an album for each folder it finds, and upload all the contained photos into this album.

I figured out how to create categories, subcategories and albums, but when it try to upload a file I get this error:
System.AggregateException was unhandled
  HResult=-2146233088
  Message=Mindestens ein Fehler ist aufgetreten.
  Source=mscorlib
  StackTrace:
       bei System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       bei System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       bei System.Threading.Tasks.Task`1.get_Result()
       bei SmugMugModel.ImageUpload.UploadImage(String filename, String MD5Checksum)
       bei SmugMugModel.ImageUpload.UploadImage(String fileName)
       bei UpMug.Uploader.UploadImages(Album album, IEnumerable`1 imageFiles) in d:\VS\Projects\UpMug\UpMug\Uploader.cs:Zeile 177.
       bei UpMug.Uploader.Run() in d:\VS\Projects\UpMug\UpMug\Uploader.cs:Zeile 64.
       bei UpMug.Program.Main(String[] args) in d:\VS\Projects\UpMug\UpMug\Program.cs:Zeile 82.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=Der Remoteserver hat einen Fehler zurückgegeben: (417) Expectation Failed.
       Source=System
       StackTrace:
            bei System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
            bei System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
         --- End of stack trace from previous location where exception was thrown ---
            bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            bei SmugMugModel.ImageUpload.<UploadImageAsync>d__8.MoveNext()
       InnerException: 
Any idea what I am doing wrong? This is my uploader class. Everything works fine until UploadImages (at the bottom) is called and
var img = albumUploader.UploadImage(imageFile.FullName);
throws the exception.
Nov 2, 2013 at 10:30 PM
Thats my code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmugMugModel;

namespace UpMug
{
    public class Uploader
    {
        readonly HashSet<string> ValidFileExtensions = new HashSet<string>(new[] { ".jpg", ".jpeg", ".gif", ".png" });
        MyUser _user;
        Options _options;
        List<Category> _categories;

        public Uploader(MyUser user, Options options)
        {
            _user = user;
            _options = options;
            _categories = _user.GetTree(true);
        }

        public void Run()
        {
            foreach (var categoryDir in _options.LocalDir.GetDirectories())
            {
                Category category;
                try
                {
                    category = GetOrCreateCategory(categoryDir.Name);
                    Console.WriteLine("OK.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("error.");
                    Console.WriteLine(e);
                    continue;
                }

                foreach (var subDir in categoryDir.GetDirectories())
                {
                    var subSubDirs = subDir.GetDirectories();
                    if (subSubDirs.Length == 0)
                    {
                        var albumDir = subDir;
                        var imageFiles = albumDir.GetFiles().Where(f => ValidFileExtensions.Contains(f.Extension.ToLower()));
                        if (imageFiles.ElementAtOrDefault(0)!=null)
                        {
                            Album album;
                            try
                            {
                                album = GetOrCreateAlbum(albumDir.Name, category);
                                Console.WriteLine("OK.");
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("error.");
                                Console.WriteLine(e);
                                continue;
                            }

                            UploadImages(album, imageFiles);
                        }
                        else
                        {
                            Console.WriteLine("Ignoring empty directory \"" + albumDir.Name + "\".");
                        }
                        continue;
                    }

                    var subCategoryDir = subDir;

                    SubCategory subcategory;
                    try
                    {
                        subcategory = GetOrCreateSubCategory(subCategoryDir.Name, category);
                        Console.WriteLine("OK.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("error.");
                        Console.WriteLine(e);
                        continue;
                    }

                    foreach (var albumDir in subCategoryDir.GetDirectories())
                    {
                        var imageFiles = albumDir.GetFiles().Where(f => ValidFileExtensions.Contains(f.Extension.ToLower()));
                        if (imageFiles.ElementAtOrDefault(0) != null)
                        {
                            Album album;
                            try
                            {
                                album = GetOrCreateAlbum(albumDir.Name, subcategory);
                                Console.WriteLine("OK.");
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("error.");
                                Console.WriteLine(e);
                                continue;
                            }

                            UploadImages(album, imageFiles);
                        }
                        else
                        {
                            Console.WriteLine("Ignoring empty directory \"" + albumDir.Name + "\".");
                        }
                    }
                }

            }
        }

        Category GetOrCreateCategory(string categoryName)
        {
            var category = _categories.FirstOrDefault(c=>(c.Name == categoryName));
            if (category != null)
            {
                Console.Write("Existing category \"" + categoryName +"\"...");
                return category;
            }
            Console.Write("Creating new category \"" + categoryName + "\"...");
            return _user.CreateCategory(categoryName);
        }


        SubCategory GetOrCreateSubCategory(string subCategoryName, Category category)
        {
            var subCategory = category.SubCategories.FirstOrDefault(sc => (sc.Name == subCategoryName));
            if (subCategory != null)
            {
                Console.Write("Existing subcategory \"" + subCategoryName + "\"...");
                return subCategory;
            }
            Console.Write("Creating new subcategory \"" + subCategoryName + "\"...");
            return category.CreateSubCategory(subCategoryName);
        }


        Album GetOrCreateAlbum(string albumTitle, Category category)
        {
            var album = category.Albums.FirstOrDefault(a => a.Title==albumTitle);
            if (album != null)
            {
                Console.Write("Existing album \"" + albumTitle + "\"...");
                return album;
            }
            Console.Write("Creating new album \"" + albumTitle + "\"...");
            return category.CreateAlbum(albumTitle);
        }

        Album GetOrCreateAlbum(string albumTitle, SubCategory subcategory)
        {
            var album = subcategory.Albums.FirstOrDefault(a => a.Title == albumTitle);
            if (album != null)
            {
                Console.Write("Existing album \"" + albumTitle + "\"...");
                return album;
            }
            Console.Write("Creating new album \"" + albumTitle + "\"...");
            return subcategory.CreateAlbum(albumTitle);
        }



        void UploadImages(Album album, IEnumerable<FileInfo> imageFiles)
        {
            var albumUploader = album.CreateUploader();
            foreach (var imageFile in imageFiles)
            {
                Console.Write("Uploading image \"" + imageFile.Name + "\"...");
                try
                {
                    var img = albumUploader.UploadImage(imageFile.FullName);
                    Console.WriteLine("OK.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("error.");
                    Console.WriteLine(e);
                }
            }
        }
    }
}
Nov 3, 2013 at 7:36 PM
Edited Nov 3, 2013 at 7:36 PM
Well, Google came up with an answer. I had to add
ServicePointManager.Expect100Continue = false;
now everything works fine.
I don't have a clue what this is about - well, I'm happy! =) Uploading....

wef.