1개의 Windows 서비스에서 2개의 WCF 서비스를 호스팅하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/54419

  •  09-06-2019
  •  | 
  •  

문제

net.tcp를 사용하여 단일 Windows 서비스에서 호스팅하려는 두 개의 서비스가 있는 WCF 응용 프로그램이 있습니다.두 서비스 중 하나를 제대로 실행할 수 있지만 두 서비스를 모두 Windows 서비스에 넣으려고 하면 첫 번째 서비스만 로드됩니다.두 번째 서비스 섹터가 호출되고 있지만 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 기사 두 개의 서비스 호스트를 만듭니다.그러나 실제로 각 서비스 호스트를 직접 호출하는 대신 실행하려는 각 서비스를 정의하는 원하는 만큼 많은 클래스로 나눌 수 있습니다.

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();


    }

그런 다음 하나의 Windows 서비스 호스트에 원하는 만큼 WCF 서비스를 추가할 수 있습니다.

stop 메소드도 호출하는 것을 기억하세요....

다른 팁

            Type serviceAServiceType = typeof(AfwConfigure);
            Type serviceAContractType = typeof(IAfwConfigure);

            Type serviceBServiceType = typeof(ConfigurationConsole);
            Type serviceBContractType = typeof(IConfigurationConsole);

            Type serviceCServiceType = typeof(ConfigurationAgent);
            Type serviceCContractType = typeof(IConfigurationAgent);

            ServiceHost serviceAHost = new ServiceHost(serviceAServiceType);
            ServiceHost serviceBHost = new ServiceHost(serviceBServiceType);
            ServiceHost serviceCHost = new ServiceHost(serviceCServiceType);
            Debug.WriteLine("Enter1");
            serviceAHost.Open();
            Debug.WriteLine("Enter2");
            serviceBHost.Open();
            Debug.WriteLine("Enter3");
            serviceCHost.Open();
            Debug.WriteLine("Opened!!!!!!!!!");

하나의 Windows 서비스에서 두 개의 WCF 서비스를 시작하려면 두 개의 ServiceHost 인스턴스가 있는 하나의 ServiceInstaller가 필요하며 둘 다 (단일) OnStart 메서드에서 시작됩니다.

Visual Studio에서 새 Windows 서비스를 만들도록 선택할 때 템플릿 코드에 있는 ServiceInstaller의 패턴을 따를 수 있습니다. 일반적으로 여기에서 시작하는 것이 좋습니다.

아마도 2명의 서비스 호스트만 필요할 것입니다.

_host1 및 _host2.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top