如何在 1 个 Windows 服务中托管 2 个 WCF 服务?
-
09-06-2019 - |
题
我有一个 WCF 应用程序,它有两个服务,我尝试使用 net.tcp 将它们托管在单个 Windows 服务中。我可以很好地运行其中一个服务,但是当我尝试将它们都放入 Windows 服务时,只有第一个服务会加载。我已确定正在调用第二个服务 ctor,但 OnStart 从未触发。这告诉我 WCF 在加载第二个服务时发现了一些问题。
使用net.tcp我知道我需要打开端口共享并在服务器上启动端口共享服务。这一切似乎都运行正常。我尝试过将服务放在不同的 TCP 端口上,但仍然没有成功。
我的服务安装程序类如下所示:
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller _process;
private ServiceInstaller _serviceAdmin;
private ServiceInstaller _servicePrint;
public ProjectInstaller()
{
_process = new ServiceProcessInstaller();
_process.Account = ServiceAccount.LocalSystem;
_servicePrint = new ServiceInstaller();
_servicePrint.ServiceName = "PrintingService";
_servicePrint.StartType = ServiceStartMode.Automatic;
_serviceAdmin = new ServiceInstaller();
_serviceAdmin.ServiceName = "PrintingAdminService";
_serviceAdmin.StartType = ServiceStartMode.Automatic;
Installers.AddRange(new Installer[] { _process, _servicePrint, _serviceAdmin });
}
}
这两项服务看起来非常相似
class PrintService : ServiceBase
{
public ServiceHost _host = null;
public PrintService()
{
ServiceName = "PCTSPrintingService";
CanStop = true;
AutoLog = true;
}
protected override void OnStart(string[] args)
{
if (_host != null) _host.Close();
_host = new ServiceHost(typeof(Printing.ServiceImplementation.PrintingService));
_host.Faulted += host_Faulted;
_host.Open();
}
}
解决方案
您的服务以此为基础 MSDN 文章 并创建两个服务主机。但是,您不必直接直接调用每个服务主机,而是可以将其分解为任意多个类,这些类定义了您要运行的每个服务:
internal class MyWCFService1
{
internal static System.ServiceModel.ServiceHost serviceHost = null;
internal static void StartService()
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Instantiate new ServiceHost.
serviceHost = new System.ServiceModel.ServiceHost(typeof(MyService1));
// Open myServiceHost.
serviceHost.Open();
}
internal static void StopService()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
};
在Windows服务主机的主体中,调用不同的类:
// Start the Windows service.
protected override void OnStart( string[] args )
{
// Call all the set up WCF services...
MyWCFService1.StartService();
//MyWCFService2.StartService();
//MyWCFService3.StartService();
}
然后,您可以向一台 Windows 服务主机添加任意数量的 WCF 服务。
记住也要调用停止方法......
其他提示
Type serviceAServiceType = typeof(AfwConfigure);
Type serviceAContractType = typeof(IAfwConfigure);
Type serviceBServiceType = typeof(ConfigurationConsole);
Type serviceBContractType = typeof(IConfigurationConsole);
Type serviceCServiceType = typeof(ConfigurationAgent);
Type serviceCContractType = typeof(IConfigurationAgent);
ServiceHost serviceAHost = new ServiceHost(serviceAServiceType);
ServiceHost serviceBHost = new ServiceHost(serviceBServiceType);
ServiceHost serviceCHost = new ServiceHost(serviceCServiceType);
Debug.WriteLine("Enter1");
serviceAHost.Open();
Debug.WriteLine("Enter2");
serviceBHost.Open();
Debug.WriteLine("Enter3");
serviceCHost.Open();
Debug.WriteLine("Opened!!!!!!!!!");
如果您希望一项 Windows 服务启动两项 WCF 服务,则需要一个具有两个 ServiceHost 实例的 ServiceInstaller,这两个实例均在(单个)OnStart 方法中启动。
当您选择在 Visual Studio 中创建新的 Windows 服务时,您可能希望遵循模板代码中的 ServiceInstaller 模式 - 一般来说,这是一个很好的起点。
您可能只需要 2 个服务主机。
_主机1和_主机2。
不隶属于 StackOverflow