Cra*_*ler 8 wcf windows-services
我有一个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 });
}
}
Run Code Online (Sandbox Code Playgroud)
这两项服务看起来非常相似
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Wir*_*ren 11
将您的服务基于此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;
}
}
};
Run Code Online (Sandbox Code Playgroud)
在Windows服务主机的主体中,调用不同的类:
// Start the Windows service.
protected override void OnStart( string[] args )
{
// Call all the set up WCF services...
MyWCFService1.StartService();
//MyWCFService2.StartService();
//MyWCFService3.StartService();
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将一个WCF服务添加到一个Windows服务主机.
REMEBER也可以调用stop方法....
| 归档时间: |
|
| 查看次数: |
8535 次 |
| 最近记录: |