如何在 ASP.NET 上包含 libsodium.net [英] How to include libsodium.net on ASP.NET

查看:20
本文介绍了如何在 ASP.NET 上包含 libsodium.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.NET(使用 .asmx)文件上构建了一个旧的 webservice.我需要使用sodium.net - 不幸的是它在加载依赖的libsodium.dll 文件时失败.关于我做错了什么有什么想法吗?

I have an old webservice build on ASP.NET (using .asmx) files. I need to use sodium.net - unfortunately it fails while loading the dependent libsodium.dll file. Any ideas about what I make wrong?

  • 我已经通过 NuGet 添加了 libsodium.net.

  • I have added libsodium.net through NuGet.

我已将 64 位 DLL 重命名为libsodium.dll"(以及其他命名约定).

I have renamed the 64 bit DLL to "libsodium.dll" (and other naming conventions too).

我曾尝试直接引用 libsodium.dll,但 VS 拒绝了它(不是有效的 DLL).所以我将它添加为内容"而不是复制到输出".

I have tried to reference libsodium.dll directly but VS rejects it (not a valid DLL). So I have added it as "content" instead with "copy to output".

构建后,我可以看到网站/Bin 文件夹包含sodium.dll(.NET 程序集)和libsodium.net.

After building I can see that the website/Bin folder contains both sodium.dll (the .NET assembly) and libsodium.net.

当我尝试使用 libsodium.net 时,我得到:

When I try to use libsodium.net I get:

ERROR 2015-02-02 11:14:27,118 13798ms [41] CabinetService doRequest - 捕获:Sodium.SodiumCore"的类型初始值设定项引发异常.System.TypeInitializationException:Sodium.SodiumCore"的类型初始值设定项引发异常.---> System.DllNotFoundException: 无法加载 DLL 'libsodium.dll': 找不到指定的模块.(来自 HRESULT 的异常:0x8007007E)在 DynamicDllInvokeType.sodium_init()在Sodium.SodiumCore..cctor()--- 内部异常堆栈跟踪结束 ---在Sodium.SodiumCore.LibraryName()在Sodium.SecretBox.Create(Byte[] message, Byte[] nonce, Byte[] key)在 Macaroons.SecretBoxCryptoAlgorithm.Encrypt(Byte[] key, Byte[] plainText) in c:ProjectsMacaroons.NetMacaroons.NetSecretBoxCryptoAlgorithm.cs:line 58

ERROR 2015-02-02 11:14:27,118 13798ms [41] CabinetService doRequest - Caught: The type initializer for 'Sodium.SodiumCore' threw an exception. System.TypeInitializationException: The type initializer for 'Sodium.SodiumCore' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libsodium.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at DynamicDllInvokeType.sodium_init() at Sodium.SodiumCore..cctor() --- End of inner exception stack trace --- at Sodium.SodiumCore.LibraryName() at Sodium.SecretBox.Create(Byte[] message, Byte[] nonce, Byte[] key) at Macaroons.SecretBoxCryptoAlgorithm.Encrypt(Byte[] key, Byte[] plainText) in c:ProjectsMacaroons.NetMacaroons.NetSecretBoxCryptoAlgorithm.cs:line 58

所以即使它在 Bin 文件夹中,它也找不到libsodium.dll".我也确实尝试删除对sodium.net"的依赖,在我收到运行时错误说sodium.net"丢失后 - 当我重新添加它时,那个错误消失了,我得到了上面的那个(表明sodium.net"正确加载).

So it cannot find "libsodium.dll" even though it is in the Bin folder. I did also try to remove the dependency on "sodium.net" where after I got a runtime error saying "sodium.net" is missing - when I re-added it, that error disappeared and I got the one above instead (indicating the "sodium.net" loads correctly).

所以我打开C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filescabinetservice"中的网站影子文件夹并搜索sodium".唯一的结果是某些子文件夹中的sodium.dll".没有libsodium.dll".

So I open up the website shadow folder in "C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filescabinetservice" and search for "sodium". The only result is "sodium.dll" in some subfolder. There is no "libsodium.dll".

很明显 ASP.NET 在创建网站的卷影副本时忽略了libsodium.dll"文件.

So apparently ASP.NET ignores the "libsodium.dll" file when creating the shadow copy of the website.

我还尝试将 libsodium.dll(32 位)添加到 C:WindowsSystem32,将 libsodium.dll(64 位)添加到 C:WindowsSysWOW64.结果一样.

I have also tried adding the libsodium.dll (32 bit) to C:WindowsSystem32 and libsodium.dll (64 bit) to C:WindowsSysWOW64. Same result.

我已经尝试过 C:WindowsMicrosoft.NETassemblyGAC_32libsodium 和 C:WindowsMicrosoft.NETassemblyGAC_64libsodium.结果一样.

And I have tried C:WindowsMicrosoft.NETassemblyGAC_32libsodium and C:WindowsMicrosoft.NETassemblyGAC_64libsodium. Same result.

如何让 ASP.NET 知道依赖关系?

How can I make ASP.NET aware of the dependency?

推荐答案

事实证明 ASP.NET 不会制作非托管 DLL(例如 libsodium.dll 和 libsodium-64.dll)的卷影副本.

It turns out that ASP.NET doesn't make shadow copies of unmanaged DLLs such as libsodium.dll and libsodium-64.dll.

Sodium.dll(托管代码)尝试从与Sodium.dll 的影子副本相同的目录(这将不起作用)或PATH 环境变量目录中的某些位置加载DLL.

Sodium.dll (the managed code) tries to load the DLLs from either the same directory as the shadow copy of Sodium.dll (which is not going to work) - or some where in the PATH environment variable's directories.

我的解决方案是在调用任何Sodium代码之前将AppDomain Bin目录添加到路径中:

My solution was to add the AppDomain Bin directory to the path before calling any Sodium code:

// Make it possible to load unmanaged libsodium DLLs that .NET does not make shadow copies of.
// -> Simply point the "path" variable to the Bin directory.
string path = Environment.GetEnvironmentVariable("PATH");
string binDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
Environment.SetEnvironmentVariable("PATH", path + ";" + binDir);

2018 年 1 月更新(来自 Jordan Rieger 的回答):

Update January 2018 (from Jordan Rieger's answer):

请注意,您可能还需要安装 Microsoft Visual C++ 2015可在您的服务器上重新分发(x64 或 x86 目标,取决于您的流程.)

Note that you may also need to install the Microsoft Visual C++ 2015 Redistributable on your server (either the x64 or x86 target, depending on your process.)

不要忘记重新启动您的应用程序池(我重置了 IIS)安装可再发行组件.

Don't forget to restart your app pool (I did an IIS reset) after installing the redistributable.

这篇关于如何在 ASP.NET 上包含 libsodium.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