Create a WCF Service host to IIS and a wcf host to iis

Source: Internet
Author: User

Create a WCF Service host to IIS and a wcf host to iis
I. Introduction to WCF:

Windows Communication Foundation (WCF) is a series of application frameworks developed by Microsoft to support data Communication. It can be translated as a Windows Communication development platform.

Integrates the existing. net Remoting, WebService, and Socket mechanisms for windows communications, and integrates HTTP and FTP-related technologies.

Introduced by. NET Framework 3.0.

The final goal of WCF is to send and receive messages between customers and services through processes or different systems, through a local network, or through the Internet.
WCF combines the functions of Web Services,. net Remoting, message queue, and Enterprise Services and integrates them in Visual Studio.
WCF is specially used for service-oriented development.

2. Create a simple WCF Service Application 1. Create a blank solution and create a project in the solution. The project type is: WCF Service Application. As shown in figure:

The generated solution project contains three files: Service1.Svc, IService1.cs, and Web. config. View the Code:
// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "IService1" in the Code and configuration file at the same time ". [ServiceContract] public interface IService1 {[OperationContract] string GetData (int value); [OperationContract] CompositeType GetDataUsingDataContract (CompositeType composite); // TODO: add your service operation here} // use the data conventions described in the following example to add a composite type to the service operation. [DataContract] public class CompositeType {bool boolValue = true; string stringValue = "Hello"; [DataMember] public bool BoolValue {get {return boolValue;} set {boolValue = value ;}} [DataMember] public string StringValue {get {return stringValue;} set {stringValue = value ;}}}
public class Service1 : IService1    {        public string GetData(int value)        {            return string.Format("You entered: {0}", value);        }        public CompositeType GetDataUsingDataContract(CompositeType composite)        {            if (composite == null)            {                throw new ArgumentNullException("composite");            }            if (composite.BoolValue)            {                composite.StringValue += "Suffix";            }            return composite;        }    }
[ServiceContract] indicates that the interface is a WCF interface. If it is not added, it cannot be called externally.
[OperationContract] to indicate that this method is a method of the WCF interface. If it is not added, it is the same as above.
[DataContract] Once a data type is declared as DataContract, this type can be serialized and transmitted between the server and client.
[DataMember] (data member) can be transferred only when it is marked as DataMember. Set Service1.svc of this project as the starting page, and F5 runs debugging.
Double-click the GetData () method and enter value = 1, invoke, You entered: 1, indicating that the service test is successful. 3. Publish the WCF Service to the IIS right-click Project and publish it as a file system.

Are you sure you have enabled or disabled the Windows feature to install the wcf Service and IIS
Open IIS, add a site, and start. Set the identity to Everyone for access. Open the browser and enter 10.0.0.217: 20000 to enter the directory:
You can find that there are User. svc and Sercice1.svc WCF Service interfaces. Click to enter>
It indicates that our WCF Service is successfully hosted on IIS. 4. The client calls the WCF Service. In the solution, create a console application, right-click to reference and add a service reference, http: // 10.0.0.217: 20000/Service1.svc? Wsdl
The namespace can be modified. After the namespace is successfully referenced, it is found that there are two more namespace named service files: ServiceReference1 and ServiceReference2. Here are service references of Service1.svc and User. svc respectively.
Class Program {static void Main (string [] args) {WcfTest. demo1.ServiceReference1. service1Client c = new ServiceReference1.Service1Client (); Console. writeLine (c. getData (1); WcfTest. demo1.ServiceReference2. userClient u = new ServiceReference2.UserClient (); Console. writeLine (u. insertUser ("James "));}}



In this way, a simple WCF Service Application is created, which is hosted on the IIS process and called through service reference.

Use:

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.