The http_listener class blocks on Windows if the machine has a single core (e.g. small Azure VMs)
Following code for repro (and workaround)
#include "stdafx.h"
#include <cpprest/http_listener.h>
#include <cpprest/filestream.h>
using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;
// Set this to 1 to show the fix
// Setting 0 will cause hang on single core machines (like small Azure instances)
#define SET_MIN_CONCURRENCY 0
int _tmain(int argc, _TCHAR* argv[])
{
#if SET_MIN_CONCURRENCY
// This policy fixes the issue
concurrency::CurrentScheduler::Create(Concurrency::SchedulerPolicy(1, 2, Concurrency::MinConcurrency));
// oversubscribe isn't sufficient
//concurrency::Context::Oversubscribe(true);
#endif
http_listener listener(U("http://+:80/test"));
listener.support(methods::GET, [&](http_request request)
{
request.reply(status_codes::OK, U("Hello, world"));
});
puts("Opening http://localhost/test for serving");
listener.open().wait();
puts("Press [enter] to quit");
std::string line;
std::getline(std::cin, line);
listener.close().wait();
#if SET_MIN_CONCURRENCY
//concurrency::Context::Oversubscribe(false);
concurrency::CurrentScheduler::Detach();
#endif
return 0;
}
Following code for repro (and workaround)
#include "stdafx.h"
#include <cpprest/http_listener.h>
#include <cpprest/filestream.h>
using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;
// Set this to 1 to show the fix
// Setting 0 will cause hang on single core machines (like small Azure instances)
#define SET_MIN_CONCURRENCY 0
int _tmain(int argc, _TCHAR* argv[])
{
#if SET_MIN_CONCURRENCY
// This policy fixes the issue
concurrency::CurrentScheduler::Create(Concurrency::SchedulerPolicy(1, 2, Concurrency::MinConcurrency));
// oversubscribe isn't sufficient
//concurrency::Context::Oversubscribe(true);
#endif
http_listener listener(U("http://+:80/test"));
listener.support(methods::GET, [&](http_request request)
{
request.reply(status_codes::OK, U("Hello, world"));
});
puts("Opening http://localhost/test for serving");
listener.open().wait();
puts("Press [enter] to quit");
std::string line;
std::getline(std::cin, line);
listener.close().wait();
#if SET_MIN_CONCURRENCY
//concurrency::Context::Oversubscribe(false);
concurrency::CurrentScheduler::Detach();
#endif
return 0;
}