【发布时间】:2013-09-19 08:33:45
【问题描述】:
我创建了一个 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;
}
app.config:
<?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);
}
}
【问题讨论】:
-
这取决于您在调用 host.Open() 时所做的事情。此外,我相信该服务不会记录任何内容,除非您明确声明必须这样做。
-
哪个用户帐户用于运行 NT Serice?作为控制台运行时,您在启动控制台的凭据下运行(通常是开发人员以高安全权限登录)。当作为 NTService 运行时,它是一个分配的 windows 帐户。这可能与帐户拥有的权限有关吗?我也似乎在配置部分缺少“Service1”。您使用的是哪个版本的 .Net 框架? (NTServices 配置加载已更改)
-
.net 4,不知道是用哪个账号来运行服务的,现在试了一个简单的代码,同样的情况,只是简单的文字写不行。跨度>
标签: c# wcf windows-services