将 bindingRedirect 配置应用于 pythonnet

Applying bindingRedirect config to pythonnet

我有一个应用程序依赖于 NuGet 的 <bindingRedirect> 功能来确保 log4net.dll 的单一版本。绑定重定向会自动添加到应用程序 app.config 文件中。

我想将该应用程序的程序集加载到 Python 中并调入其代码,但由于绑定重定向是特定于应用程序的,因此 Pythonnet 无法获取它们程序集加载失败:

LOG: Post-policy reference: log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
[...snip...]
LOG: Assembly Name is: log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
WRN: Comparing the assembly name resulted in the mismatch: Major Version

我可以让 pythonnet 引用我的应用程序的 app.config 并使用那里找到的 <bindingRedirect> 吗?或者我可以在启动后应用绑定重定向,而不需要 app.config?

以下是在使用来自 CPython 的 .NET 程序集时使用 pythonnet 启用 app.config 的方法:

python.exe.config 文件放在下一个 python.exe 中,配置如下供稍后检测:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="customAppSettingsGroup"> <section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration" /> </sectionGroup> </configSections> <customAppSettingsGroup> <customAppSettings> <add key="Debugger" value="True"/> </customAppSettings> </customAppSettingsGroup> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup> </configuration>

使用以下代码生成 .NET 程序集以测试和读取 app.config 设置:

using System;

using System.Collections.Specialized;
using System.Configuration;

namespace dotnet20
{
    public class Class1
    {
        public Class1()
        {
            NameValueCollection settings =
                ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings") as NameValueCollection;

            if (settings != null)
            {
                foreach (string key in settings.AllKeys)
                {
                    if ((key == "Debugger") && (settings[key] == "True"))
                    {
                        Console.WriteLine("Detected debugger mode");
                    }
                }
            }
        }
    }
}

现在测试 pythonnet 是否能够从 app.config 中检测到这些自定义设置:

python
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> import sys
>>> sys.path.append(r"C:\pythonnet\dotnet2.0\bin\Debug")
>>> clr.AddReference("dotnet2.0")
<System.Reflection.RuntimeAssembly object at 0x000001A51626D630>
>>> from dotnet20 import Class1
>>> Class1()
Detected debugger mode
<dotnet20.Class1 object at 0x000001A51626D6A0>
>>>

我在使用带有 .net dLL 的 Pythonnet 时遇到了类似的问题。

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.

我没有 Microsoft.Extensions.DependencyInjection.Abstractions 的 3.1.0.0 版,因此出现错误。我的 .net 项目有 app.config 文件指向此依赖项的较新版本 (3.1.5.0) [bindingRedirect]

解决方案是使用 .net 项目中的 app.config 文件,将其重命名为 python.exe.config 并将其放在 python.exe 所在的同一文件夹中。我不确定这是否是最好的解决方案,但我研究了很多其他解决方案,这似乎只有效。

您可以使用 pyinstaller(使用 pip 安装:pip install pyintaller 参见 project page)创建单个可执行文件并将配置文件放在同一文件夹中。 能够在每个项目的基础上使用 app.exe.config 文件。

pyinstaller --onefile script.py

这将在您当前的工作目录中创建一个包含 exe 文件 (script.exe) 的 dist 文件夹,您可以在其中放置 script.exe.config 文件,该文件将根据 [=23] 进行解析=] script.exe