英文:
ILRepack'd DLL has IConfiguration, but that doesn't match Microsoft.Extensions.Configuration IConfiguration?
问题
以下是翻译的内容:
在一个 .Net 标准项目中,我需要从 Microsoft.Extensions.Configuration 传递 IConfiguration 给构造函数。
然后对此项目运行了 ILRepack,使用了 /internalize 标志。
接着,我从另一个项目引用了生成的 DLL,该项目也引用了 Microsoft.Extensions.Configuration,并尝试将 IConfiguration 传递给前面提到的构造函数。
然而,这两种类型不再匹配,导致出现以下错误:
错误 CS1503:参数 1:无法从
'Microsoft.Extensions.Configuration.IConfiguration
[C:\Users\me\.nuget\packages\microsoft.extensions.configuration.abstractions\7.0.0\lib\net6.0\Microsoft.Extensions.Configuration.Abstractions.dll]' 转换为 'Microsoft.Extensions.Configuration.IConfiguration
[C:\git\ILrepackedProject\src\ILrepackedProject\bin\Debug\netstandard20\ILrepackedProject.dll]'
如果我使用 dotPeek 查看我的 ILRepack'd DLL,我看到以下内容:
不幸的是,我对如何解决这个问题感到困惑。我需要重新打包或内部化这些依赖项,否则我们将再次陷入版本问题的 DLL 地狱。
我尝试了不将它们内部化,但它只是说明命名空间同时存在于 MS 官方包和我的重新打包的 DLL 中,看不到明显的解决方法。
我尝试使用 AssemblyInfo 使内部接口对我的消费项目可见,但没有成功。
我尝试了 ILrepack 中的答案,如此处所示:https://stackoverflow.com/questions/71825784/c-sharp-rename-namespaces-during-build
(即 renameinternalized 命令),但结果与之前相同。
我还尝试使用 /internalize:"repackExcludes.txt",其中包含如下条目:"^Microsoft.Extensions.Configuration(.){0,}$",导致消费项目抱怨该类型在两个不同的库中定义(我的 DLL 和 Microsoft 的包)
就像这样:
我以为我解决了这个问题,甚至发布了一个答案,如下所示,但问题仍然存在:
为了解决这个问题,我从我的 .net 标准和 .net 6 项目中删除了所有对 Microsoft.Extensions.Configuration.Abstractions 的引用。
然后我注意到仍然以某种方式存在对 Microsoft.Extensions.Configuration.Abstractions 的引用。
这是因为我将以下内容添加到我的 .Net 6 应用程序中,以允许其使用提供给 Asp.Net 应用程序的功能:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Microsoft.AspNetcore.App 恰好包含对 Microsoft.Extensions.Configuration.Abstractions 6.0.0 的引用,而我的 .Net Standard 应用程序正在使用 7.0.0。
因此,它们发生了冲突。
为了解决这个问题,所有项目现在都引用版本 6.0.0。
有什么想法吗?
英文:
In one .Net standard project, i have required a constructor to pass in IConfiguration from Microsoft.Extensions.Configuration.
This project has then had ILRepack run on it, using the /internalize flag.
I then reference the resulting DLL from another project, which also references Microsoft.Extensions.Configuration, and attempts to pass IConfiguration to the constructor mentioned before.
However, the two types do not match any more, and the following error occurs:
> Error CS1503 Argument 1: cannot convert from
> 'Microsoft.Extensions.Configuration.IConfiguration
> [C:\Users\me\.nuget\packages\microsoft.extensions.configuration.abstractions\7.0.0\lib\net6.0\Microsoft.Extensions.Configuration.Abstractions.dll]' to 'Microsoft.Extensions.Configuration.IConfiguration
> [C:\git\ILrepackedProject\src\ILrepackedProject\bin\Debug\netstandard20\ILrepackedProject.dll]'
If i take a look at my ILRepack'd DLL with dotPeek, i see the following:
Unfortunately, i'm lost as to what i can do here. I need to Repack or internalize the dependencies, otherwise we're going to go into DLL hell with versioning all over again.
I've tried to not internalize them, and it simply states that the Namespace is present in both the MS Official package, and my repacked DLL - with no obvious resolution in sight.
I've tried using AssemblyInfo to make the internals visible to my consuming project with 'InternalsVisibleTo', without success.
I've tried the answer with ILrepack, shown here: https://stackoverflow.com/questions/71825784/c-sharp-rename-namespaces-during-build
(i.e. the renameinternalized command) with the same results as before.
I've also tried using /internalize:"repackExcludes.txt" with an entry like "^Microsoft.Extensions.Configuration(.){0,}$" which results in the consuming project complaining that the type is defined in two different libraries (my DLL, and Microsofts package)
Like so:
I thought i'd resolved this, and even posted an answer, which is as follows, however the problem persists:
"To fix this problem, i removed all references to Microsoft.Extensions.Configuration.Abstractions from my .net standard and .net 6 projects.
I then noted that there was still somehow a reference to Microsoft.Extensions.Configuration.Abstractions.
This comes from adding the following to my .Net 6 application, to allow it to utilise functionality given to Asp.Net apps:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Microsoft.AspNetcore.App happens to include a reference to Microsoft.Extensions.Configuration.Abstractions 6.0.0
While my .Net Standard app was using 7.0.0.
Therefore, they clashed.
To fix this, all projects are now referencing version 6.0.0."
Ideas?
答案1
得分: 0
为解决此问题,我在Csproj中的发布后任务中添加了一个删除任务,用于删除我不想要的DLL文件。在这种情况下,它看起来像这样:
<Target Name="Custom AfterPublish" AfterTargets="Publish">
<Delete Files="$(PublishDir)\Microsoft.Extensions.Configuration.dll"></Delete>
<Delete Files="$(PublishDir)\Microsoft.Extensions.Configuration.Abstractions.dll"></Delete>
</Target>
当然,仅仅这样还不够。我需要确保这些DLL文件以某种方式被包含进来,因此我通过nuspec/nupkg将它们添加为我的项目的依赖项。
我还为项目添加了多目标支持,目标为NetStandard2.0、Net6和Net48。
最后,我使依赖于IConfiguration的代码与.Net6的条件编译相关联。这将其排除在我的包的.Net48和.NetStandard版本构建之外。
现在,当该包在一个Net6项目中使用时,您将获得具有添加了IConfiguration依赖项的版本,这在.Net6中可以正常工作。对于其他构建,您将不会获得依赖于上述两个DLL的IConfiguration相关的代码。
英文:
To resolve this, I added a delete task to my post-publish tasks in the Csproj, which deletes the DLLs I don't want ILRepacking.
In this case, it looks like:
<Target Name="Custom AfterPublish" AfterTargets="Publish">
<Delete Files="$(PublishDir)\Microsoft.Extensions.Configuration.dll"></Delete>
<Delete Files="$(PublishDir)\Microsoft.Extensions.Configuration.Abstractions.dll"></Delete>
</Target>
Of course, this alone was not enough. I needed to ensure that those DLLs were included somehow, so I added them as dependencies to my project via the nuspec / nupkg.
I added multi-targetting to the project, and targeted NetStandard2.0,Net6,Net48.
Finally, I made the code depending on IConfiguration conditional compilation to .Net6. This leaves it out of .Net48 and .NetStandard builds of my package.
Now, when the package is used in a Net6 project, you get the version with the dependency-added IConfiguration from .Net6, which works fine.
With other builds, you don't get the IConfiguration reliant code, and don't end up with the dependency on the two DLL's mentioned above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论