2012-02-20 153 views
1

環境:Windows XP,Visual Studio 2010中,IIS 安裝。WCF服務,在WCF SVC主機的工作,不工作作爲Windows服務

我已經開發了一個WCF服務作爲WCFSvcHost託管時工作正常庫;我可以連接到它,更新客戶的服務參考,作品。

我無法承載它作爲Windows服務,雖然。該服務安裝好,我可以附加到它並打破我的代碼。爲了調試主機,我將代碼移到了OnResume()(OnStart什麼都不做)。

這裏是我的onResume(名稱已更改爲保護無辜者):

protected override OnResume() 
{ 
    if (_selfHost == null) 
    { 
     _selfHost = new ServiceHost(typeof(MyService)); 
     _selfHost.Open(); 
    } 
} 

當通過Open()方法步進,我得到以下異常:

System.InvalidOperationException was unhandled by user code 
    Message=The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address. 
    Source=System.ServiceModel 
    StackTrace: 
     at System.ServiceModel.Description.ServiceMetadataBehavior.EnsureGetDispatcher(ServiceHostBase host, ServiceMetadataExtension mex, Uri url, String scheme) 
     at System.ServiceModel.Description.ServiceMetadataBehavior.CreateHttpGetEndpoints(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex) 
     at System.ServiceModel.Description.ServiceMetadataBehavior.ApplyBehavior(ServiceDescription description, ServiceHostBase host) 
     at System.ServiceModel.Description.ServiceMetadataBehavior.System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) 
     at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) 
     at System.ServiceModel.ServiceHostBase.InitializeRuntime() 
     at System.ServiceModel.ServiceHostBase.OnBeginOpen() 
     at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open() 
     at MyService.StartService() 
     at MyService.OnContinue() 
     at System.ServiceProcess.ServiceBase.DeferredContinue() 
    InnerException: 

沒有量改變我的配置文件似乎解決了這個問題:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" /> 
    </startup> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="MyService"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/MyService/" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="wsHttpBinding" contract="MyLib.IMyService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="http://localhost:8732/MyService/mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="True" /> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

作爲一個測試,我試圖設置httpG etEnabled爲False,並刪除MEX終結點,但只有移到別處的問題:錯誤信息消失並啓動服務,但後來我的客戶端不能調用服務:

System.ServiceModel.EndpointNotFoundException was caught 
    Message=There was no endpoint listening at http://localhost:8732/MyService/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. 
[...] 

    InnerException: System.Net.WebException 
     Message=The remote server returned an error: (400) Bad Request. 
     Source=System 
     StackTrace: 
      at System.Net.HttpWebRequest.GetResponse() 
      at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
     InnerException: 

任何幫助嗎?

+0

你有沒有嘗試在控制檯應用程序託管?不是爲了替代服務,而只是爲了確保它能夠工作? – 2012-02-20 22:45:59

+0

另外 - 我會通過一個非常簡單的例子來得到一個WCF服務作爲一個Web服務來運行,看看這樣做是否有助於你看到你在做什麼不同。請參閱:http://msdn.microsoft.com/en-us/library/ms733069.aspx – 2012-02-20 22:48:00

+0

特里:聰明的想法。我得到與Windows服務相同的錯誤。 – dlesage 2012-02-20 23:23:09

回答

0

明白了。

我曾錯誤地複製從配置文件中的相關部分,而不是一次,而是兩次(無論是在Windows服務和控制檯應用程序)。

感謝特里。你讓我沿着正確的路線思考。