Chapter 2 Introduction to WCF)

Source: Internet
Author: User
Tags webhost
Chapter 1 Introduction to wcf soa is a topic that has received much attention in the industry in recent years. It represents a direction of software architecture. In line with the development trend of SOA, Microsoft launched a new distributed communication framework windows Communication Foundation (WCF) at the end of 2006. WCF is released as a component of. Net framework3.0. 1.1 is SOA a web service architecture? Service-oriented (SO) represents a design concept, which is the same as object orientation (OO) and component-oriented (CO, the idea is to break down the focus. service orientation is not related to technology. A service is an autonomous service. The principle of autonomy requires a single service to be as independent and self-contained as possible in terms of the underlying logic control. The service is as independent as possible from accessing its clients and other services. Services can be deployed independently and version policies and security policies can be implemented. One goal of SOA dependent on the standard SOA of development is to enable service interoperability developed by different vendors. SOA adopts message-based communication. SOA uses XML, XSD, and WSDL as the "language" of service description ". SOA supports cross-platform SOA to encourage the creation of composite services SOA to encourage the reuse of services SOA emphasizes loose coupling 1.2 WCF is an integration of existing distributed communication technologies Fig 1-1 WCF is a technology for existing distributed Communication integration
1.3 construct a simple WCF Application diagram 1-2 call relationship between client and Service Process Diagram 1-3 class diagram of the WCF contract 1.3.1 create a new WCF Service (1) create a solution, consolehost, and a new console application lelehost.
(2) Add a class library iservice to create an interface icalculator. Pay attention to add service reference, system. servicemodel. The Code is as follows:
using System.ServiceModel;namespace IService{    [ServiceContract(Name="CalculatorService", Namespace="http://www.artech.com")]    public interface ICalculator    {        [OperationContract]        double Add(double x, double y);    }}

(3) Add a class library service and create a class calculatorservice, which is the implementation interface icalculator. The Code is as follows:
using IService;namespace Service{    public class CalculatorService:ICalculator    {        public double Add(double x, double y)        {            return x + y;        }    }}

(4) Complete the consolehost code to add references to the service class library. The Code is as follows:
Using system; using system. servicemodel; using service; namespace consolehost {class program {static void main (string [] ARGs) {using (servicehost host = new servicehost (typeof (calculatorservice) {Host. opened + = delegate {console. writeline ("calculatorservice has been started. Press any key to terminate the service! ") ;}; Host. open (); console. Read ();}}}}
Add the configuration file app. config as follows:
<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior name="metadataBehavior">          <serviceMetadata httpGetEnabled="false"/>        </behavior>      </serviceBehaviors>    </behaviors>    <services>      <service name="Service.CalculatorService" behaviorConfiguration="metadataBehavior">        <endpoint address="http://127.0.0.1:3721/calculatorservice"                   binding="wsHttpBinding"                   contract="IService.ICalculator"/>      </service>    </services>  </system.serviceModel></configuration>

(5) Add a new project to the web host, select the WCF Service Application, add a reference to the service class library to the project name webhost, and create a calculatorservice. SVC file. The Code is as follows:
<%@ ServiceHost Language="C#" Debug="true" Service="Service.CalculatorService"%>

Note that the key part of modifying the code in Web. config is as follows:

<System. servicemodel> <services> <service name = "service. calculatorservice" behaviorconfiguration = "webhost. service1behavior"> <! -- Service endpoints --> <endpoint address = "" binding = "wshttpbinding" Contract = "iservice. icalculator"> <! -- During deployment, the following identification elements should be deleted or replaced to reflect the identity of the deployed service under it. After deletion, WCF automatically derives the corresponding identifier. --> <Identity> <DNS value = "localhost"/> </identity> </Endpoint> <endpoint address = "mex" binding = "Mexico httpbinding" Contract = "imetadataexchange "/> </service> </services> <behaviors> <servicebehaviors> <behavior name = "webhost. service1behavior "> <! -- To avoid metadata leakage, set the following value to false before deployment and delete the above metadata endpoint --> <servicemetadata httpgetenabled = "true"/> <! -- To receive fault exception details for debugging, set the following value to true. Set false before deployment to avoid leakage of exception information --> <servicedebug includeexceptiondetailinfaults = "false"/> </behavior> </servicebehaviors> </behaviors> </system. servicemodel>
1.3.2 run the WCF Service (1). Run the console host service console to run the WCF Service. Run the consolehost project to start the process. The operation is successful, for example:
(2) Deploy the WCF Service on IIS. Step 1: Add a website

Step 2: Modify the version

      

Step 3: Start IIS successfully

    

1.3.3 the client calls the WCF Service to create a solution wcfclient and add a console project wcfclient. Create a folder DLL under the solution, copy the iservice DLL to the DLL folder, add a reference to iservice, and add a reference to system. servicemodel. The Code is as follows:

 

using System;using System.ServiceModel;using IService;namespace WCFClient{    class Program    {        static void Main(string[] args)        {            using(ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))            {                ICalculator proxy = channelFactory.CreateChannel();                Console.WriteLine("3 + 1 = {0}", proxy.Add(3, 1));            }            Console.Read();        }    }}

The code for the app. config file is as follows:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <client>      <endpoint name="calculatorservice"                address="http://127.0.0.1:3721/calculatorservice"                binding="wsHttpBinding"                contract="IService.ICalculator"                />    </client>  </system.serviceModel></configuration>

The configuration file is used to call the console host's WCF Service. If you want to call IIS's WCF, change the yellow part of the code to http: // 127.0.0.1: 8024/calculatorservice. SVC. The Console port number is the app under the consolehost project. config, And the IIS port number is configured when the web is set up.

The running result is as follows:

  

Comprehensive Analysis of WCF
Amy
September 16, 2014

Chapter 2 Introduction to WCF)

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.