2017-10-18 128 views
0

我有这个奇怪的问题,我添加保留URL到ACL列表(编程方式如此),但是当程序执行时,我仍然收到错误消息“访问被拒绝”。C#网址保留主机问题

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

从CMD窗口运行netsh http show urlacl(如管理员)我可以看到预留的网址确实是它应该是

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) 

回答

0

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

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