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;
}
}
}
}
c# excel-dna
New contributor
add a comment |
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;
}
}
}
}
c# excel-dna
New contributor
add a comment |
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;
}
}
}
}
c# excel-dna
New contributor
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
c# excel-dna
New contributor
New contributor
New contributor
asked 12 hours ago
Alpagas
11
11
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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