Gijs van der Meijde
Gijs van der Meijde

Reputation: 13

reference asp.net core web application from .net core console application

I have an ASP.NET core 3.0 web application (to setup a SignalR server) i want to start from a .net core console application. However, whenever i set the output type of the ASP.NET application to 'Console application' and reference it from a simple test .net core application i get the following error upon building:

Error: Program does not contain a static 'Main' method suitable for an entry point.

It also complains it cannot find the .dll file for the web application, but i guess that is because of the other build error. Whenever i set the ASP.NET core 3.0 web application output type to 'Class library' however, everything builds just fine. Only when i try to start the web application from the console application it only partially starts. I can clearly see the SignalR service and endpoint are being added, but when i try to connect a client to the SignalR server this isnt possible. What am i missing? why cant i start the asp.net core web application from the .net core console application?

Good to note: when i start the web application from a main function within the web application itself (using the same code as i use in the console application) everything works just fine.

Upvotes: 1

Views: 802

Answers (2)

Edward
Edward

Reputation: 29976

In general, you should not start the web app from a console app. You should deploy it with options from Host and deploy ASP.NET Core.

For testing purpose, you could follow steps below:

  1. Reference Asp.net Core web app from .net core console app
  2. Add code below to Program.cs in Console App

    CoreAPI.Program.Main(args);
    
    namespace ConsoleApp1
    {
    class Program
    {
            static void Main(string[] args)
            {
                 Console.WriteLine("Start .net core");
                 CoreAPI.Program.Main(args);           
            }
    }    
    }
    

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239290

You cannot simply just reference the project. It won't do anything, because it needs to actually be running. You need to publish and deploy the ASP.NET Core app, and then access it via HTTP from your console app.

Upvotes: 0

Related Questions