這將是很長的後,因爲我想盡可能解釋和詳細說明我的問題。我是後端開發新手,現在我正在使用WCF,C#和.NET Framework 4.5開發安全(https)REST Web服務。對於這種情況,我使用在IIS中配置的自簽名證書。 我的問題是,服務主機似乎隨機崩潰,沒有任何異常或消息在跟蹤日誌。我在Windows Server 2012 R2上的VMware中進行了測試。在其他操作系統上(WS 2008,WS 2010)似乎工作正常。 這裏是我如何與註冊事件處理程序的故障狀態啓動服務:意外的Windows服務器2012 R2上的WCF服務崩潰
private ServiceHost listener = null;
public void Start(byte[] certData, string certPassword) {
int port = Config.MobileServicePort;
SslCertificate cert = new SslCertificate();
cert.RemoveCertificate(port);
cert.InstallCertificate(port, certData, certPassword);
Uri uri = new Uri(string.Format("https://127.0.0.1:{0}/{1}", port, "MobileService"));
if (listener != null) {
listener.Close();
listener = null;
}
listener = new ServiceHost(typeof(MobileService), uri);
listener.Faulted += FaultedStateHandling;
try {
listener.Open();
Logger.System().Info("REGISTER service at '{0}'", uri.ToString());
} catch (TimeoutException timeEx) {
Logger.System().Error("Open timeout exception: " + timeEx);
} catch (CommunicationException comEx) {
Logger.System().Error("Open communication exception: " + comEx);
} catch (Exception ex) {
Logger.System().Error(ex);
}
}
這裏是處理器的故障狀態:
private void FaultedStateHandling(object sender, EventArgs e) {
try {
Logger.System().Info("<------------***Mobile service Faulted****------------>");
listener.Abort();
listener.Faulted -= new EventHandler(FaultedStateHandling);
int port = Config.MobileServicePort;
Uri uri = new Uri(string.Format("https://127.0.0.1:{0}/{1}", port, "MobileService"));
listener = new ServiceHost(typeof(MobileService), uri);
listener.Faulted += new EventHandler(FaultedStateHandling);
listener.Open();
Logger.System().Info("<------------***Mobile service Restarted****------------>");
} catch (Exception ex) {
Logger.System().Error(ex);
}
}
這是我的app.config配置MessageLogging和跟蹤:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Critical,Information,ActivityTracing"
propagateActivity="true">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\log\TraceMessages.svclog" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\log\MessageLogging.svclog" />
</listeners>
</source>
</sources>
<trace autoflush="true" />
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="false"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="500"
maxSizeOfMessageToLog="5000"/>
</diagnostics>
<services>
<service behaviorConfiguration="MobileBehavior" name="noq.Mobile.Service.MobileService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpTransportSecurity"
behaviorConfiguration="REST"
contract="noq.Mobile.Service.IMobileService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange" />
<!--<host>
<baseAddresses>
<add baseAddress="http://localhost:11082/MobileService/" />
</baseAddresses>
</host>-->
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpTransportSecurity" maxBufferSize="1000000000"
maxReceivedMessageSize="1000000000">
<readerQuotas maxStringContentLength="1000000000" />
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MobileBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
我用這個問題存貨了大約1個月,並且找不到解決方案。我嘗試了很多配置,看起來問題更深入。我檢查了MSDN論壇和support.microsoft。在那裏我發現了類似的問題,但不一樣,他們提供了一些熱修復,如that。 這個問題可能與虛擬機配置有關嗎? 在此先感謝您並提供任何建議。
事件日誌(應用程序日誌或系統日誌)告訴你什麼? – rene
嗨,@rene,所以在WCF跟蹤中,我發現了一條錯誤消息:無法找到與綁定BasicHttpBinding的端點匹配方案http的基地址。註冊的基地址方案是[]。 – Artiom
@rene [微軟提供的修補程序](https://support.microsoft.com/en-us/kb/2537715),但僅適用於WS2008R2。我試圖通過在app.config文件中設置BaseAddress來解決此問題,使得此錯誤不再出現,但它不能解決問題服務仍然崩潰的問題。我嘗試了幾乎所有類型的服務配置,但仍然沒有結果。在系統日誌和應用程序日誌中 - 什麼也沒有,服務只是停止響應從客戶端(Android),當服務關閉時,在日誌中看到消息:握手期間發生未知錯誤。 – Artiom