简体   繁体   English

casablanca http_listener “将 url 添加到 url 组时出错”

[英]casablanca http_listener “Error adding url to url group”

I am currently trying to implement a REST interface with casablanca but I keep getting "Error adding url to url group".我目前正在尝试使用 casablanca 实现 REST 接口,但我不断收到“将 url 添加到 url 组时出错”的消息。 And I don't really know how to fix that.我真的不知道如何解决这个问题。 Here is my main method:这是我的主要方法:

    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");
}

I am now trying to figure out where the problem could be but I didn't get far.我现在试图找出问题可能出在哪里,但我没有走远。 I am setting the Endpoint and creating the http_listener like this (Server class extends BaseController):我正在设置端点并像这样创建 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());
    }

When the Server accepts, the supporting methods are being set on the listener当服务器接受时,正在侦听器上设置支持方法

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));
}

The exception is thrown in the http_listener.cpp open() method:在 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;
    });
}

I can't find any help elsewhere and I can't seem to figure out why it fails to open.我在其他地方找不到任何帮助,我似乎无法弄清楚它为什么无法打开。 Any help would be appreciated!任何帮助,将不胜感激! Thanks.谢谢。

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

For Windows,you have another option that requires two steps.对于 Windows,您还有另一个需要两个步骤的选项。

1) Change the URI that you are listening to http://*:41004 1) 更改您正在侦听的 URI http://*:41004

2) Add an application manifest to the application you are building that requests admin privileges when the program runs. 2) 向您正在构建的应用程序添加一个应用程序清单,该应用程序在程序运行时请求管理员权限。

In Visual Studio, you need to add a post build step settings of the project.在 Visual Studio 中,您需要添加项目的构建后步骤设置。 Assume you have application called "MyApplication.exe"假设您有名为“MyApplication.exe”的应用程序

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

The manifest file would be named "MyApplication.exe.manifest" and would contain the following contents:清单文件将命名为“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>

The RC file for MyApplication.exe must also point to the manifest file by having in the file. MyApplication.exe 的 RC 文件还​​必须通过文件中的 指向清单文件。

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

More about application manifests can be found here: https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests有关应用程序清单的更多信息,请访问: https : //docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM