dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.6k stars 10.06k forks source link

Let user call IWebHost use a fake request and get the response directly. #13814

Closed tiandian closed 5 years ago

tiandian commented 5 years ago

Is your feature request related to a problem? Please describe.

I am trying to use CefGlue or Chromely or CefSharp to combine asp.net core to develop a cross platform desktop app, but after asp.net core run, it need listen the http(s) port, and it will occupies the main thread's message loop, so it need run in a standalone process, it is not so good for a desktop app.

Describe the solution you'd like

public static void Main(string[] args)
{
    var host = CreateWebHostBuilder(args).Build();
    host.Run();
}

above is the current asp.net core code, after host.run(), it will occupies main thread's message loop, it is not good for a desktop app, i hope i can do as below:

public static void Main(string[] args)
{
    var host = CreateWebHostBuilder(args).Build();

    //don' run, the main thread message loop is for cefglue/Chromely/CefSharp.
    //host.Run();

    //below is run in a cefglue/CefSharp's message loop, I wll get the request data from cefglue,
    //then package it and send it to the asp.net core pipeline, and get the response from the asp.net 
    //core pipeline, then send bak to the cefglue/CefSharp.
    //............................
    while (get request from cefglue)
    {
        //1. I will create a request use the data from the cefglue/CefSharp,
        MyRequest request = new MyRequest ()
        {
            Url = "http://demo.com/home/test",  //the url is from the cefglue/CefSharp's request,
            Method = "Post",  //the data is from the cefglue/CefSharp's request,
            Data = "The postData" //the data is from the cefglue/CefSharp's request,
        }

       //2. asp.net core deal with the request and return the response
       MyResponse response = host.ProcessRequest(MyRequest);

       //3. then i wll send the MyResponse data to the cefglue/CefSharp.
    }
    //............................
}

So all i need is IWebHost add a ProcessRequest method.

Additional context

There are other way to develop a desktop use asp.net core, like Electron.NET , but it let asp.net core run in a standalone process, and it is not eay to write code/debug/publish, if asp.net core can run with CefGlue /Chromely in the same process/project, it will be more easy to develop cross platform desktop apps.

thanks!

Tratcher commented 5 years ago

TestServer is good for this. It lets you send request to the web app in memory rather than over the network.

Your alternative is to use IWebHost.Start() rather than Run(). Run calls Start and then blocks until CTL+C is pressed. Start doesn't block.

tiandian commented 5 years ago

@Tratcher, i have test TestServer, it is just what i want! Thank you!