What should you do in the Windows Service class?

You are developing a Windows Service. The Windows Service will host a Windows Communication Foundation (WCF) service.
The Windows Service class will inherit from ServiceBase.
You need to ensure that the WCF service starts when the Windows Service is restarted. What should you do in the Windows Service class?

You are developing a Windows Service. The Windows Service will host a Windows Communication Foundation (WCF) service.
The Windows Service class will inherit from ServiceBase.
You need to ensure that the WCF service starts when the Windows Service is restarted. What should you do in the Windows Service class?

A.
– Create a public method named Main.
– Create a new ServiceHost in the Main method.
– Override the OnShutdown method and close the ServiceHost.

B.
– Override the OnStart method and create and open a new ServiceHost.
– Override the OnStop method and close the ServiceHost.

C.
– Override the OnPowerEvent method and open a new ServiceHost.
– Override the OnShutdown method and close the ServiceHost.

D.
– Override the OnContinue method and open a new ServiceHost.
– Override the OnStop method and close the ServiceHost.

Explanation:
Windows services can be managed with the Microsoft.ManagementConsole.SnapIn in Microsoft Management Console (MMC)
and can be configured to start up automatically when the system boots up.
This hosting option consists of registering the application domain (AppDomain) that hosts a WCF service as a managed
Windows service so that the process lifetime of the service is controlled by the Service Control Manager (SCM) for Windows services.

The service code includes a service implementation of the service contract, a Windows Service class, and an installer class.
The service implementation class, CalculatorService, is a WCF service. The CalculatorWindowsService is a Windows service.
To qualify as a Windows service, the class inherits from ServiceBase and implements the OnStart and OnStop methods.

In OnStart, a ServiceHost is created for the CalculatorService type and opened. In OnStop, the service is stopped and disposed.
The host is also responsible for providing a base address to the service host, which has been configured in application settings.
The installer class, which inherits from Installer, allows the program to be installed as a Windows service by the Installutil.exe tool.



Leave a Reply 1

Your email address will not be published. Required fields are marked *