将bindingRedirect配置应用于pythonnet

时间:2017-07-04 12:19:35

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

我有一个依赖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吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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