【问题标题】:Applying bindingRedirect config to pythonnet将 bindingRedirect 配置应用于 pythonnet
【发布时间】:2017-07-04 12:19:35
【问题描述】:

我有一个应用程序依赖 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?

【问题讨论】:

  • 您是在使用 Python 中的 .NET 程序集还是在 .NET 中嵌入 Python?
  • 我正在从 Python 调用 .NET 程序集
  • 那么app.config 应该适用于python.exepythonw.exe。你试过这个吗?
  • 复制 app.config 就像是对本地问题的全局解决方案(一个 python.exe,许多应用程序需要 bindingRedirects),但它让我解决了这个问题,我想不出更好的方法。
  • 另一种解决方案是:stackoverflow.com/a/33362587/2230844

标签: c# python .net app-config python.net


【解决方案1】:

以下是在使用来自 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>
>>>

【讨论】:

    【解决方案2】:

    我在使用带有 .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 所在的同一文件夹中。我不确定这是否是最好的解决方案,但我已经研究了许多其他解决方案,但这似乎只是有效。

    【讨论】:

      【解决方案3】:

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

      pyinstaller --onefile script.py
      

      这将在您当前的工作目录中创建一个带有 exe 文件 (script.exe) 的 dist 文件夹,您可以在其中放置您的 script.exe.config 文件,该文件将在运行 script.exe 时进行解析

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-30
        • 1970-01-01
        • 2016-05-13
        • 1970-01-01
        • 2018-06-24
        相关资源
        最近更新 更多