C# URL 保留主机问题

C# URL Reservation Host issue

我遇到了这个奇怪的问题,我确实将 Reserve URL 添加到 ACL 列表(以编程方式),但是当执行程序时,我仍然收到错误消息 "Access Denied"。

    public void initRest() {
        // Add ACL Service Exception
        addACLServiceException();
        /*
         * initRest will lunch a separate thread that will be responsible for REST service
         */
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            RestServicesImpl services = new RestServicesImpl();
            WebHttpBinding binding = new WebHttpBinding();
            WebHttpBehavior behavior = new WebHttpBehavior();
            WebServiceHost _serviceHost = new WebServiceHost(services, new Uri("http://localhost:8000/RestService"));
            _serviceHost.AddServiceEndpoint(typeof(RestServices), binding, "");
            _serviceHost.Open();
            Console.ReadKey();
            _serviceHost.Close();
        }).Start();            
    }

    private void addACLServiceException() {
        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/K netsh http add urlacl url = http://+:8000/RestService/ user=%USERNAME%";
        startInfo.Verb = "runas";
        process.StartInfo = startInfo;
        process.Start();
    }

运行 netsh http show urlacl 来自 CMD window(作为管理员)我可以看到保留的 URL 确实在它应该在的地方

 Reserved URL            : http://+:8000/RestService/
    User: DESKTOP-F8O3V2Q\Boss
        Listen: Yes
        Delegate: No
        SDDL: D:(A;;GX;;;S-1-5-21-990234104-2306344669-2817477651-1001)

嘿...所以似乎线程执行得更快,然后添加了 ACL 异常。一个简单的线程睡眠命令就这样解决了

public void initRest() {
    // Add ACL Service Exception
    addACLServiceException();
    Thread.Sleep(2000);
    ... rest of code