2017-10-18 127 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