AverageAsker
AverageAsker

Reputation: 188

OWIN based In-process HTTP Server

I have OWIN enabled Web API 2 Service and I want to host it in .NET Console App and the only client for that service would also be in the same application's process. So, it should not be possible to access the service's endpoint by any other process. Is this possible?

If I run my app like this:

using (WebApp.Start<MyStartup>(new StartOptions(MyUrl) { ServerFactory = "Microsoft.Owin.Host.HttpListener" }))
{
   StartClientThread();

   Console.WriteLine("Press any key to exit");
   Console.ReadLine();
}

it then will be accessible to any client on the hosting machine but I need to limit the clients to that same process only.

This is possible without OWIN so the requests are sent by using HTTP stack inproc:

HttpServer server = InitHttpServer();

using (HttpClient client = new HttpClient(server, false))
{
   // use client's methods to send http requests to the service.
}

But I need similar with OWIN.

Upvotes: 0

Views: 742

Answers (1)

Kahbazi
Kahbazi

Reputation: 14995

You can use Microsoft.Owin.Testing nuget package. This is mainly for testing but I think is this case it will serve your purpose.

using (var server = TestServer.Create<Startup>())
{
    var result = await server.HttpClient.GetAsync("api/book");
    string responseContent = await result.Content.ReadAsStringAsync();
    var entity = JsonConvert.DeserializeObject<List<string>>(responseContent);
}

As you can see, you can use server.HttpClient to send request to your app.

I just don't know if it's good in performance as selfhost is.

Upvotes: 2

Related Questions