about uscontact

Hosting multiple WCF services under a single windows service

Category:
Author: zemna
Date: April 17, 2021
Share this:

하나의 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 serviceHosts = new List();

public void OpenAll() { OpenHost(); OpenHost(); OpenHost(); }

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

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

Share this:

Leave a Reply

Your email address will not be published.

ZEMNA.NET

Professional software development agency estalished in Indonesia. We offers website setup, web application development, fulfillment service, and etc.
Follow us
Follow us to get latest news and tutorials.
2022 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️
magnifiercrossmenu