[en] Need Help for run Async Methods (solved)

Topics: Technical Support
May 20, 2015 at 11:18 AM
Hi,

for learning purposes i use Zyan as Server for my Windows Form GUI.
I'm trying now for add async functionality to my application, so users can cancel long running sql queries, or run more than one query at once.

but seems i'm to stupid for do this... perhaps you have some tipps for me or any simple example what i must do for run async methods.

For Example, when i connect to Zyan i would check if SQL can open or is running, i try this:

in my WinGui:
private async void ConnectToZyan()
{
     cts = new CancellationTokenSource();
     var cancellationToken = cts.Token;

     var someTask = Task.Run(() =>
     {
           ConnectToZyan(userNameTextBox.Text, passwordTextBox.Text);
     }, cancellationToken);

      try
      {
            await someTask;
            var checkSql = await SqlConnectionExistsAsync();
      }
      catch (Exception ex)
      {
      }
}
public async Task<bool> SqlConnectionExistsAsync()
        {
            try
            {
                return await interfaceProxy.SqlConnectionExistsAsync();
                //try this too
               // var myVar = await Task.Run(() => interfaceProxy.SqlConnectionExistsAsync());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ex " + ex.Message);
            }
            return false;            
}
My Interface:
public interface ILogonService : IDisposable
{
     User LogOn(string accountName, string password);
}

public interface ILogonServiceAsync : ILogonService
 {
        Task<User> LogOnAsync(string accountName, string password);
        Task<bool> SqlConnectionExistsAsync();
 }
my Service:
 [Serializable]
public class LogOnService : ILogonService, ILogonServiceAsync,
{
     public async Task<bool> SqlConnectionExistsAsync()
     {
     string connString = ConfigurationManager.ConnectionStrings["ExampleConnection"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connString))
            {
                try
                {
                    await connection.OpenAsync();
                    if (connection.State == ConnectionState.Open)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                catch (SqlException ex)
                {
                    return false;
                }
            }
        }
}
In Zyan program i register Components:
catalog1.RegisterComponent<ILogonService, LogOnService>();
// Async
catalog1.RegisterComponent<ILogonServiceAsync, LogOnService>();
I Get follow error:
Exception Typ "System.Threading.Tasks.Task`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" in Assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ist nicht als serialisierbar gekennzeichnet. (is not Serialisable)
i Think all my construct is false, so if you have little Example for me how i can use Zyan with Async , this would make me much happy .

Thanks.
peter
Coordinator
May 20, 2015 at 1:51 PM
Edited May 20, 2015 at 1:55 PM
Hi Peter,
  • Use async methods on the client side only.
  • Don't use Task types in your interfaces, they are not serializable anyway.
  • Whenever you call a server method, wrap the call with Task.Run().
Just as simple as that.

Here is an example:
// note that server component interface is synchronous
public interface ISampleService
{
    string GeneratePassword(int length);
}

// and client code is asynchronous
using (var conn = await Task.Run(() => new ZyanConnection(url, protocol)))
{
    var proxy = conn.CreateProxy<ISampleService>();

    var password = await Task.Run(() => proxy.GeneratePassword(length));
    Console.WriteLine("Server generated password: {0}", password);
}
The complete code for this example is located here:
https://gist.github.com/yallie/7d263d3a44c0c0936a7e

Regards, Alex
Marked as answer by yallie on 5/21/2015 at 8:13 AM
May 20, 2015 at 2:15 PM
Hi Alex,

thanks for the Reply and Example.

i would try this, Async only on the Client Side...

i was confused about this Posting here and the Asynchronous interface
Coordinator
May 20, 2015 at 7:32 PM
That was a proposal, a sketch for a new feature, never implemented.
I came to the conclusion that there is a better way of building asynchronous apps on Zyan.
Try my example code and let me know if it helps.

Cheers, Alex
May 21, 2015 at 3:24 PM
Hi Alex,

i tried the Example, and i think this works for me.

Thank you very much

peter
Coordinator
May 21, 2015 at 4:14 PM
You're welcome!

— Alex