Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Function with params #25

Open
cubewise-tryan opened this issue Aug 28, 2019 · 2 comments
Open

Async Function with params #25

cubewise-tryan opened this issue Aug 28, 2019 · 2 comments

Comments

@cubewise-tryan
Copy link

Hi,

I am trying to do a POC creating a function that allows an asynchronous call with unlimited number of arguments. When adding async Task as the return type the fixed arguments in the function are always empty and only the params are populated. If I return object the fixed arguments are available.

Not sure if I am doing something incorrectly or there is a bug.

Sample code below:

using System;
using System.Net;
using System.Diagnostics;

using ExcelDna.Integration;
using ExcelDna.Registration;
using ExcelDna.Logging;

namespace test
{
   public class AddIn : IExcelAddIn
   {

      public void AutoOpen()
      {
         ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

         Trace.Listeners.Add(new LogDisplayTraceListener());

         ExcelRegistration.GetExcelFunctions()
                          .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false)
                          .ProcessParamsRegistrations()
                          .RegisterFunctions();
      }

      public void AutoClose()
      {

      }

   }

}
using System;
using System.Threading.Tasks;
using System.Diagnostics;
using ExcelDna.Integration;

namespace test
{
   public static class MyFunctions
   {
      [ExcelFunction(Description = "Returns a value")]
      public static async Task<object> NotWorking([ExcelArgument(Name = "Arg1", Description = "Arg 1")] string arg1, 
         [ExcelArgument(Name = "Argument", Description = "Arg 2, Arg 3, etc")]params string[] args)
      {
         try {
            var result = arg1 + ", " + String.Join(", ", args);
            Trace.TraceInformation(result);
            Trace.Flush();
            return result;
         } catch (Exception ex){
            Trace.TraceError(ex.Message);
            Trace.Flush();
            return ex.Message;
         }
         
      }

      [ExcelFunction(Description = "Returns a value")]
      public static object Working([ExcelArgument(Name = "Arg1", Description = "Arg 1")] string arg1, 
         [ExcelArgument(Name = "Argument", Description = "Arg 2, Arg 3, etc")]params string[] args)
      {
         try {
            var result = arg1 + ", " + String.Join(", ", args);
            Trace.TraceInformation(result);
            Trace.Flush();
            return result;
         } catch (Exception ex){
            Trace.TraceError(ex.Message);
            Trace.Flush();
            return ex.Message;
         }
         
      }


   }

}

@govert
Copy link
Member

govert commented Aug 28, 2019

@cubewise-tryan With your code I get the same buggy behaviour.
You can work around it by changing the order in which you do the transformations, putting the Params transform before the Task<T> transform:

         ExcelRegistration.GetExcelFunctions()
                          .ProcessParamsRegistrations()
                          .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false)
                          .RegisterFunctions();

This is a quirk that I need to fix or document somewhere, so let's keep the issue open for now.

@cubewise-tryan
Copy link
Author

Thanks, it works. I should have tried that out myself.

@govert govert closed this as completed Oct 6, 2021
@govert govert reopened this Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants