如何在1个Windows服务中托管2个WCF服务?
发布时间:2020-12-14 04:10:11 所属栏目:Windows 来源:网络整理
导读:我有一个WCF应用程序,它有两个服务,我试图使用net.tcp在一个 Windows服务中托管.我可以运行任何一种服务,但只要我尝试将它们都放在Windows服务中,只有第一个服务加载.我已经确定第二个服务ctor正在被调用,但OnStart永远不会被激活.这告诉我WCF发现加载第二个
我有一个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服务主机. REMEBER也可以调用stop方法…. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows – VPN无法正常连接
- Visual Studio Code 这个名字绝对是下下策
- windows-server-2003 – 当实际大小为2.7TB时,MediaShield
- HTTPS协议分析
- windows-7 – 允许非管理员在Windows 7上以管理员身份运行程
- Windows防火墙的设置允许Docker for Windows共享驱动器
- Windows7 64位机上Emgu CV2 4 2安装与配置
- .net – System.Windows.Media.DrawingVisual.RenderOpen()
- Windows – Samba无法在没有Internet连接的LAN上工作
- windows – 委派注销权限?
推荐文章
站长推荐
- 在windows上vs2017 编译webrtc
- windows-server-2003 – Windows文件共享上每个文
- win10 uwp 渲染原理 DirectComposition 渲染
- 在Windows上进行Linux的C开发
- windows-8 – Windows 8商店证书:由于认证(发布
- Windows身份验证一直要求输入用户名/密码
- windows 10环境下安装Tesseract-OCR与python集成
- Windows 10 安装 VMware workstation 12 版本出错
- windows-7 – 在Windows 7中建立VPN连接后,我可以
- windows-10 – 在Windows 10 Fall Creators Upda
热点阅读