Hosting multiple WCF services under a single windows service

하나의 Windows Service 응용프로그램에서 다수의 WCF 서비스를 운영할 때, 손쉽게 각 ServiceHost를 생성할 수 있도록 ServiceManager 클래스를 만들어서 사용하는 방법을 설명한 소스코드이다.

ServiceManager 클래스를 사용하게 되면 서비스 추가시 ServiceManager 클래스의 OpenAll() 함수에서 추가할 서비스 클래스만 명시해 주면 된다. ^^

[code lang="csharp"] using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceProcess;

namespace Server
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

private readonly ServiceManager serviceManager = new ServiceManager();

protected override void OnStart(string[] args)
{
base.OnStart(args);
serviceManager.OpenAll();
}

protected override void OnStop()
{
serviceManager.CloseAll();
base.OnStop();
}
}

public class ServiceManager
{
readonly List<ServiceHost> serviceHosts = new List<ServiceHost>();

public void OpenAll()
{
OpenHost<Service1>();
OpenHost<Service2>();
OpenHost<Service3>();
}

public void CloseAll()
{
foreach (ServiceHost serviceHost in serviceHosts)
{
serviceHost.Close();
}
}

private void OpenHost<T>()
{
Type type = typeof(T);
ServiceHost serviceHost = new ServiceHost(type);
serviceHost.Open();
serviceHosts.Add(serviceHost);
}
}
}
[/code]

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments