Title: How can I add MyApp.deps.json to the hostfxr initialization sequence? · pythonnet/pythonnet · Discussion #2613 · GitHub
Open Graph Title: How can I add MyApp.deps.json to the hostfxr initialization sequence? · pythonnet/pythonnet · Discussion #2613
X Title: How can I add MyApp.deps.json to the hostfxr initialization sequence? · pythonnet/pythonnet · Discussion #2613
Description: How can I add MyApp.deps.json to the hostfxr initialization sequence?
Open Graph Description: I have the following runtimeconfig.json: { "runtimeOptions": { "tfm": "net8.0", "framework": { "name": "Microsoft.NETCore.App", "version": "8.0.0" }, "configProperties": { "System.Runtime.Serializa...
X Description: I have the following runtimeconfig.json: { "runtimeOptions": { "tfm": "net8.0", "framework": { "name": "Microsoft.NETCore.App", "ver...
Opengraph URL: https://github.com/pythonnet/pythonnet/discussions/2613
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"How can I add MyApp.deps.json to the hostfxr initialization sequence?","text":"I have the following
\nruntimeconfig.json:
\n{\n \"runtimeOptions\": {\n \"tfm\": \"net8.0\",\n \"framework\": {\n \"name\": \"Microsoft.NETCore.App\",\n \"version\": \"8.0.0\"\n },\n \"configProperties\": {\n \"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization\": false\n }\n }\n}\nand I simply use this code to load the .NET runtime:
\n import pythonnet\n\n path = os.path.join(\"path\", \"to\", \"app\", \"lib\", \"dotnet\")\n sys.path.insert(0, path)\n config_path = os.path.join(path, \"MyApp.runtimeconfig.json\")\n pythonnet.load(\"coreclr\", runtime_config=str(config_path))\n\n import clr\n\n clr.AddReference(\"MyApp\")
\nThis works fine mostly, however, I noticed that if my app overrides a framework dependency e.g. upgrading System.Collections.Immutable from version 8.0.0 (provided by the framework) to 9.0.0 (provided via NuGet, but still compatible with net8.0), my application starts crashing, because the SCI version 9.0.0 cannot be loaded.
\nAfter a lot debugging, I realized that this is because the MyApp.deps.json is not considered when first initializing hostfxr therefore it only ever loads the TPA version provided by the framework. See the host.log attachment for details.
\nUnfortunately, it seems that hostfxr is considering Python.Runtime.dll the \"main assembly\" as can be seen here
\nCoreCLR path = 'C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\coreclr.dll', CoreCLR dir = 'C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\'\nLoaded library from C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\coreclr.dll\n--- Invoked hostpolicy [version: 8.0.4 @Commit: 2d7eea252964e69be94cb9c847b371b23e4dd470] corehost_resolve_component_dependencies = {\n Component main assembly path: C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.dll\n}\n-- arguments_t: app_root='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\' deps='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.deps.json' mgd_app='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.dll'\n
\nI was able to work around this by doing the following:
\n import pythonnet\n\n path = os.path.join(\"path\", \"to\", \"app\", \"lib\", \"dotnet\")\n sys.path.insert(0, path)\n config_path = os.path.join(path, \"MyApp.runtimeconfig.json\")\n\n runtime = clr_loader.get_coreclr(runtime_config=str(config_path))\n \n pythonnet.set_runtime(runtime) \n\n libPath = os.path.join(path, \"MyApp.dll\")\n assembly = runtime.get_assembly(str(libPath))\n\n print(\"Loaded \" + libPath)\n\n # just invoke an empty function to load the library\n func = assembly.get_function(\"MyApp.Interop.Host.Initialize\")\n\n result = func(b\"\")\n assert result == 0, f\"Failed to initialize MyApp.dll, error code {result}\"\n \n pythonnet.load(runtime)\n\n import clr\n\n clr.AddReference(\"Python.Runtime\")\n clr.AddReference(\"MyApp\")
\nnow, hostfxr loads the \"correct\" deps.json file, but I get a Python error whenever I do something that involves list or dict types:
\nsomeManagedObj.ListProperty = get_python_list()
\n\nTypeError: 'list' value cannot be converted to System.Collections.Generic.IReadOnlyList`1[System.String]
\n
\nWhat am I missing here? Thank you very much!
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You are not missing anything, this is simply tricky. .NET Framework didn't have this \"problem\", and Python.NET was developed against .NET Framework.
\nI'm not sure why the conversions are messed up in your case.
\nCould 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 the get_coreclr factory).
","upvoteCount":1,"url":"https://github.com/pythonnet/pythonnet/discussions/2613#discussioncomment-14219614"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:ffc95c0f-3375-70f4-1bbe-d191b8ac30c8 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | C106:3DC7AA:F8B09B:15254B9:696FFDE3 |
| html-safe-nonce | 33491bb167d10e49aa3db610a29a4d39beb3d77d818452d15d11012a16ffb760 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTA2OjNEQzdBQTpGOEIwOUI6MTUyNTRCOTo2OTZGRkRFMyIsInZpc2l0b3JfaWQiOiI2MDQwMjQ3NDc5MjA0NTc2NzM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 86e750d0b15373f9e1ed5176d18b19a354350d46678b24fc210c6342061509eb |
| hovercard-subject-tag | discussion:8801905 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/pythonnet/pythonnet/2613/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/b25ba26e8334bfeb6fcd053821a93412e1f6f80c2183f6545bb8cf4eb073f4bf/pythonnet/pythonnet/discussions/2613 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b25ba26e8334bfeb6fcd053821a93412e1f6f80c2183f6545bb8cf4eb073f4bf/pythonnet/pythonnet/discussions/2613 |
| og:image:alt | I have the following runtimeconfig.json: { "runtimeOptions": { "tfm": "net8.0", "framework": { "name": "Microsoft.NETCore.App", "version": "8.0.0" }, "configProperties": { "System.Runtime.Serializa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366807b865cee6776368231232f84d6c8096e6bce43f701a4fb28ea795ec427 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 33f356bb2fb58726ccb2f26395bf8ddc9a2d9eaa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width