C# WCF 自托管打开但被浏览器拒绝
C# WCF self-hosting opened but refused by browsers
我在 Windows 服务中自托管了一个 WCF,运行 作为本地系统。
WCF 配置以编程方式完成(意味着没有 app.config 配置)。
此 WCF 服务主机已正确打开,没有任何异常,我可以在调试模式下看到 2 个最终端点(元数据的主端点和 mex 端点)。
当我打开网络浏览器 (IE) 到我的主要端点“https://myserver.mydomain.fr:443/endpoint”时,出现错误 "This page can't be displayed"。如果我 运行 一个 IE 网络诊断,我得到: "This driver or remote ressource is not configured to accept connections on port https"
Url 已通过 netsh http add urlacl url=https://myserver.mydomain.fr:443/endpoint/ 保留并且证书已正确附加到端口 443(具有此 Windows 服务和证书指纹)。
你知道我应该把精力放在哪里吗?在 WCF 配置上?
它工作但几天前停止工作...
UPDATE1:这是我的代码。
我的主要 ServiceHost class 是:
public class CustomServiceHost : ServiceHost
{
private string _AssembyIDOfWindowsService=String.Empty;
private string _mainEndPointRelativeAddress = String.Empty;
private string _mexRelativeAddress = "mex";
public CustomServiceHost(Type serviceType, Uri[] baseAddresses, string mainEndPointRelativeAddress, string mexRelativeAddress)
: base(serviceType, baseAddresses)
{
_mainEndPointRelativeAddress = mainEndPointRelativeAddress;
_mexRelativeAddress = mexRelativeAddress;
}
protected override void InitializeRuntime()
{
//Add Service debug behavior
AddServiceDebugBehavior();
//Add main endpoint and mex endpoint
AddEndpoints();
//Configure throttling: means number max of connections, sessions and objects
ConfigureThrottling();
//Allow clients to connect without client certificates
Credentials.ClientCertificate.Authentication.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
Base.InitializeRuntime(); //My error was here... WTF... So easy...
}
/// <summary>
/// Add main endpoint on wer server and mex endpoint for operations signatures
/// </summary>
private void AddEndpoints()
{
BasicHttpsBinding basicHttpsBinding = WcfHelpers.ConfigureBasicHttpsBinding();
foreach (Uri address in this.BaseAddresses)
{
//Create endpoint with or without an endpoint name (ie https://myserver.mydomain.fr/443 or https://myserver.mydomain.fr/443/endpoint )
ServiceEndpoint endpoint = new ServiceEndpoint(
ContractDescription.GetContract(Description.ServiceType),
basicHttpsBinding,
new EndpointAddress(address + _mainEndPointRelativeAddress));
//Add main service endpoint to service
AddServiceEndpoint(endpoint);
//adding behavior
AddServiceMetadataBehavior();
//adding mex endpoint (address.AbsoluteUri always finish with \ so not needed between base address and mexRelativeEndpoint
AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpsBinding(),
address.AbsoluteUri + _mexRelativeAddress);
}
}
/// <summary>
/// Add debug behavior if not already done
/// </summary>
private void AddServiceDebugBehavior()
{
ServiceDebugBehavior debugBehavior = Description.Behaviors.Find<ServiceDebugBehavior>();
if (debugBehavior == null)
{
Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
if (!debugBehavior.IncludeExceptionDetailInFaults)
debugBehavior.IncludeExceptionDetailInFaults = true;
}
}
/// <summary>
/// Add service metadata behavior
/// </summary>
private void AddServiceMetadataBehavior()
{
ServiceMetadataBehavior metadataBehavior = Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
serviceMetadataBehavior.HttpsGetEnabled = true;
Description.Behaviors.Add(serviceMetadataBehavior);
}
}
/// <summary>
/// Configure max numbers of concurrent accesses (calls, instances and sessions)
/// </summary>
private void ConfigureThrottling()
{
ServiceThrottlingBehavior throttleBehavior = Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttleBehavior != null) return;
throttleBehavior = new ServiceThrottlingBehavior
{
MaxConcurrentCalls = 100, // MaxConcurrentCalls should be set to less than the SQL connection pool size in queued scenarios
MaxConcurrentInstances = 100,
MaxConcurrentSessions = 100
};
Description.Behaviors.Add(throttleBehavior);
}
}
要配置 https 绑定,我使用这个:
/// <summary>
/// Used by server and clients to define common bindings (thats why its a static method)
/// </summary>
public class WcfHelpers
{
public static BasicHttpsBinding ConfigureBasicHttpsBinding()
{
BasicHttpsBinding basicHttpsBinding;
basicHttpsBinding = new BasicHttpsBinding
{
Name = "myBasicHttpsBinding",
OpenTimeout = new TimeSpan(0, 10, 0),
CloseTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 10, 0),
MaxBufferPoolSize = 104857600,
MaxReceivedMessageSize = 104857600,
Namespace = "https://myserver.mydomain.fr/443/endpoint",
ReaderQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 104857600,
MaxStringContentLength = 104857600,
MaxArrayLength = 104857600,
MaxBytesPerRead = 104857600,
MaxNameTableCharCount = 104857600
},
Security =
{
Mode = BasicHttpsSecurityMode.Transport
}
};
return basicHttpsBinding;
}
}
最后我像这样创建我的 ServiceHost:
//Create my ServiceHost
CustomServiceHost _serviceHost = new CustomServiceHost(typeof(WCF_Service_Library.Service)
, new Uri[] { new Uri("https://myserver.mydomain.fr:443/") }
, "endpoint"
, "mex");
//Attach a selected X509 certificate (X509Certificate2 certFound)
_serviceHost.Credentials.ServiceCertificate.Certificate = certFound;
//Open ServiceHost
_serviceHost.Open();
...
//Close ServiceHost
_serviceHost.Close();
UPDATE2:WCF 日志文件是:
<events><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1825288Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1985712Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Construire ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Construct</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524326</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1985712Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.GetServiceElement.aspx</TraceIdentifier><Description>Obtenir ServiceElement.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/ServiceConfigurationTraceRecord"><FoundServiceElement>False</FoundServiceElement></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2135766Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2135766Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Construire ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Construct</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Ouvrir ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Open</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524333</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.ServiceHostBaseAddresses.aspx</TraceIdentifier><Description>Adresses de base ServiceHost.</Description><AppDomain>Windows Service</AppDomain><Source>Windows_Service.CustomServiceHost/45867655</Source><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/CollectionTraceRecord"><BaseAddresses><Address>https://myserver.mydomain.fr:443/</Address></BaseAddresses></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Ouvrir ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Open</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Fermer ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524333</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.ServiceHostBaseAddresses.aspx</TraceIdentifier><Description>Adresses de base ServiceHost.</Description><AppDomain>Windows Service</AppDomain><Source>Windows_Service.CustomServiceHost/45867655</Source><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/CollectionTraceRecord"><BaseAddresses><Address>https://myserver.mydomain.fr:443/</Address></BaseAddresses></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Fermer ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131073</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.4795464Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="2" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.AppDomainUnload.aspx</TraceIdentifier><Description>Déchargement d'un domaine d'application.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><AppDomain.FriendlyName>Windows Service</AppDomain.FriendlyName><ProcessName>Windows Service</ProcessName><ProcessId>6676</ProcessId></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131073</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.4795464Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="2" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord Severity="Information" Channel="Debug" xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.AppDomainUnload.aspx</TraceIdentifier><Description>Déchargement AppDomain. AppDomain.FriendlyName Windows Service, ProcessName Windows Service, ProcessId 6676.</Description><AppDomain>Windows Service</AppDomain></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent></events>
感谢您的帮助!
重新安装并重启主机服务。
实现以下 wcf 处理程序
serviceHost.Opening += new EventHandler(serviceHost_Opening);
serviceHost.Opened += new EventHandler(serviceHost_Opened);
serviceHost.Closing += new EventHandler(serviceHost_Closing);
serviceHost.Closed += new EventHandler(serviceHost_Closed);
serviceHost.Faulted += new EventHandler(serviceHost_Faulted);
serviceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(serviceHost_UnknownMessageReceived);
serviceHost.Open();
同时检查 windows 事件和 netstat
netstat -aon | find ":443"
查找端口绑定是否实际处于活动状态以及哪个程序属于该 PID。
请注意,服务主机打开后不要关闭(在windows服务OnStart
中打开wcp,在OnStop
中关闭)
另请参阅 specifying endpoint address in code
的 msdn 示例
Uri baseAddress = new Uri("http://localhost:8000/HelloService");
string address = "http://localhost:8000/HelloService/MyService";
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService), baseAddress))
{
serviceHost.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(), address);
serviceHost.Open();
Console.WriteLine("Press <enter> to terminate service");
Console.ReadLine();
serviceHost.Close();
}
最后一点,我希望你应该添加
base.InitializeRuntime();
在protected override void InitializeRuntime()
结束
我在 Windows 服务中自托管了一个 WCF,运行 作为本地系统。 WCF 配置以编程方式完成(意味着没有 app.config 配置)。
此 WCF 服务主机已正确打开,没有任何异常,我可以在调试模式下看到 2 个最终端点(元数据的主端点和 mex 端点)。
当我打开网络浏览器 (IE) 到我的主要端点“https://myserver.mydomain.fr:443/endpoint”时,出现错误 "This page can't be displayed"。如果我 运行 一个 IE 网络诊断,我得到: "This driver or remote ressource is not configured to accept connections on port https"
Url 已通过 netsh http add urlacl url=https://myserver.mydomain.fr:443/endpoint/ 保留并且证书已正确附加到端口 443(具有此 Windows 服务和证书指纹)。
你知道我应该把精力放在哪里吗?在 WCF 配置上? 它工作但几天前停止工作...
UPDATE1:这是我的代码。
我的主要 ServiceHost class 是:
public class CustomServiceHost : ServiceHost
{
private string _AssembyIDOfWindowsService=String.Empty;
private string _mainEndPointRelativeAddress = String.Empty;
private string _mexRelativeAddress = "mex";
public CustomServiceHost(Type serviceType, Uri[] baseAddresses, string mainEndPointRelativeAddress, string mexRelativeAddress)
: base(serviceType, baseAddresses)
{
_mainEndPointRelativeAddress = mainEndPointRelativeAddress;
_mexRelativeAddress = mexRelativeAddress;
}
protected override void InitializeRuntime()
{
//Add Service debug behavior
AddServiceDebugBehavior();
//Add main endpoint and mex endpoint
AddEndpoints();
//Configure throttling: means number max of connections, sessions and objects
ConfigureThrottling();
//Allow clients to connect without client certificates
Credentials.ClientCertificate.Authentication.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
Base.InitializeRuntime(); //My error was here... WTF... So easy...
}
/// <summary>
/// Add main endpoint on wer server and mex endpoint for operations signatures
/// </summary>
private void AddEndpoints()
{
BasicHttpsBinding basicHttpsBinding = WcfHelpers.ConfigureBasicHttpsBinding();
foreach (Uri address in this.BaseAddresses)
{
//Create endpoint with or without an endpoint name (ie https://myserver.mydomain.fr/443 or https://myserver.mydomain.fr/443/endpoint )
ServiceEndpoint endpoint = new ServiceEndpoint(
ContractDescription.GetContract(Description.ServiceType),
basicHttpsBinding,
new EndpointAddress(address + _mainEndPointRelativeAddress));
//Add main service endpoint to service
AddServiceEndpoint(endpoint);
//adding behavior
AddServiceMetadataBehavior();
//adding mex endpoint (address.AbsoluteUri always finish with \ so not needed between base address and mexRelativeEndpoint
AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpsBinding(),
address.AbsoluteUri + _mexRelativeAddress);
}
}
/// <summary>
/// Add debug behavior if not already done
/// </summary>
private void AddServiceDebugBehavior()
{
ServiceDebugBehavior debugBehavior = Description.Behaviors.Find<ServiceDebugBehavior>();
if (debugBehavior == null)
{
Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
if (!debugBehavior.IncludeExceptionDetailInFaults)
debugBehavior.IncludeExceptionDetailInFaults = true;
}
}
/// <summary>
/// Add service metadata behavior
/// </summary>
private void AddServiceMetadataBehavior()
{
ServiceMetadataBehavior metadataBehavior = Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
serviceMetadataBehavior.HttpsGetEnabled = true;
Description.Behaviors.Add(serviceMetadataBehavior);
}
}
/// <summary>
/// Configure max numbers of concurrent accesses (calls, instances and sessions)
/// </summary>
private void ConfigureThrottling()
{
ServiceThrottlingBehavior throttleBehavior = Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttleBehavior != null) return;
throttleBehavior = new ServiceThrottlingBehavior
{
MaxConcurrentCalls = 100, // MaxConcurrentCalls should be set to less than the SQL connection pool size in queued scenarios
MaxConcurrentInstances = 100,
MaxConcurrentSessions = 100
};
Description.Behaviors.Add(throttleBehavior);
}
}
要配置 https 绑定,我使用这个:
/// <summary>
/// Used by server and clients to define common bindings (thats why its a static method)
/// </summary>
public class WcfHelpers
{
public static BasicHttpsBinding ConfigureBasicHttpsBinding()
{
BasicHttpsBinding basicHttpsBinding;
basicHttpsBinding = new BasicHttpsBinding
{
Name = "myBasicHttpsBinding",
OpenTimeout = new TimeSpan(0, 10, 0),
CloseTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 10, 0),
MaxBufferPoolSize = 104857600,
MaxReceivedMessageSize = 104857600,
Namespace = "https://myserver.mydomain.fr/443/endpoint",
ReaderQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 104857600,
MaxStringContentLength = 104857600,
MaxArrayLength = 104857600,
MaxBytesPerRead = 104857600,
MaxNameTableCharCount = 104857600
},
Security =
{
Mode = BasicHttpsSecurityMode.Transport
}
};
return basicHttpsBinding;
}
}
最后我像这样创建我的 ServiceHost:
//Create my ServiceHost
CustomServiceHost _serviceHost = new CustomServiceHost(typeof(WCF_Service_Library.Service)
, new Uri[] { new Uri("https://myserver.mydomain.fr:443/") }
, "endpoint"
, "mex");
//Attach a selected X509 certificate (X509Certificate2 certFound)
_serviceHost.Credentials.ServiceCertificate.Certificate = certFound;
//Open ServiceHost
_serviceHost.Open();
...
//Close ServiceHost
_serviceHost.Close();
UPDATE2:WCF 日志文件是:
<events><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1825288Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1985712Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Construire ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Construct</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524326</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:55:54.1985712Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.GetServiceElement.aspx</TraceIdentifier><Description>Obtenir ServiceElement.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/ServiceConfigurationTraceRecord"><FoundServiceElement>False</FoundServiceElement></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2135766Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2135766Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{de1073e0-a063-4b78-a802-e2ea11c2711d}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Construire ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Construct</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Ouvrir ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Open</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524333</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.ServiceHostBaseAddresses.aspx</TraceIdentifier><Description>Adresses de base ServiceHost.</Description><AppDomain>Windows Service</AppDomain><Source>Windows_Service.CustomServiceHost/45867655</Source><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/CollectionTraceRecord"><BaseAddresses><Address>https://myserver.mydomain.fr:443/</Address></BaseAddresses></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:55:54.2765741Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{c002e9ab-b644-4f63-8efe-ce7b77e403e3}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Ouvrir ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Open</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" RelatedActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Start">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Start"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Fermer ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>524333</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.ServiceHostBaseAddresses.aspx</TraceIdentifier><Description>Adresses de base ServiceHost.</Description><AppDomain>Windows Service</AppDomain><Source>Windows_Service.CustomServiceHost/45867655</Source><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/CollectionTraceRecord"><BaseAddresses><Address>https://myserver.mydomain.fr:443/</Address></BaseAddresses></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Transfer">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" RelatedActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131085</EventID><Type>3</Type><SubType Name="Stop">0</SubType><Level>255</Level><TimeCreated SystemTime="2016-07-19T09:56:09.3075479Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{19dd45d9-e0f7-4ffe-aa8e-4aaf7b5b18ba}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="4" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Stop"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.ActivityBoundary.aspx</TraceIdentifier><Description>Limite d'activité.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><ActivityName>Fermer ServiceHost 'WCF_Service_Library.Service'.</ActivityName><ActivityType>Close</ActivityType></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131073</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.4795464Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="2" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.AppDomainUnload.aspx</TraceIdentifier><Description>Déchargement d'un domaine d'application.</Description><AppDomain>Windows Service</AppDomain><ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord"><AppDomain.FriendlyName>Windows Service</AppDomain.FriendlyName><ProcessName>Windows Service</ProcessName><ProcessId>6676</ProcessId></ExtendedData></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131073</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2016-07-19T09:56:09.4795464Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Windows Service" ProcessID="6676" ThreadID="2" /><Channel/><Computer>DEV01</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord Severity="Information" Channel="Debug" xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"><TraceIdentifier>http://msdn.microsoft.com/fr-FR/library/System.ServiceModel.Diagnostics.AppDomainUnload.aspx</TraceIdentifier><Description>Déchargement AppDomain. AppDomain.FriendlyName Windows Service, ProcessName Windows Service, ProcessId 6676.</Description><AppDomain>Windows Service</AppDomain></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent></events>
感谢您的帮助!
重新安装并重启主机服务。
实现以下 wcf 处理程序
serviceHost.Opening += new EventHandler(serviceHost_Opening);
serviceHost.Opened += new EventHandler(serviceHost_Opened);
serviceHost.Closing += new EventHandler(serviceHost_Closing);
serviceHost.Closed += new EventHandler(serviceHost_Closed);
serviceHost.Faulted += new EventHandler(serviceHost_Faulted);
serviceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(serviceHost_UnknownMessageReceived);
serviceHost.Open();
同时检查 windows 事件和 netstat
netstat -aon | find ":443"
查找端口绑定是否实际处于活动状态以及哪个程序属于该 PID。
请注意,服务主机打开后不要关闭(在windows服务OnStart
中打开wcp,在OnStop
中关闭)
另请参阅 specifying endpoint address in code
的 msdn 示例Uri baseAddress = new Uri("http://localhost:8000/HelloService");
string address = "http://localhost:8000/HelloService/MyService";
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService), baseAddress))
{
serviceHost.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(), address);
serviceHost.Open();
Console.WriteLine("Press <enter> to terminate service");
Console.ReadLine();
serviceHost.Close();
}
最后一点,我希望你应该添加
base.InitializeRuntime();
在protected override void InitializeRuntime()