【发布时间】:2018-02-20 03:34:31
【问题描述】:
在 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("");
}
}
从控制台应用程序引用类库。 从控制台应用程序的 main 方法调用 Class1 中的测试方法:
class Program
{
static void Main(string[] args)
{
new Class1().Test();
}
}
构建并运行。抛出异常然后尝试执行Test() 方法:
System.IO.FileNotFoundException 发生 HResult=0x80070002
消息=无法加载文件或程序集 'System.ServiceModel, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089'。 该系统找不到指定的文件。来源= StackTrace:在 ClassLibrary1.Class1.Test() 在 c:\users\rth\source\repos\ConsoleApp2\ClassLibrary1\Class1.cs:line 15 在 ConsoleApp1.Program.Main(String[] args) 中 c:\users\rth\source\repos\ConsoleApp2\ConsoleApp1\Program.cs:第 11 行
System.ServiceModel 不在任一项目的 /bin/Debug 文件夹中。
我尝试手动将 System.ServiceModel 复制到控制台应用程序的 /bin/debug 文件夹,但得到了同样的错误。
如何引用引用 System.ServiceModel 的类库而不出现运行时异常?
【问题讨论】:
-
System.ServiceModel 在 GAC 和 Framework 目录中,至少在我的机器上。您可能引用了具有您不满足的特定版本要求的程序集。检查您的 GAC 和您的框架目录。然后检查以确保您的参考不是针对特定版本。
-
@Kevin-hirst:当我查看 System.ServiceModel 引用的属性时,路径是 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1 \System.ServiceModel.dll。如果我把这个文件放在控制台应用程序的 /bin/debug 下,那么它仍然无法工作,并且程序集加载器应该在此处查看。需要注意的一件事:如果我创建一个 .net 框架控制台应用程序(不是 .net 核心)并引用相同的类库,则没有问题。
-
如果我将控制台应用程序的目标框架更改为 .NET Framework 4.6.1,它可以工作。
标签: c# .net visual-studio-2017 .net-core-2.0 .net-4.6.2