c# - dotnet core 2.0 中的库获取 FileNotFoundexception

标签 c# shared-libraries .net-core

我们有一个严重依赖“适配器”的系统,它实现了一个简单的接口(interface)来进行独特的处理。通常,我们通过程序集名称动态加载它们,但在这种情况下,我们只想从我们的解决方案中将它们作为“库”加载。在 dotnet Core 2.0 中,这似乎被打破了。我包括一个展示这个问题的小样本。也许我的设计有缺陷,或者发生了某些变化。同样的模式在 dotnet Framework(在 Core 之前)中对我们很有效。

我提供了一个示例和我的示例项目的屏幕截图,但这就是它的要点。

Solution Folder Layout

我有一个名为 ExternalAdapter 的 dotnet core 2.0“库”。

public class TheAdapter : TestExternalInterface.Interfaces.ISomeExternalAdapter
{
    public string GetSomeValue(string valueType)
    {
        switch (valueType)
        {
            case "Red":
            case "red":
                return "Your color is red";
            case "Blue":
            case "blue":
                return "Your color is blue";

            default:
                return "Unknown Value Type";
        }
    }

    public List<string> GetSomeValues(string valueType)
    {
        switch (valueType)
        {
            case "Color":
            case "color":
                return new List<string> { "Red", "Blue" };
            case "Flavor":
            case "flavor":
                return new List<string> { "Sweet", "Savory" };

            default:
                return new List<string> { "Color", "Flavor" };
        }
    }
}

它有一个名为 TheAdapter 的类,它实现了 ISomeExternalAdapter 接口(interface)。

我有一个 dotnet 核心 2.0“控制台应用程序”,它将一个新的 ExternalAdapter.TheAdapter() 实例化为 ISomeExternalAdapter 接口(interface)。

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("About to load external adapter...Press <ENTER> to continue.");
            Console.ReadLine();
            LoadAndTestExternalAdapter();            
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.ReadLine();
    }

    private static void LoadAndTestExternalAdapter()
    {
        Interfaces.ISomeExternalAdapter adapter = new ExternalAdapter.TheAdapter() as Interfaces.ISomeExternalAdapter;
        Console.WriteLine(adapter.GetSomeValue("red"));
        Console.WriteLine(adapter.GetSomeValues("flavor"));
    }
}

失败并出现 System.IO.FileNotFoundException:

Could not load file or assembly 'ExternalAdapter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

然而,当我查看主程序的\bin\Debug\netcoreapp2.0 文件夹时,发现了 ExternalAdapter.dll。

如果我进行清理/重建,它也会尽职尽责地把它放在那里,所以我很确定应该找到该文件。

这是 2.0 中的错误吗?还是我们需要以不同的方式加载“接口(interface)”适配器?

我们最终的项目有几十个“适配器”,它们将根据我们需要在服务器上执行的处理类型通过它们的接口(interface)加载,但是这个小例子指出了我们在主应用程序中遇到的问题。 感谢您提供任何帮助。

最佳答案

通过将“接口(interface)定义”移动到它自己的库并将该库作为我的 ExternalAdapter 项目和 TestExternalInterface 项目中的引用,问题已得到解决。或者,2.0 的更新已修复它 - 我不确定。但是一个月后,我开始工作了。新项目布局的屏幕截图如下:

New Project Layout

关于c# - dotnet core 2.0 中的库获取 FileNotFoundexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985795/

相关文章:

c# - 无法将用户控件放在窗体上

c# regex 查找和替换重用匹配文本的一部分

c++ - 通过 Firefox p11 模块打开 Wxwidget 窗口时崩溃

c - 即使定义了 "-nostdlib"选项,如何运行构造函数

python - 从其他语言调用 Python 的通用(或最佳)选项?

c# - Xamarin 表单 + Prism 登录流程

c# - 在 C# 中,null 是否也继承自 Object?

c# - 在 .NET Core 中转换无限嵌套对象

c# - 如何使用 C# (dotnet core 3.1) 进行 OAuth 1 Twitter API 调用

azure - 将 Application Insights 遥测处理器添加到 Azure Functions