Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entry point not found in .NET Core 2.0 DLL

I couldn't find anything that explains this -- for some reason my .NET Core 2.0 ASP.NET application does not run as a DLL via:

dotnet MyProject.Web.dll

And instead I get the exception:

Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'MyProject.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

namespace MyProject.Web
{
public class Program
{
    public static void Main(string[] args)
    {
        LoadDependencies();

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    } 

    private static void LoadDependencies()
    {
        DependencyLocator.Instance.DefineIfUndefined<IDataProvider, DataProvider>();
    }
}
}

It runs fine as a standalone executable (when targeting a "Console Application" in the project's config), but now that I'm trying to deploy to a server that needs it to run via the dotnet command (as a DLL, i.e. "dotnet .\MyProject.Web.dll"), it seems to be having issues. I get the above exception on both my server and my local development box.

I'm kind of blown away that it cannot locate the Main method -- it's declared as static and in Program.cs. Am I missing something?

(EDIT: To clarify, the DLL I'm trying to run against the "dotnet" command is from the target compiling as a "Console Library," since my server is explicitly asking for a DLL, since they will not run executables).

like image 397
Locke Avatar asked Aug 20 '18 17:08

Locke


1 Answers

OK, so this is annoying and will hopefully help someone else out.

My host wants to specifically run DLL's thru .NET Core ONLY. They do not allow for executables to be run.

Because DLL's are frequently built as "Class Library" output types on the project, I assumed that this was the workflow necessary to build it. However, I found out that whenever you build your project as a "Console Application," it builds a DLL in addition to an EXE. So, in the above example, MyProject.Web.exe and MyProject.Web.dll are both built when the output type is "Console Application."

MyProject.Web.dll that comes from "Console Application" is different than MyProject.Web.Dll that comes from "Class Library." The one that comes from "Class Library" will NOT have an entry point that can be discovered on it, which will lead to the problem above.

So, if you're getting this error, look for the DLL that ships with your EXE of the same name -- that's the actual DLL you'll want to run in your dotnet console (i.e. dotnet MyProject.Web.dll)

like image 180
Locke Avatar answered Oct 19 '22 20:10

Locke