looking for some solutions? You are welcome.

SOLVED: Libraries in dotnet core 2.0 get FileNotFound exception

twreynol:

We have a system that relies heavily on "Adapters" that implement a simple interface for doing unique processing. Normally, we dynamically load these by assembly name, but in this case we simply want to load them as "Libraries" from within our solution. In dotnet Core 2.0, this seems to be broken. I am including a small sample that exhibits this problem. Perhaps my design is flawed, or something has changed. This same pattern is working well for us in dotnet Framework (prior to Core).

I am including an example and a screen shot of what my sample project looks like, but this is the jist of it.

Solution Folder Layout

I have a dotnet core 2.0 "library" called ExternalAdapter.

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" };
        }
    }
}

It has a class in it named TheAdapter that implements the ISomeExternalAdapter interface.

I have a dotnet core 2.0 "console app" that instantiates a new ExternalAdapter.TheAdapter() as the 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"));
    }
}

This fails with a 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.

Yet when I look in my \bin\Debug\netcoreapp2.0 folder of the main program, the ExternalAdapter.dll is found.

If I do a clean / rebuild, it dutifully puts it there as well, so I am pretty sure the file should be found.

Is this a bug in 2.0? or do we need to load "Interfaced" adapters a different way?

Our eventual project has dozens of "adapters" that will be loaded via their interface depending on the type of processing we need to do at our server, but this small example points out the problem we are having in our main app. Any and all help is appreciated.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE
Share:

No comments:

Recent