Accessing static data from a runtime compiled function in Excel DNA











up vote
0
down vote

favorite












I scratching my head on this issue. I build a plugin architecture with Excel DNA, and I want to compile new functions at runtime and register them in Excel DNA.
It works perfectly fine until I try to access static data in the new functions context. My static data Toto is null.



When adding some logs, I discovered that the static constructor of RuntimeCompiler was called twice.
It seems that new functions are running in a different memory space. However, when I log the Appdomain Id, in the function and outside, it seems to be the same.



The code is working perfectly well in a unit test. So that is why I think it is related with the Excel/Excel DNA memory management.



How can I share the same memory space with my new loaded functions ?



Has anyone have already experienced this issue. ?



Thanks a lot



alpaga



namespace  test.xl.Loader
{

public static class RuntimeCompiler
{

public static string Toto { get; set; }

public static void CreateAndRegisterFunctions()
{
try
{
var wrapperClassPath = CreateWrapperClassFromServiceDefinition();

if (wrapperClassPath == null) return;

var assemblyPath = CompileExecutable(wrapperClassPath);

if (assemblyPath == null) return;

var methodsToRegister = GetMethodsFromAssembly(assemblyPath);

if (methodsToRegister == null) return;

ExcelAsyncUtil.QueueAsMacro(() => Integration.RegisterMethods(methodsToRegister));

}
catch (Exception e)
{
_logger.ErrorFormat(e.Message);
}
}

private static string CreateWrapperClassFromServiceDefinition()
{
try
{

Toto = "lol";

var generatedClass = @"

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using test.xl.Loader;

namespace A
{
public class Wrapper
{
public static object FlGetToto(string arg)
{
return test.xl.Loader.RuntimeCompiler.Toto;
}
}
}
";

var tmp = Path.GetTempFileName()+".cs";

using (var file = new StreamWriter(tmp))
{
file.Write(generatedClass);
}

return tmp;

}
catch (Exception e)
{
_logger.Error($"Fail while creating Wrapper Class : {e.Message}");
return null;
}
}

private static List<MethodInfo> GetMethodsFromAssembly(string assemblyPath)
{
try
{
var dllName = Path.GetFileName(assemblyPath);

var assemblyFullName = AssemblyName.GetAssemblyName($".\ {dllName}");

var assembly = Assembly.Load(assemblyFullName);

AppDomain.CurrentDomain.Load(assembly.GetName());

Type myType = assembly.GetType("A.Wrapper");

return myType.GetMethods().ToList().Where(x => x.Module.Name == dllName).ToList();
}
catch (Exception e)
{
_logger.Error($"Fail while getting methods from Assembly : {e.Message}");
return null;
}
}
}
}









share|improve this question







