How can I add MyApp.deps.json to the hostfxr initialization sequence? #2613
-
I have the following
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
} and I simply use this code to load the .NET runtime: import pythonnet
path = os.path.join("path", "to", "app", "lib", "dotnet")
sys.path.insert(0, path)
config_path = os.path.join(path, "MyApp.runtimeconfig.json")
pythonnet.load("coreclr", runtime_config=str(config_path))
import clr
clr.AddReference("MyApp") This works fine mostly, however, I noticed that if my app overrides a framework dependency e.g. upgrading After a lot debugging, I realized that this is because the Unfortunately, it seems that
I was able to work around this by doing the following: import pythonnet
path = os.path.join("path", "to", "app", "lib", "dotnet")
sys.path.insert(0, path)
config_path = os.path.join(path, "MyApp.runtimeconfig.json")
runtime = clr_loader.get_coreclr(runtime_config=str(config_path))
pythonnet.set_runtime(runtime)
libPath = os.path.join(path, "MyApp.dll")
assembly = runtime.get_assembly(str(libPath))
print("Loaded " + libPath)
# just invoke an empty function to load the library
func = assembly.get_function("MyApp.Interop.Host.Initialize")
result = func(b"")
assert result == 0, f"Failed to initialize MyApp.dll, error code {result}"
pythonnet.load(runtime)
import clr
clr.AddReference("Python.Runtime")
clr.AddReference("MyApp") now, hostfxr loads the "correct" deps.json file, but I get a Python error whenever I do something that involves someManagedObj.ListProperty = get_python_list()
What am I missing here? Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You are not missing anything, this is simply tricky. .NET Framework didn't have this "problem", and Python.NET was developed against .NET Framework. I'm not sure why the conversions are messed up in your case. Could you see whether this pythonnet/clr-loader#66 helps? It is already merged on |
Beta Was this translation helpful? Give feedback.
You are not missing anything, this is simply tricky. .NET Framework didn't have this "problem", and Python.NET was developed against .NET Framework.
I'm not sure why the conversions are messed up in your case.
Could you see whether this pythonnet/clr-loader#66 helps? It is already merged on
master
, just not released, yet (as I want to integrate it back into theget_coreclr
factory).