In one of my previous blogs, I described how to publish a WCF service and host it on IIS, and today I'll show you how it's good to host a WCF service in a Windows service. Of course, you can save a series of problems such as deploying IIS, can make deployment easier, of course, the WCF boarding method is generally divided into the following four ways, for each of the ways I would like to briefly introduce the following:
Please refer to the msdn:https://msdn.microsoft.com/zh-cn/library/ms733109 (v=vs.100). aspx for more information about the specific boarding options.
One, the WCF service boarding way:
1): Hosted on IIS: similar to the classic WebService hosting, the service as a Web project, the need to provide the Svc file, the disadvantage is that only use the HTTP protocol, that is, only use the monotonous service, there is no session state. IIS is also limited by the port (all services must use the same port), and the main advantage is that the client's first request is to start the host process automatically.
2): Hosted on was (full name Windows Activation Service): Was is a new process activation service, which is a generalization of Internet information Services (IIS) features that use non-HTTP transport protocols. WCF uses the listener adapter interface to pass activation requests that are received through non-HTTP contracts that are supported by WCF (for example, TCP, named pipes, and Message Queuing). A managed web site that can host services that can be installed and configured separately, without relying on IIS, using any protocol. You need to provide the SVC file or provide equivalent information within the configuration file.
3): Self-hosted: A way for developers to provide and manage the life cycle of the host process. You can use a console program, a WinForm window program, and a WPF program to provide hosting services. Any protocol can be used. Must be started before the client. You can implement WCF advanced Features: Service bus, service discovery, Singleton services.
4): Hosted on Windows services: This scenario can be enabled through the hosted Windows service hosting option, which is a long-running WCF service that is hosted outside of Internet Information Services (IIS) in a secure environment without message activation. The lifetime of the service is changed by the operating system control. This host option is available in all versions of Windows. You can use Microsoft.ManagementConsole.SnapIn in the Microsoft Management Console (MMC) to manage Windows services, and you can configure them to start automatically when the system starts. This hosting option includes registering the application domain hosting the WCF service as the managed Windows service, so the process lifetime of the service is controlled by the Service Control Manager (SCM) of the Windows service.
This article is intended to cover the fourth type: WCF programs are hosted in a managed Windows service.
1 Create a new WCF service and follow the relevant rules to build a complete WCF program.
A: Define the service interface
Note: Using the rename command on the Refactor menu, you can change the interface name "IService1" in code and configuration files at the same time. [ServiceContract] public interface Ibasicservice { [OperationContract] string Login (string Username, string password, string version); [OperationContract] Users GetUserInfo (string userName); [OperationContract] BOOL SaveOption (String option_name, String option_value); [OperationContract] String Getoptionvalue (string option_name); [OperationContract] BOOL Saveoptionbyuser (String option_name, string option_value, int userid); [OperationContract] String Getoptionvaluebyuser (string option_name, int userid); [OperationContract] String Testsqlconnection (); }
Host WCF in a managed Windows service