我有一个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 article并创建两个服务主机.
但是,不是直接实际调用每个服务主机,而是可以根据需要将其分解为多个类,以定义要运行的每个服务:
但是,不是直接实际调用每个服务主机,而是可以根据需要将其分解为多个类,以定义要运行的每个服务:
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(); }
然后,您可以将一个WCF服务添加到一个Windows服务主机.