The host of the WCF, the host of the WCFWCF

Source: Internet
Author: User

The host of the WCF, the host of the WCFWCF

I. WCF Service applications and WCF Service Libraries

  In our usual development process, the commonly used project types include "WCF Service Application" and "WCF Service library ".

A WCF Service application is an executable program. It has an independent process and is defined by the WCF Service Contract. The running effect can be seen directly. This project template is based on IIS-hosted programs, as shown in section 1 of this series. This type is common when developing IIS-based WCF Service programs. It is easy to understand.

The WCF Service library can be considered as a class library containing the WCF Service and contract definitions. It cannot be run directly. You can reference it in other projects and enable hosting this library in the host, which is a bit similar to the class library we applied in the Web project. When designing a WCF Service, the service class is defined as a separate library and can be used by other projects. Improves code reusability.

Of course, you can also modify the code, such as moving the classes in the WCF Service program to a separate class library, or moving the classes in the class library to the WCF Service Program.

Ii. Overview

Through the previous introduction, we know that WCF must be hosted on a "Host Program" during runtime, And that WCF itself cannot run independently (each WCF Service must be hosted in a Windows Process ).. . Net provides a variety of hosts for WCF to run.

// Add reference using System. serviceModel; namespace WCFLibrary {[ServiceContract] interface IUser {[OperationContract] string ShowName (string name) ;}} namespace WCFLibrary {class User: IUser {public string ShowName (string name) {string wcfName = string. format ("WCF Service, display name: {0}", name); return wcfName ;}}}

Because the original contract is IService and the current one is IUser, You need to modify the configuration file:

<Service name = "WCFLibrary. User">

<Endpoint address = "" binding = "wsHttpBinding" contract = "WCFLibrary. IUser">

Click "F5", as shown in the following figure. The operation is successful:

2. IIS HOST

In section 1, we host WCF on IIS. The main advantage of hosting a service in IIS is that the host process is automatically started when a client request occurs, in addition, you can use IIS to manage the lifecycle of the host process. The process of development and use is very similar to that of Web Service.

3.Console Application host

Create a host

(1) create a console output project WCFHost_Console under the solution.

(2) Add a reference to System. ServiceModel. dll.

(3) Add a project reference for the WCF Service Library (WCFLibrary.

(4) create a Host Program. The Code is as follows:

Using System; using WCFLibrary; using System. serviceModel; using System. serviceModel. description; namespace WCFHost_Console {class Program {static void Main (string [] args) {// create the base address Uri of the host baseAddress = new Uri ("http: // localhost: 8080/User "); // create the host using (ServiceHost host = new ServiceHost (typeof (User), baseAddress) {// Add the endpoint host to the host. addServiceEndpoint (typeof (IUser), new WSHttpBinding (), ""); // set the HttpGetEnabled attribute to true ServiceMetadataBehavior smb = new ServiceMetadataBehavior (); smb. httpGetEnabled = true; // Add behavior to Behaviors host. description. behaviors. add (smb); // open the host. open (); Console. writeLine ("the HTTP listener in WCF has been started .... "); Console. readLine (); host. close ();}}}}

(5) run the Host Program [run the Host Program before calling the client], as shown in:

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. serviceModel> <services> <service name = "WCFLibrary. User">

Shows the running program:

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. serviceModel> <bindings> <netTcpBinding> <binding name = "netTcpBindConfig"> <security mode = "None"> <transport clientCredentialType = "Windows" protectionLevel = "EncryptAndSign"/> <message clientCredentialType = "Windows"/> </security> </binding> </netTcpBinding> </bindings> <services> <service behaviorConfiguration = "MyBehavior" name = "WCFHost_WAS.User"> <endpoint address = "" bindi Ng = "netTcpBinding" contract = "WCFHost_WAS.IUser" bindingConfiguration = "netTcpBindConfig"> </endpoint> <! -- End of metadata exchange --> <endpoint address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name = "MyBehavior"> <serviceMetadata/> <serviceDebug detail = "true"/> <dataContractSerializer maxItemsInObjectGraph = "6553600"/> </behavior> </ serviceBehaviors> </behaviors> </system. serviceModel> </configuration>

(5) Deployment Service

Just like other Web applications, you can drop related files to a specified directory on the server.

Right-click User. svc, as shown below:

<Client> <endpoint address = "http: // localhost: 8080/User" binding = "wsHttpBinding" contract = "wcfhost_lele.iuser"/> <endpoint address = "http: // localhost: 8081/User "binding =" wsHttpBinding "bindingConfiguration =" WSHttpBinding_IUser "contract =" WCFHost_Form.IUser "name =" regular "> <identity> <userPrincipalName value =" WIN-EOUTAA4CP4O \ Administrator "/> </identity> </endpoint> <endpoint address = "net. tcp: // win-eoutaa4cp4o/User. svc "binding =" netTcpBinding "bindingConfiguration =" NetTcpBinding_IUser "contract =" WCFHost_WAS.IUser "name =" NetTcpBinding_IUser "/> </client>

We call services of three different hosts in a Web application.

VII. Summary

Through the above examples, we have implemented the console host, Form host, and WAS host (based on the TCP protocol. In the actual development process, we sometimes use Windows service-based hosts, but most of them use IIS as the host, which is convenient and fast.

 

Link: http://www.cnblogs.com/iamlilinfeng/archive/2012/10/01/2706353.html

Related Article

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.