New contributor




Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I scratching my head on this issue. I build a plugin architecture with Excel DNA, and I want to compile new functions at runtime and register them in Excel DNA.
    It works perfectly fine until I try to access static data in the new functions context. My static data Toto is null.



    When adding some logs, I discovered that the static constructor of RuntimeCompiler was called twice.
    It seems that new functions are running in a different memory space. However, when I log the Appdomain Id, in the function and outside, it seems to be the same.



    The code is working perfectly well in a unit test. So that is why I think it is related with the Excel/Excel DNA memory management.



    How can I share the same memory space with my new loaded functions ?



    Has anyone have already experienced this issue. ?



    Thanks a lot



    alpaga



    namespace  test.xl.Loader
    {

    public static class RuntimeCompiler
    {

    public static string Toto { get; set; }

    public static void CreateAndRegisterFunctions()
    {
    try
    {
    var wrapperClassPath = CreateWrapperClassFromServiceDefinition();

    if (wrapperClassPath == null) return;

    var assemblyPath = CompileExecutable(wrapperClassPath);

    if (assemblyPath == null) return;

    var methodsToRegister = GetMethodsFromAssembly(assemblyPath);

    if (methodsToRegister == null) return;

    ExcelAsyncUtil.QueueAsMacro(() => Integration.RegisterMethods(methodsToRegister));

    }
    catch (Exception e)
    {
    _logger.ErrorFormat(e.Message);
    }
    }

    private static string CreateWrapperClassFromServiceDefinition()
    {
    try
    {

    Toto = "lol";

    var generatedClass = @"

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using test.xl.Loader;

    namespace A
    {
    public class Wrapper
    {
    public static object FlGetToto(string arg)
    {
    return test.xl.Loader.RuntimeCompiler.Toto;
    }
    }
    }
    ";

    var tmp = Path.GetTempFileName()+".cs";

    using (var file = new StreamWriter(tmp))
    {
    file.Write(generatedClass);
    }

    return tmp;

    }
    catch (Exception e)
    {
    _logger.Error($"Fail while creating Wrapper Class : {e.Message}");
    return null;
    }
    }

    private static List<MethodInfo> GetMethodsFromAssembly(string assemblyPath)
    {
    try
    {
    var dllName = Path.GetFileName(assemblyPath);

    var assemblyFullName = AssemblyName.GetAssemblyName($".\ {dllName}");

    var assembly = Assembly.Load(assemblyFullName);

    AppDomain.CurrentDomain.Load(assembly.GetName());

    Type myType = assembly.GetType("A.Wrapper");

    return myType.GetMethods().ToList().Where(x => x.Module.Name == dllName).ToList();
    }
    catch (Exception e)
    {
    _logger.Error($"Fail while getting methods from Assembly : {e.Message}");
    return null;
    }
    }
    }
    }









    share|improve this question







    New contributor




    Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I scratching my head on this issue. I build a plugin architecture with Excel DNA, and I want to compile new functions at runtime and register them in Excel DNA.
      It works perfectly fine until I try to access static data in the new functions context. My static data Toto is null.



      When adding some logs, I discovered that the static constructor of RuntimeCompiler was called twice.
      It seems that new functions are running in a different memory space. However, when I log the Appdomain Id, in the function and outside, it seems to be the same.



      The code is working perfectly well in a unit test. So that is why I think it is related with the Excel/Excel DNA memory management.



      How can I share the same memory space with my new loaded functions ?



      Has anyone have already experienced this issue. ?



      Thanks a lot



      alpaga



      namespace  test.xl.Loader
      {

      public static class RuntimeCompiler
      {

      public static string Toto { get; set; }

      public static void CreateAndRegisterFunctions()
      {
      try
      {
      var wrapperClassPath = CreateWrapperClassFromServiceDefinition();

      if (wrapperClassPath == null) return;

      var assemblyPath = CompileExecutable(wrapperClassPath);

      if (assemblyPath == null) return;

      var methodsToRegister = GetMethodsFromAssembly(assemblyPath);

      if (methodsToRegister == null) return;

      ExcelAsyncUtil.QueueAsMacro(() => Integration.RegisterMethods(methodsToRegister));

      }
      catch (Exception e)
      {
      _logger.ErrorFormat(e.Message);
      }
      }

      private static string CreateWrapperClassFromServiceDefinition()
      {
      try
      {

      Toto = "lol";

      var generatedClass = @"

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Linq;
      using System.Threading.Tasks;
      using test.xl.Loader;

      namespace A
      {
      public class Wrapper
      {
      public static object FlGetToto(string arg)
      {
      return test.xl.Loader.RuntimeCompiler.Toto;
      }
      }
      }
      ";

      var tmp = Path.GetTempFileName()+".cs";

      using (var file = new StreamWriter(tmp))
      {
      file.Write(generatedClass);
      }

      return tmp;

      }
      catch (Exception e)
      {
      _logger.Error($"Fail while creating Wrapper Class : {e.Message}");
      return null;
      }
      }

      private static List<MethodInfo> GetMethodsFromAssembly(string assemblyPath)
      {
      try
      {
      var dllName = Path.GetFileName(assemblyPath);

      var assemblyFullName = AssemblyName.GetAssemblyName($".\ {dllName}");

      var assembly = Assembly.Load(assemblyFullName);

      AppDomain.CurrentDomain.Load(assembly.GetName());

      Type myType = assembly.GetType("A.Wrapper");

      return myType.GetMethods().ToList().Where(x => x.Module.Name == dllName).ToList();
      }
      catch (Exception e)
      {
      _logger.Error($"Fail while getting methods from Assembly : {e.Message}");
      return null;
      }
      }
      }
      }









      share|improve this question







      New contributor




      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I scratching my head on this issue. I build a plugin architecture with Excel DNA, and I want to compile new functions at runtime and register them in Excel DNA.
      It works perfectly fine until I try to access static data in the new functions context. My static data Toto is null.



      When adding some logs, I discovered that the static constructor of RuntimeCompiler was called twice.
      It seems that new functions are running in a different memory space. However, when I log the Appdomain Id, in the function and outside, it seems to be the same.



      The code is working perfectly well in a unit test. So that is why I think it is related with the Excel/Excel DNA memory management.



      How can I share the same memory space with my new loaded functions ?



      Has anyone have already experienced this issue. ?



      Thanks a lot



      alpaga



      namespace  test.xl.Loader
      {

      public static class RuntimeCompiler
      {

      public static string Toto { get; set; }

      public static void CreateAndRegisterFunctions()
      {
      try
      {
      var wrapperClassPath = CreateWrapperClassFromServiceDefinition();

      if (wrapperClassPath == null) return;

      var assemblyPath = CompileExecutable(wrapperClassPath);

      if (assemblyPath == null) return;

      var methodsToRegister = GetMethodsFromAssembly(assemblyPath);

      if (methodsToRegister == null) return;

      ExcelAsyncUtil.QueueAsMacro(() => Integration.RegisterMethods(methodsToRegister));

      }
      catch (Exception e)
      {
      _logger.ErrorFormat(e.Message);
      }
      }

      private static string CreateWrapperClassFromServiceDefinition()
      {
      try
      {

      Toto = "lol";

      var generatedClass = @"

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Linq;
      using System.Threading.Tasks;
      using test.xl.Loader;

      namespace A
      {
      public class Wrapper
      {
      public static object FlGetToto(string arg)
      {
      return test.xl.Loader.RuntimeCompiler.Toto;
      }
      }
      }
      ";

      var tmp = Path.GetTempFileName()+".cs";

      using (var file = new StreamWriter(tmp))
      {
      file.Write(generatedClass);
      }

      return tmp;

      }
      catch (Exception e)
      {
      _logger.Error($"Fail while creating Wrapper Class : {e.Message}");
      return null;
      }
      }

      private static List<MethodInfo> GetMethodsFromAssembly(string assemblyPath)
      {
      try
      {
      var dllName = Path.GetFileName(assemblyPath);

      var assemblyFullName = AssemblyName.GetAssemblyName($".\ {dllName}");

      var assembly = Assembly.Load(assemblyFullName);

      AppDomain.CurrentDomain.Load(assembly.GetName());

      Type myType = assembly.GetType("A.Wrapper");

      return myType.GetMethods().ToList().Where(x => x.Module.Name == dllName).ToList();
      }
      catch (Exception e)
      {
      _logger.Error($"Fail while getting methods from Assembly : {e.Message}");
      return null;
      }
      }
      }
      }






      c# excel-dna






      share|improve this question







      New contributor




      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 12 hours ago









      Alpagas

      11




      11




      New contributor




      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Alpagas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Alpagas is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349357%2faccessing-static-data-from-a-runtime-compiled-function-in-excel-dna%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Alpagas is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Alpagas is a new contributor. Be nice, and check out our Code of Conduct.













          Alpagas is a new contributor. Be nice, and check out our Code of Conduct.












          Alpagas is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349357%2faccessing-static-data-from-a-runtime-compiled-function-in-excel-dna%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin