我试图将我的C#项目发布到一个可执行文件中,以便发布它。然而,我引用了 "Interop.IWshRuntimeLibrary",自从包含了它,我的项目发布了,但在执行时却崩溃了。
An assembly specified in the application dependencies manifest was not found:
package: 'Interop.IWshRuntimeLibrary', version: '1.0.0.0'
path: 'Interop.IWshRuntimeLibrary.dll'
到目前为止,我已经尝试过:
将Interop.IWshRuntimeLibrary的Copy Local和Embed Interlop Types属性设置为每一个组合都是trueefalse。
在.csproj文件中把下面的标签设置为truefalse。
<PublishWithAspNetCoreTargetManifest>
我的.csproj文件目前是这样的。
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Project</RootNamespace>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary.dll">
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
项目中的其他引用,比如Newtonsoft,都能正常工作。我查阅了网络上能找到的所有与此相关的帖子。最接近描述我的问题的线程是 无法加载文件或程序集Interop.IWshRuntimeLibrary? 但我在那里也没有找到有用的信息。
理想的情况是,我只想点击发布,然后发布到我桌面上的一个文件夹--最好是以单个.exe的形式发布,但如果可以的话,整个文件夹都可以。我不知道我是否可能遗漏了某个步骤,因为我以前从未使用过发布功能。我不知道下一步该怎么尝试。谢谢。
你现在是如何发布的?
你应该可以在运行publish命令时加上一个标志,让它以单个文件的形式发布。
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
更多信息在这里。https:/dotnetcoretutorials.com20190620publishing-a-single-exe-file-in-net-core-3-0。
但对于你的特殊问题。当以单个文件的形式发布时(你可能已经在做了),会涉及到某种程度的树形抖动,以试图限制它发布的依赖关系。在某些情况下,如果你引用的库是用反射或类似的方法加载的,那么ILLinker就不知道它是否真的被引用和使用了。
为了解决这个问题,你可以在你的csproj文件中添加以下内容。
<ItemGroup>
<TrimmerRootAssembly Include="Interop.IWshRuntimeLibrary" />
</ItemGroup>
然后像这样发布你的项目。
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
更多关于ILLinker如何工作的信息在这里。https:/dotnetcoretutorials.com20190627the-publishtrimmed-flag-with-il-linker。