Pythonnet 异常:"The 'ExeConfigFilename' argument cannot be null"

Pythonnet exception: "The 'ExeConfigFilename' argument cannot be null"

正在尝试让 Flask 应用程序调用某些第三方 .NET 代码。我正在使用我自己在 Ubuntu 14.04 主机上构建的 pythonnet。 develop 和 master 分支都试过了。

该应用程序与 Flask 内置开发服务器完美配合,默认情况下 运行s localhost:5000。

问题是当我将 Apache/WSGI 加入混合时。如果有人愿意,我可以继续介绍 WSGI 配置的细节(实际上是通过 Puppet 部署的,但这无关紧要),但为了简洁起见,我们假设它是相当标准的。我之前设置过 WSGI 应用程序。 Web 应用程序在尝试使用其中一个 .NET 对象时崩溃,而该对象又尝试反序列化某些 POSTed JSON。堆栈跟踪如下:

   ERROR:SdoEngine:Failed to process request body: System.TypeInitializationException: The type initializer for 'System.Web.Script.Serialization.JavaScriptSerializer' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system. ---> System.ArgumentException: The 'ExeConfigFilename' argument cannot be null.
at System.Configuration.ExeConfigurationHost.CheckFileMap (ConfigurationUserLevel level, System.Configuration.ExeConfigurationFileMap map) <0x401bd900 + 0x00130> in <filename unknown>:0 
at System.Configuration.ExeConfigurationHost.InitForConfiguration (System.String& locationSubPath, System.String& configPath, System.String& locationConfigPath, IInternalConfigRoot root, System.Object[] hostInitConfigurationParams) <0x401bd4d0 + 0x0011c> in <filename unknown>:0 
at System.Configuration.InternalConfigurationSystem.InitForConfiguration (System.String& locationConfigPath, System.String& parentConfigPath, System.String& parentLocationConfigPath) <0x401bd470 + 0x0004d> in <filename unknown>:0 
at System.Configuration.Configuration..ctor (System.Configuration.InternalConfigurationSystem system, System.String locationSubPath) <0x401bd2e0 + 0x0008d> in <filename unknown>:0 
at System.Configuration.InternalConfigurationFactory.Create (System.Type typeConfigHost, System.Object[] hostInitConfigurationParams) <0x401bd060 + 0x0007c> in <filename unknown>:0 
at System.Configuration.ConfigurationManager.OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, System.Reflection.Assembly calling_assembly, System.String exePath) <0x401bc720 + 0x002b1> in <filename unknown>:0 
at System.Configuration.ClientConfigurationSystem.get_Configuration () <0x401bc5b0 + 0x00049> in <filename unknown>:0 
--- End of inner exception stack trace ---
at System.Configuration.ClientConfigurationSystem.get_Configuration () <0x401bc5b0 + 0x000c9> in <filename unknown>:0 
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection (System.String configKey) <0x401bc530 + 0x0001f> in <filename unknown>:0 
at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) <0x401bc3a0 + 0x00037> in <filename unknown>:0 
at System.Web.Script.Serialization.JavaScriptSerializer..ctor (System.Web.Script.Serialization.JavaScriptTypeResolver resolver, Boolean registerConverters) <0x401bbe60 + 0x0006c> in <filename unknown>:0 
at System.Web.Script.Serialization.JavaScriptSerializer..cctor () <0x401bbde0 + 0x00032> in <filename unknown>:0 
--- End of inner exception stack trace ---
at StormTestCoreLib.ScreenDefinition.ScreenDef.DeSerialize (System.String serializedObject) <0x401bbae0 + 0x00045> in <filename unknown>:0 
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x401b0780 + 0x000b7> in <filename unknown>:0 

Mono 版本是根据 www.mono-project.com/docs/getting-started/install/linux/ 安装的 4.2.2。还从 Mono Github 存储库的源代码构建了它,结果相同。有一个相关的 bug which was fixed on release 3.8,我确实有一个修复程序。我知道,因为我在单声道库上有 运行 nm 并且 mono_domain_set_config 在那里。

我是单声道的新手。它确实看起来像一个配置问题。我已经比较了开发服务器和 WSGI 环境变量,没有任何问题。在这一点上,我将不胜感激任何人可能拥有的任何指示。

PD:不得不删除一些链接。遗憾的是声望不够。我希望问题很清楚。

原来是pythonnet的bug。如果我在他们的 PyNet_Init 中添加对 mono_domain_set_config 的调用,问题就会消失。

diff --git a/src/monoclr/pynetinit.c b/src/monoclr/pynetinit.c
index eaa1d9c..ed247f2 100644
--- a/src/monoclr/pynetinit.c
+++ b/src/monoclr/pynetinit.c
@@ -31,6 +31,7 @@ PyNet_Args* PyNet_Init(int ext) {
     pn_args->shutdown_name = "Python.Runtime:Shutdown()";

     pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION);
+    mono_domain_set_config(pn_args->domain, ".", "Python.Runtime.dll.config");