我有一个.netstandard2.0项目,假设它叫做common,它使用nuget.org中的System.Configuration.ConfigurationManager5.0.0和log 4 net2.0.15。这个公共项目由项目A(一个.net5项目)使用,也由项目B(一个.net framework 4.6.1项目)使用。在公共项目中,我使用系统.配置.配置管理器和log 4 net如下:
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
</ItemGroup>
现在,当log 4 net尝试初始化时,我遇到了System.Configuration.ConfigurationManager.dll的程序集加载问题,因为它需要4.0.1.0System.Configuration.ConfigurationManager的www.example.com,而不是5.0.0。
错误消息如下所示:
System.IO.FileNotFoundException:未能加载文件或程序集“System.Configuration.ConfigurationManager,版本=4.0.1.0,区域性=neutral,PublicKeyToken= cc 7 b13 ffcd 2ddd 51”。系统找不到指定的文件。
如何强制log4net.dll使用System.Configuration.ConfigurationManager5.0.0.0?我尝试按如下方式添加log4net.dll.config,但不起作用。log 4 net仍尝试加载旧版本的System.Configuration.ConfigurationManager。除了从源代码重新生成log 4 net之外,是否有人知道如何解决此问题?
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Configuration.ConfigurationManager" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我检查了log4net.dll的元数据,它确实使用了4.0.1.0版本System.Configuration.ConfigurationManager。我尝试使用上述语法重新绑定,但似乎被忽略。
要重现此问题,需要将A和B的输出文件夹设置为相同:
<OutputPath>$(SolutionDir)$(Configuraiton)$(Platform)</OutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
我还发现如果我只重建A(或重新构建解决方案,三个项目A、B和Common在同一个解决方案中),然后通过右键单击-〉Properties -〉Details检查输出文件夹中的log4net.dll属性,产品版本显示“2.0.15.0-NET Standard 2.0”。但是,如果我只重新构建B,它将变为“2.0.15.0-.NET 4.5”,在这种情况下,问题就没有了。程序集加载问题只在log 4 netiderdll显示“2. 0. 15. 0-NET标准2. 0”时发生。
1条答案
按热度按时间nhaq1z211#
随着你提供的最新信息,一切终于走到了一起。
根本问题是在同一个文件夹中构建两个独立的.NET实现。
AppendTargetFrameworkToOutputPath
的存在是有原因的。一个
.NET
dll不能依赖.NETFramework
类库,反之亦然。这基本上就是这里发生的事情。构建的输出是互相攻击的,运行的基本上是你最近构建的。当我按照你描述的方式设置我的项目时,我可以重新创建它。
项目A:
net5.0
,项目B:net461
,常见:netstandard2.0
.构建版本
A
-〉构建版本B
。B
运行良好,但A
运行不佳。构建版本
B
-〉构建版本A
。A
运行良好,但B
运行不佳。如果我添加这个手工组装绑定,则重定向到
B
的appconfig构建
B
-〉构建A
。两个都可以,但是这是非常不可靠的。如果程序集是通过反射启动的,那么你需要格外小心,因为你需要确保CLR加载配置文件的方式与启动EXE时的方式相同。这是设置所有绑定重定向的方式(
.NETFramework
的app.config,.NET
的deps.json
)。