I’m facing with problem about creating HTTP server which has API on his side and also is able to listen all calls and log it.
I want to create something like:
Hub – server responsible for handle my window services which will registered, also communication logging between hub and nodes.
Nodes – window services which working as Client, which executing some tasks and using Hub API are able to logging information about that.
I dont know it is possible to create HTTP server which include API and HTTPListener. I tried like that, but this wont work.
Example:
static void Main(string[] args)
{
var httpServer = new HttpServer();
httpServer.Start(); //Listener using address - http://localhost:8080 the same as SelfHostServer
Console.WriteLine("Starting HTTP listener...");
var config = new HttpSelfHostConfiguration("http://localhost:8080/");
config.Routes.MapHttpRoute(
"API Default",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
while (Program._keepRunning) { }
httpServer.Stop();
Console.WriteLine("Exiting gracefully...");
}
Im not sure Im going good way, maybe I should create a separate server to listen and a separate selfhost server for the API?
Can someone explain what should I do for achive this? Thanks
EDIT
2
Answers
I was looking for middleware - https://learn.microsoft.com/pl-pl/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0
Selfthost API server and HttpListener server was not needed. All my web api calls will be catch and I can log all communications:
I can recommend you this simple working server template (However it is better to use frameworks like asp net core instead of your approach):