Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.6k views
in .NET FTP by (220 points)
I'm trying to write code around the asynchronous methods based on the TAP pattern with async and await in .NET 4.5.

For example
   public Task ConnectAsync()
        {
            return Task.Factory.FromAsync(secureFtp.BeginConnect, secureFtp.EndConnect, secureFtp);
            
        }


That works, except I do not get any errors when it cannot connect. I'm unsure if it is from my unfamiliarity with the new TAP pattern, or asynchronous for that matter, or if the edt dll eats the error.

I do get an error if I subscribe to the error event on the SecureFTPConnection but that seems like mixing async models. I believe this may be my unfamiliarity with the new pattern and how it is supposed to mesh with the APM pattern.

2 Answers

0 votes
by (51.6k points)
I just wrote this little test program:
using System;
using System.Threading.Tasks;

using EnterpriseDT.Net.Ftp;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SecureFTPConnection ftp = new SecureFTPConnection();
                ftp.ServerAddress = "wrongserveraddress";
                ftp.UserName = "wrongusername";
                ftp.Password = "wrongpassword";

                TaskFactory factory = new TaskFactory();
                Task t = factory.FromAsync(ftp.BeginConnect, ftp.EndConnect, ftp);
                t.Wait();
            }
            catch (AggregateException ex)
            {
                Console.Error.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message + "\n" + ex.InnerException.StackTrace);
            }
            Console.ReadLine();
        }
    }
}

and it worked fine. The exception was thrown from t.Wait() and caught by the exception-handler. The expected SocketException was available via the AggregateException's InnerException property.

- Hans (EnterpriseDT)
0 votes
by (220 points)
Thanks.

That is very similar to my code. I found out after posting this that the async/await stuff does not really work in console programs. Which is what I was writing to test it and for a quick solution. The magic is the Wait(). Even when I had try/catch around the code it was never caught. async and a void method don't mix.

Categories

...