【问题标题】:casablanca http_listener "Error adding url to url group"casablanca http_listener“将 url 添加到 url 组时出错”
【发布时间】:2018-06-21 15:11:50
【问题描述】:

我目前正在尝试使用 casablanca 实现 REST 接口,但我不断收到“将 url 添加到 url 组时出错”。我真的不知道如何解决这个问题。这是我的主要方法:

    int main(int argc, char* argv[])
{
    InterruptHandler::hookSIGINT();

    Server server;
    server.setEndpoint(L"http", 41004, L"/api/v1");

    try {
        // wait for server initialization...
        server.accept().wait();
        std::wcout << L"Modern C++ Server now listening for requests at: " << server.endpoint() << '\n';

        InterruptHandler::waitForUserInterrupt();

        server.shutdown().wait();
    }
    catch (std::exception & e) {
        std::cerr << e.what() << '\n'; //this is returning "Error adding url to url group"
    }

    system("pause");
}

我现在正试图找出问题可能出在哪里,但我没有走远。我正在设置端点并像这样创建 http_listener(服务器类扩展 BaseController):

void BaseController::setEndpoint(const std::wstring &scheme, const int port, const std::wstring &path) {
        uri_builder endpointBuilder;

        endpointBuilder.set_scheme(scheme);
        endpointBuilder.set_host(L"0.0.0.0");
        endpointBuilder.set_port(port); //41004
        endpointBuilder.set_path(path);

        _listener = http_listener(endpointBuilder.to_uri());
    }

当服务器接受时,正在监听器上设置支持方法

void Server::initRestOpHandlers() {
    _listener.support(methods::GET, std::bind(&Server::handleGet, this, std::placeholders::_1));
    _listener.support(methods::POST, std::bind(&Server::handlePost, this, std::placeholders::_1));
}

http_listener.cpp open()方法中抛出异常:

pplx::task<void> details::http_listener_impl::open()
{
    // Do nothing if the open operation was already attempted
    // Not thread safe
    if (!m_closed) return pplx::task_from_result();

    if ( m_uri.is_empty() )
        throw std::invalid_argument("No URI defined for listener.");
    m_closed = false;

    return web::http::experimental::details::http_server_api::register_listener(this).then([this](pplx::task<void> openOp)
    {
        try
        {
            // If failed to open need to mark as closed.
            openOp.wait();
        }
        catch(...)
        {
            m_closed = true;
            throw;
        }
        return openOp;
    });
}

我在其他地方找不到任何帮助,而且我似乎无法弄清楚为什么它无法打开。任何帮助,将不胜感激!谢谢。

【问题讨论】:

    标签: c++ visual-studio httplistener casablanca cpprest-sdk


    【解决方案1】:

    好的,我自己解决了...我必须在我的 BaseController 中使用 127.0.0.1 作为主机

    【讨论】:

      【解决方案2】:

      对于 Windows,您还有另一个需要两个步骤的选项。

      1) 更改您正在收听的 URI http://*:41004

      2) 将应用程序清单添加到您正在构建的应用程序中,该应用程序在程序运行时请求管理员权限。

      在 Visual Studio 中,您需要添加项目的构建后步骤设置。假设您有名为“MyApplication.exe”的应用程序

      "mt.exe" -manifest \"MyApplication.exe.manifest\" -outputresource:"$(TargetDir)$(TargetFileName)"\;\#1
      

      清单文件将命名为“MyApplication.exe.manifest”并包含以下内容:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
        <assemblyIdentity version="1.0.0.0"
           name="MyApplication"
           type="win32"/> 
        <description>My Application</description> 
        <!-- Identify the application security requirements. -->
        <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
          <security>
            <requestedPrivileges>
              <requestedExecutionLevel level="requireAdministrator" uiAccess="false">
                </requestedExecutionLevel>
              </requestedPrivileges>
              <applicationRequestMinimum>
                  <defaultAssemblyRequest permissionSetReference="FullTrust" />
                  <PermissionSet version="1" ID="FullTrust" Unrestricted="true" />
              </applicationRequestMinimum>
             </security>
        </trustInfo>
      </assembly>
      

      MyApplication.exe 的 RC 文件还​​必须通过在文件中的来指向清单文件。

      #define MANIFEST_RESOURCE_ID 1
      MANIFEST_RESOURCE_ID RT_MANIFEST "MyApplication.exe.manifest"
      

      更多关于应用程序清单的信息可以在这里找到: https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 2014-11-12
        • 2022-09-30
        • 1970-01-01
        相关资源
        最近更新 更多