6

How do I launch an ASP.NET Core Web-API programmatically from C#? The API should launch after I click on a button in a form.

I already added my API to my solution via Add -> Existing Project... but I don't know how to launch it from a different Project.

The Web-API is running on .NET Core 2.0 and I want to launch it from a project using .NET Framework 4

Felix
  • 113
  • 1
  • 9
  • WebApi does not use documentation, so you can not add some reference as you can do with WCF or Webservices. So you need to write your own client ... https://www.google.be/search?q=generic+client+for+webapi – Laurent Lequenne Jun 11 '18 at 11:56

3 Answers3

2

Probably you are looking for something like this(link)

dotnet run [-c|--configuration] [-f|--framework] [--force] [--launch-profile] [--no-build] [--no-dependencies]
[--no-launch-profile] [--no-restore] [-p|--project] [--runtime] [[--] [application arguments]]

dotnet run [-h|--help]

  • 1
    I thought there might be a more elegant way than running a cmd command. Especially when the API should also be shut down programmatically. – Felix Jun 11 '18 at 11:55
  • @Felix if you are using windows you can add this Web API to your IIS and start/stop it there. if not, I think there some similar way to do that with UNIX/Linux – Alexander Demianenko Jun 11 '18 at 12:08
  • 1
    All ASP.NET Core apps are essentially just console apps. Which means they're separate processes. You start the process just like you'd start *any* process, i.e. run it from the shell. – Chris Pratt Jun 11 '18 at 12:59
0

You can also try to run the application as a Windows Service, and start the service on click.

koelkastfilosoof
  • 2,212
  • 1
  • 18
  • 28
0

Here is a sample project showcasing hosting of ASP.NET Core app in WPF: https://github.com/mkArtakMSFT/Samples/tree/master/WpfAspNetCore

Couple of things to pay attention to:

  • The .NET Framework 4.6.1 is the minimum version which can host ASP.NET Core apps
  • This sample is dummy and doesn't have much configuration (based off of empty ASP.NET Core Web app template content). You can, however, add reference to the Microsoft.AspNetCore.Mvc package to add support for MVC.
Artak
  • 2,819
  • 20
  • 31