Several common hosts in WCF, detailed explanation of the methods for carrying the WCF Service

Source: Internet
Author: User

1: Preface

We all know that WCF must provide its own host to carry the service during running. Instead of hosting, WCF provides a servicehost class that allows youProgramHost WCF Service. Then, call the open method of servicehost. We know that WCF is a set of technologies for SOA. for SOA, we must ensure that the services can run normally and smoothly. Therefore, it is very important to host our services and what to host our services, therefore, it is necessary to select an appropriate host Method for our applications.

2: Common host types

You can use Visual Studio. NET to create the following types of managed applications:

Winforms Application

Console Application

Windows Services

Web applications hosted in Internet Information Service (IIS)

Below I will take a demo as an example to introduce these commonly used hosts.

3: Hoster features

When selecting an application type, you must consider certain requirements. servicehost must be instantiated to provide the bearer environment required to run the service. Console applications and winforms applications usually run on users' desktops. They are visible on the desktop and can carry your services with poor security. Therefore, they are not suitable for enterprise hosts. Because we want to enable our enterprise services to support more and more services, it is obviously not suitable when using console applications and winforms applications as the host, in this architecture ,. These are suitable for enterprise hosts that generally meet high availability requirements, for example. Therefore, we cannot use the console or winforms application as a host for the enterprise. The only feasible solution is to host the service on IIS or host it to the Windows service. In fact, we usually use Windows applicatin or console application as the host during project development. this is conducive to debugging and testing. During project deployment, we usually choose IIS or Windows service as the host. IIS has many advantages, and it provides many functions,

4: soultion Structure

This soultion mainly introduces three hosts, namely console applicatin, Windows service, and IIS, because Windows application and console application are not much different.

First, we will introduce host WCF Service in Windows Services. windows service is a process managed by the operating system. NET environment has provided such a template, with Windows Services as a host is a good choice:

Windows Services HOST: wcfwindowsserviceshost in application solution.

Services contract and services

[Servicecontract] public interface itest {[operationcontract] Double add (Double X, Double Y );}

Public class test: itest {

Public double add (Double X, Double Y) {return X + Y ;}

}

5: Windows Services host

First, create a Windows service project.

The Windows Services project contains two files: service1.cs and program. CS. The service1.cs file contains the service implementation, while the program. CS file is used for instantiation and essentially carries the Windows service. To host the WCF Service within the Windows service, you only need to execute the START () method and stop () method of the Windows service.

Servicehost host; Public windowservicetest () {initializecomponent ();}

Protected override void onstart (string [] ARGs) {Host = new servicehost (typeof (TEST); host. open ();
}

Protected override void onstop () {Host. Close ();} configuration file:

<System. servicemodel> <behaviors> <endpointbehaviors> <behavior name = "leleapplicationhost"> </behavior> </endpointbehaviors> </behaviors> <bindings> <attributes> <binding name = "basicbinding "> </binding> </basichttpbinding> </bindings> <services> <service name =" wcfhostservices. test "> <endpoint address =" "binding =" basichttpbinding "bindingconfiguration =" basicbinding "Contract =" wcfhostcontract. itest "behaviorconfiguration =" leleapplicationhost "> </Endpoint>

In fact, we can see that writing a host on Windows Services for the WCF Service is very simple. Windows Services has many advantages, such as starting with the operating system without manual enabling, this does not limit your binding technology. If we use IIS as the host, we have to use HTTP. so our binding will be subject to certain restrictions, saying that Windows Service has so many advantages, of course there are also some bad places, for example, to install Windows service.we have used the installutil.exe tool or the custom operation in the installation package to install the service. This tool will come with it after installing the SDK, and installutil is easy to use. The following uses the custom installation service as an example.

Open the designer view of the service class in the Windows service project. Right-click the window and click "add installer". A component named projectinstaller is added. Double-click this file in the project and there is a servicesinstall1 component, right-click to modify his name and Startup type,

 

To create an installer for installing Windows Services, we need to add the installer and deployment project (windowsservicesetup) to solution ). The following steps describe how to add setup and deployment projects to a solution:

Create an installation project:

1: Right-click the installation project, point to "add", select "project output", and select Windows Service appliation... select main output,

2: Right-click the installation project and choose View> custome action.

3: Right-click customer action and add the primary output file to custome action.

4: generate a project and install it. In this way, we can start our servcies. We can open the windows services management tool to view the installed servcie.

 

6: IIS HOST

To host WCF services in IIS, a file with the extension. SVC is required. In fact, such a template has been provided in our project template. This file associates the service with its implementation and is a means for IIS to automatically create servicehost. IIS will take over the interaction between the service and servicehost, and you do not have to instantiate and start servicehost by yourself .. The first line of the SVC file contains a command in the ASP. NET <% PAGE %> command to indicate the service to which the file points.

<% @ Servicehost Language = "C #" DEBUG = "true" service = "wcfiishost. Test" codebehind = "test. SVC. cs" %>

IIS config:

<System. servicemodel> <services> <service behaviorconfiguration = "wcfiishost. service1behavior "name =" wcfiishost. test "> <endpoint address =" "binding =" wshttpbinding "Contract =" wcfiishost. itest "> <identity> <DNS value =" localhost "/> </identity> </Endpoint> <endpoint address =" mex "binding =" mexhttpbinding "Contract =" imetadataexchange" /> </service> </services> <behaviors> <servicebehaviors> <behavior Nam E = "wcfiishost. service1behavior"> <! -- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <servicemetadata httpgetenabled = "true"/> <! -- To receive exception details in faults for debugging purposes, set the value below to true. set to false before deployment to avoid disclosing exception information --> <servicedebug includeexceptiondetailinfaults = "false"/> </behavior> </servicebehaviors> </behaviors> </system. servicemodel>

In IIS, the default behavior of host WCF services is that this IIS controls the instantiation of servicehost. On the client side, we directly reference the address. By default, the proxy file is generated on our client. We do not need to use the svcutil tool to generate the agent on the client side.

7: console application host

This method is simple, because most of the time we test the project, we use this host,

Config:

<System. servicemodel> <behaviors> <endpointbehaviors> <behavior name = "leleapplicationhost"> </behavior> </endpointbehaviors> </behaviors> <bindings> <attributes> <binding name = "basicbinding "> </binding> </basichttpbinding> </bindings> <services> <service name =" wcfhostservices. test "> <endpoint address =" "binding =" basichttpbinding "bindingconfiguration =" basicbinding "Contract =" wcfhostcontract. itest "behaviorconfiguration =" leleapplicationhost "> </Endpoint>

Start Host:

Using (servicehost host = new servicehost (typeof (TEST) {Host. open (); console. writeline ("services has begun listenting"); console. readkey ();}

Client:

Public class coclient: clientbase <wcfhostcontract. itest>, itest {public double add (Double X, Double Y) {return this. Channel. Add (x, y );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.