2011-03-29 42 views
0

我創建了一個Windows服務。Windows服務錯誤在啓動服務時

當我在我的本地計算機上安裝它之後嘗試啓動我的服務時,它給了我錯誤。

enter image description here

我的其他窗口服務工作做好,所以這個問題是不相關的Windows,但事做我的服務只有這個特定的服務給出了這樣的錯誤。

什麼可能是錯的?

這是我的Windows服務:

namespace TempWindowService 
{ 
    public partial class Service1 : ServiceBase 
    { 
     System.Threading.Thread _thread; 
     public Service1() 
     { 
      InitializeComponent(); 
     } 
     // System.Timers.Timer tm = new System.Timers.Timer(10000); 
     protected override void OnStart(string[] args) 
     { 
      TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient(); 
      //newService.BatchProcess(); 
      _thread = new Thread(new ThreadStart(newService.BatchProcess)); 
      _thread.Start(); 

      // tm.Interval = 1000; 
      //tm.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); 
      // tm.AutoReset = true; 
      // tm.Enabled = true; 

     } 

     public void StartNew() 
     { 
      TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient(); 
      newService.BatchProcess(); 
     } 
     private static void TimerElapsedEvent(object source, ElapsedEventArgs e) 
     { 

     } 


     protected override void OnStop() 
     { 

     } 
    } 
} 

我通過添加服務引用調用從Windows服務的WebService

這是在事件查看器

Service cannot be started. System.InvalidOperationException: An endpoint configuration section for contract 'MyServ.MyServSoap' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name. 
    at System.ServiceModel.Description.ConfigLoader.LookupChannel(String configurationName, String contractName, Boolean wildcard) 
什麼顯示錯誤

什麼可能是錯的?

+0

檢查是否有記錄的任何錯誤事件查看器。 – 2011-03-29 12:02:44

+0

如果該服務在能夠附加調試器之前停止,請查看[this]的回答(http://stackoverflow.com/questions/5266971/null-reference-exception/5267016#5267016)打破。 – 2011-03-29 12:07:41

+0

@Sachin和@Ole_Brun:感謝您的輸入。我已將eventViewer錯誤添加到我的問題中。請檢查一下,請告訴我可能會出現什麼問題? – 2011-03-29 12:50:19

回答

0

看看EventViewer,我相信它會包含一些與此錯誤有關的有用信息。此外,您還可以通過使用方法調試它在所示:

How to: Debug Windows Service Applications

你會發現很多有關這個錯誤:

http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=An+endpoint+configuration+section+for+contract+could+not+be+loaded+because+more+than+one+endpoint+configuration+for+that+contract+was+found

+0

如何查看EventViewer?它位於哪裏?我是新手,不太瞭解 – 2011-03-29 12:33:12

+0

計算機 - >管理 - >事件查看器 – 2011-03-29 12:37:15

+0

感謝您的輸入。我已將eventViewer錯誤添加到我的問題中。請檢查出來,請告訴我什麼是錯的? – 2011-03-29 12:47:48

1

此行很可能引發異常:

TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient(); 

檢查您的配置文件是否存在且正確;事件日誌查看器會讓你知道問題是什麼。

考慮使用trycatch來查找啓動錯誤並以有用的方式報告它們。

+0

感謝您的意見。我如何查看eventLog?即使我如何使用'try'' catch'作爲上面的行呢?你可以用一些代碼來解釋它嗎? – 2011-03-29 12:31:31

+0

我已將eventViewer錯誤添加到我的問題。請檢查一下,請告訴我可能會出現什麼問題? – 2011-03-29 12:47:06

+1

檢查你的配置文件,這是不正確的服務。檢查你是否有配置文件,嘗試重新創建Web服務引用,或者通過服務調用創建控制檯應用程序失敗,調試(更容易),然後再次將其轉換爲服務。 – 2011-03-29 13:01:32

0

如果事件查看器不幫你,你應該嘗試使用下面的方法之一是調試:

  • 將一個「Debugger.Break」的OnStart下,用調試器連接。
  • 在OnStart下放置10秒睡眠並在延遲期間連接調試器。
+2

這有點隨意。相反,我建議:'while(!Debugger.IsAttached){Thread.Sleep(1000); } Debugger.Break();'這種方式等待調試器連接 – 2011-03-29 13:00:26

+0

好的調用@ Debugger.IsAttached :) – VitalyB 2011-03-29 13:04:20

+0

只需記住在部署時取出代碼,否則你的服務將等待調試器連接。最好使它成爲一個.config選項,WaitForDebugger等 – 2011-03-29 15:20:06

0

如果你在爲你的服務配置文件中的同一合同類型的多個端點,你需要指定你是哪一個興趣:

TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient("EndPointName");