我正在尝试在通过控制台应用程序启动的 Windows 服务中托管 WCF 服务。每个服务都是自己的项目,控制台应用程序也是如此。我已将 WCF 服务库中的 app.config 复制到控制台应用程序的 app.config 中,但我不断收到“服务有零个应用程序端点......”。我在几个地方读到该错误意味着我的类型引用不完全合格,但我已经双重(三重,四重......)检查了这一点。而且我很确定我有一个 app.config。我的调试目录中有 3 个 exe:Console App、Console App vshost、Win Service。Win 服务没有 app.config,所以我尝试复制它的 app.config 以防它正在寻找它,但没有运气。我还检查以确保配置正确命名(<program>.exe.config)。
这是我正在使用的。我的控制台应用程序创建一个实例JobSchdeuler
并调用JobSchedulerConsoleStart
.
主机代码:
public partial class JobScheduler : ServiceBase
{
ServiceHost jobServiceHost = null;
public JobScheduler()
{
ServiceName = "JobSchedulerService";
InitializeComponent();
}
#region Service Init/Uninit
/// <summary>
/// OnStart
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
if (jobServiceHost != null)
{
jobServiceHost.Close();
}
jobServiceHost = new ServiceHost(typeof(JobSchedulerWCF.JobService));
jobServiceHost.Open();
}
/// <summary>
/// OnStop
/// </summary>
protected override void OnStop()
{
if (jobServiceHost != null)
{
jobServiceHost.Close();
jobServiceHost = null;
}
}
#endregion
#region Debugging
public void JobSchedulerConsoleStart()
{
this.OnStart(null);
Console.WriteLine("Service Started.");
ProcessInput();
Console.WriteLine("Service Stopped.");
this.OnStop();
}
private void ProcessInput()
{
Console.WriteLine("Press any key to quit...");
Console.ReadKey();
}
#endregion
}
应用程序配置
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="JobSchedulerWCF.Service1Behavior" name="JobSchedulerWCF.JobService, JobSchedulerWCF">
<endpoint address="" binding="wsHttpBinding" contract="JobSchedulerWCF.IJobServiceController, JobSchedulerWCF">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:12345/jobService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JobSchedulerWCF.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>