【发布时间】:2016-06-08 07:24:48
【问题描述】:
这将是很长的帖子,因为我想尽可能详细地解释我的问题。我是后端开发的新手,现在我正在使用 WCF、C# 和 .NET Framework 4.5 开发一个安全的 (https) REST Web 服务。对于这种情况,我使用在 IIS 中配置的自签名证书。 我的问题是服务主机似乎随机崩溃,在跟踪日志中没有任何异常或消息。我在 Windows Server 2012 R2 上的 VMware 中对其进行了测试。在其他操作系统(WS 2008、WS 2010)上似乎工作正常。 以下是我如何通过为故障状态注册 EventHandler 来启动服务:
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);
}
}
这是我为 MessageLogging 和 Tracing 配置的 app.config:
<?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,所以在 WCF 跟踪中我发现了一条错误消息,例如:找不到与绑定 BasicHttpBinding 的端点的方案 http 匹配的基地址。注册的基地址方案是 []。
-
@rene hotfix from microsoft 但仅适用于 WS2008R2。我试图通过在 app.config 文件中设置 BaseAddress 来修复它,使这个错误不再出现,但它不能解决问题服务仍然崩溃。我尝试了几乎所有类型的服务配置,但仍然没有结果。在系统日志和应用程序日志中 - 什么都没有,服务只是停止响应。从客户端(Android),当服务关闭时,在日志中我看到消息:握手期间出现未知错误。
标签: c# web-services wcf rest