[英]target .net framework class library with System.ServiceModel dependency from a .net core application
應該可以在Visual Studio 2017中引用.net core 2項目中的.net框架類庫,但在嘗試引用類庫依賴的System.ServiceModel時會遇到運行時異常。
創建“控制台應用程序(.NET Core)”。 Visual Studio將目標框架設置為“.NET Core 2.0”。
創建“類庫(.Net Framework)”。 Visual Studio將目標框架設置為“.NET Framework 4.6.1”。
從類庫中引用“System.ServiceModel 4.0.0.0”。 使用以下內容在類庫中填充Class1:
public class Class1
{
public void Test()
{
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("");
}
}
從控制台應用程序引用類庫。 從控制台應用程序的主要方法調用Class1中的測試方法:
class Program
{
static void Main(string[] args)
{
new Class1().Test();
}
}
建立並運行。 拋出異常然后嘗試執行Test()
方法:
System.IO.FileNotFoundException發生HResult = 0x80070002
消息=無法加載文件或程序集'System.ServiceModel,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'。 該系統找不到指定的文件。 Source = StackTrace:位於c:\\ users \\ rth \\ source \\ repos \\ ConsoleApp2 \\ ClassLibrary1 \\ Class1.cs中的ClassLibrary1.Class1.Test():第15行,位於c:\\中的ConsoleApp1.Program.Main(String [] args) users \\ rth \\ source \\ repos \\ ConsoleApp2 \\ ConsoleApp1 \\ Program.cs:第11行
System.ServiceModel不在任一項目的/ bin / Debug文件夾中。
我試圖手動將System.ServiceModel復制到控制台應用程序的/ bin / debug文件夾,但得到相同的錯誤。
如何在不獲取運行時異常的情況下引用引用System.ServiceModel的類庫?
要使上面的代碼正常工作,沒有例外:
Class Library (.Net Framework)
更新為Class Library (.NET Standard 2.0)
System.ServiceModel.Http (version v4.4.0)
添加到Class Library (.NET Standard 2.0)
依賴項中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.