在非托管的C# .NET库项目中使用NuGet库

6
我正在为一个名为BIM Vision的软件创建插件。他们在C API周围进行了包装,因此我使用了UnmanagedExports,以使我的非托管DLL与包装编译良好。
问题是我无法在我的项目中使用NuGet库。它们可以编译通过,但在我的代码中调用任何函数都会导致软件崩溃。
所有库都在它们自己单独的DLL中编译,与我的DLL一起。只有我的DLL可以被软件加载(因为其他DLL缺少与BIMVision通信的代码)。
如何让软件找到缺失的DLL?我应该把它们放在哪里?
我的.csproj长这样:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{362E40F4-4A37-48B3-A7A5-93E5535A9B22}</ProjectGuid>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ExcelLink</RootNamespace>
        <AssemblyName>ExcelLink</AssemblyName>
        <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>C:\Users\Daniel\Documents\SAMT\BIMVISION SDK\bim_vision_exe\plugins</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <PlatformTarget>x86</PlatformTarget>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>..\..\..\bim_vision_exe\plugins\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <PlatformTarget>x86</PlatformTarget>
    </PropertyGroup>
    <ItemGroup>
        <Reference Include="PresentationCore" />
        <Reference Include="System" />
        <Reference Include="System.configuration" />
        <Reference Include="System.Core" />
        <Reference Include="System.Drawing" />
        <Reference Include="System.Security" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="C:\Users\Daniel\Documents\SAMT\BIMVISION SDK\sdk .NET\BIMVision.cs">
            <Link>BIMVision.cs</Link>
        </Compile>
        <Compile Include="ExcelLink.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="EPPlus" Version="4.5.3.3" />
        <PackageReference Include="UnmanagedExports" Version="1.2.7" />
        <PackageReference Include="ZetaIpc" Version="1.0.0.9" />
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <Import Project="C:\Users\Daniel\Documents\SAMT\BIMVISION SDK\exmaples .NET\vs_2013\packages\UnmanagedExports.1.2.7\tools/RGiesecke.DllExport.targets" Condition="Exists('C:\Users\Daniel\Documents\SAMT\BIMVISION SDK\exmaples .NET\vs_2013\packages\UnmanagedExports.1.2.7\tools/RGiesecke.DllExport.targets')" />
</Project>

我使用调试器成功地获取了软件中真正的异常信息:

Exception thrown at 0x75AA9962 in bim_vision.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x0019C458.

1
这个回答解决了你的问题吗?如何使我的托管NuGet包支持C++/CLI项目? - JonasH
1
@cab13140,你的评论和问题所表达的意思完全不同。哪一个是正确的?如果需要,请编辑问题。 - Ian Kemp
3
那个未经管理的应用程序可能会在插件文件夹中查找要加载的DLL。但这不是CLR查找相关程序集的位置。它首先在GAC中查找,接着在存储.exe文件的目录中查找,然后引发AppDomain.AssemblyResolve事件。有三种方法可以让应用程序满意。另外一种方法是编写一个名为somename.exe.config的文件,并加入probing元素。 - Hans Passant
1
@HansPassant,你是正确的。在GAC中安装了正确的版本,现在它可以正常工作了。请随意发布赏金的答案... - defvs Daniel
1
尝试将包含C#代码的行放入try/catch块中。在catch中,记录异常信息以便您可以查看更多有关.NET错误的详细信息,因为C++应用程序可能会吞噬您的.NET异常并抛出您提到的异常。 - Sherif Elmetainy
显示剩余5条评论
1个回答

2

通过使用GACUTIL按以下方式安装库DLL文件,可以解决此问题。

gacutil /i [path.dll]

由于版本不匹配,仍然无法运行System.Buffers包;我安装了正确的版本,它就可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接