【问题标题】:target .net framework class library with System.ServiceModel dependency from a .net core application具有来自 .net 核心应用程序的 System.ServiceModel 依赖项的目标 .net 框架类库
【发布时间】: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


【解决方案1】:

要使上面的代码正常工作:

  1. 将您的Class Library (.Net Framework) 更新为Class Library (.NET Standard 2.0)
  2. 将 NuGet 包 System.ServiceModel.Http (version v4.4.0) 添加到 Class Library (.NET Standard 2.0) 依赖项。

【讨论】:

  • 是的,可以很好地引用 .NET Standard 2.0 库。但是,我需要特别引用 Sys-tem.ServiceModel 版本 4.0.0.0,因为 System.ServiceModel.* 版本 4.4.0 不包含我需要的功能:NetTcpSecurity with SecurityMode.Message 和 MessageCredentialType.Windows。很抱歉,对此没有很清楚。我已在问题中添加了特定的版本号。
  • @Rolf 你可能想检查一下这个SimpleSOAPClient 的解决方法是否能解决你需要的技巧stackoverflow.com/a/45988249/804385
  • 基本服务模型在System.ServiceModel.Primitives NuGet包中,兼容.NET Standard 2.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
相关资源
最近更新 更多