我创建了一个 Windows 服务来托管一些 WCF 服务,
但是当我启动它时,它会停止并显示一条消息:
我检查了 windows 日志查看器,没有任何错误
并且我之前在控制台应用程序上测试了所有内容并且它正在工作。
我的代码是:
ServiceHost host1;
ServiceHost host2;
ServiceHost host3;
public ServicesHost()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (host1 != null)
host1.Close();
if (host2 != null)
host2.Close();
if (host3 != null)
host3.Close();
host1 = new ServiceHost(typeof(Service1));
host1.Open();
host2 = new ServiceHost(typeof(Service2));
host2.Open();
host3 = new ServiceHost(typeof(Service3));
host3.Open();
}
protected override void OnStop()
{
host1.Close();
host1 = null;
host2.Close();
host2 = null;
host3.Close();
host3 = null;
}
应用程序配置:
<?xml version="1.0" encoding="utf-8" ?>
<service behaviorConfiguration="MyServiceBehavior"
name="Service2">
<endpoint address=""
binding="basicHttpBinding"
contract="IService2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Service2" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="MyServiceBehavior"
name="Service3">
<endpoint address=""
binding="basicHttpBinding"
contract="IService3">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Service3" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
编辑:我有安装程序:
[RunInstaller(true)]
public partial class ServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller process;
private System.ServiceProcess.ServiceInstaller service;
public ServiceInstaller()
{
process = new System.ServiceProcess.ServiceProcessInstaller();
process.Account = System.ServiceProcess.ServiceAccount.NetworkService;
service = new System.ServiceProcess.ServiceInstaller();
service.ServiceName = "WCFHostService";
service.DisplayName = "WCFHostService";
service.Description = "WCF Service Hosted";
service.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
Installers.Add(process);
Installers.Add(service);
}
}