2016-04-15 132 views
6

我有這個簡單的C#程序:未能加載類型 'System.Net.Security.SslStream'

using Npgsql; 
public class App { 
    public static void Main(string[] args) { 
    const string CONNECTION_STRING = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase"; 
    using (var conn = new NpgsqlConnection(CONNECTION_STRING)) { 
     conn.Open(); 
    } 
    } 
} 

,我和單編譯(MCS):

mcs -target:exe -lib:bin -r:System.Data.dll -r:Npgsql.dll -r:System.dll -r:Mono.Security.dll -out:bin/ssl.exe src/App.cs 

當我執行時,拋出一個錯誤:

Unhandled Exception: 
System.TypeLoadException: Could not load type 'System.Net.Security.SslStream' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 
    at Npgsql.NpgsqlConnector.Open() <0x4155f7f0 + 0x00115> in <filename unknown>:0 
    at Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) <0x4155c8d0 + 0x00a4f> in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'System.Net.Security.SslStream' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 
    at Npgsql.NpgsqlConnector.Open() <0x4155f7f0 + 0x00115> in <filename unknown>:0 
    at Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) <0x4155c8d0 + 0x00a4f> in <filename unknown>:0 

我Npgsql.dll版本

$ monop2 -r Npgsql.dll 

Assembly Information: 
Npgsql 
Version=2.2.0.0 
Culture=neutral 
PublicKeyToken=5d8b90d52f46fda7 

我的編譯器:

$ mcs --version 
Mono C# compiler version 4.4.0.0 

$ mono --version 
Mono JIT compiler version 4.4.0 (Stable 4.4.0.40/f8474c4 Mon Mar 28 12:22:29 UTC 2016) 
Copyright (`u`C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com 
TLS:   __thread 
SIGSEGV:  altstack 
Notifications: epoll 
Architecture: amd64 
Disabled:  none 
Misc:   softdebug 
LLVM:   supported, not enabled. 
GC:   sgen 

最後,我的環境:

$ uname --all 
Linux abe 4.5.0-1-ARCH #1 SMP PREEMPT Tue Mar 15 09:41:03 CET 2016 x86_64 GNU/Linux 

謝謝

回答

13

我有一個問題 - 你有bin文件夾內的庫Mono.Security.dll?如果是這樣,請刪除它並重試。

+0

是的,我在bin中有Mono.Security.dll。刪除後,它的作品!謝謝 – o3o

+3

我有同樣的問題,這似乎是最近(可能與mono的最新更新有關)。 – picrap

+0

我在使用JetBrains Rider的Linux中遇到了同樣的問題。該DLL在服務文件夾中。刪除文件不起作用。在構建過程中,dll被複制過。我編輯了/packages/Npgsql.2.2.2/net45/Npgsql.xml併爲Mono Security註釋掉了六行。這也沒有奏效。我將/packages/Npgsql.2.2.2/net45/Mono.Security.dll重命名爲junk.Mono.Security.dll.junk,並且工作正常。 –

0

我想放的人,將獲得不同的異常像我這個信息,我有:

System.TypeLoadException: Failure has occurred while loading a type. at Npgsql.NpgsqlConnector.Open() [0x0002b] in <filename unknown>:0 at Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) [0x0017e] in <filename unknown>:0 

conn.Open(); 

但PIKos解決方案適用於我。

我使用:

Mono JIT compiler version 4.4.0 (tarball Tue Jun 14 13:41:51 UTC 2016) 

Npgsql.dll version 2.1.0.0 
0

大廈關閉@ pikos的迴應,我發現在NuGet包的組件之一,我使用附帶它自己的Mono.Security.dll。但是,單聲道4.4.1 moves some of the types out of that assembly,並可能會有一些移動。因爲我沒有明確提及Mono.Security,所以[x | ms] build通過使用我的packages文件夾中的不兼容程序集來解決間接依賴性問題。

爲了解決這個問題,沒有任何其他的解決方法(例如刪除每個版本的程序集),我只是添加了一個明確的彙編引用到系統的Mono.Security。這會強制使用GAC中的4.4.1版本。通過不使用nuget中的那個,我失去了促使nuget的構建者加入他們自己的Mono.Security的補丁,但現在我沒有問題。因人而異。

相關問題