My WCF journey (1): create a simple WCF Program

Source: Internet
Author: User

To give readers an intuitive image of the WCF-based programming model, I will lead them to create a complete WCF application step by step. Although this function is simple, it covers the basic structure of a complete WCF application. For those who are not familiar with WCF, this example will take you into the world of WCF.

In this example, we will implement a simple computing service (CalculatorService) that provides basic addition, subtraction, multiplication, and Division operations. Like the traditional distributed communication framework, WCF essentially provides a cross-process, cross-machine, and cross-network service call. In this example, the client and service run different process simulation on the same machine,Figure 1Reflects the relationship between client and server processes.

Figure 1 computing service application running environment

The WCF Service cannot exist in isolation and must be hosted in a running process. We call the process that carries the WCF Service as the host, the process of specifying a host for a Service is called Service Hosting ). In our computing service applications, two service hosting methods are adopted: accept ). The customer uses another console application simulation (the process is client.exe ). Next, we will build such a WCF application step by step.

Step 1: Build the entire solution

Create a blank solution through VS 2008 and add the following four projects. The types, bearer functions, and reference relationships of projects are as follows. The structure of the entire project in VS is as follows:Figure 2.

  • Contracts:A class library project that defines Service Contract and references the System. ServiceMode Assembly (the majority of implementation of the WCF framework and API definition are in this Assembly );
  • Services:A class library project that provides the implementation of the WCF Service. Define all the WCF Services in this project to implement the corresponding service contract defined in Contracts, so Services have references to the Contracts project;
  • Hosting:A Console application is used to host Services defined in the Services project. The project must reference both the Contracts and Services projects and the System. ServiceMode assembly;
  • Client:A console application simulation service client that references the System. ServiceMode assembly.

Figure 2 Structure of computing service in

Step 2: create a service contract

WCF uses contract-based interaction to implement service autonomy and loose coupling between the client and the server. WCF includes four types of contracts: service contract, data contract, message contract, and error contract. Here we focus on service contract. In terms of functions, a service contract abstracts all operations provided by a service. From the perspective of message exchange, a service contract defines a message exchange process based on service calls, structure of the request message and reply message, and the message exchange mode. Chapter 2 provides a detailed description of the service contract.

Generally, we define a service contract through an interface. The following code defines an interface ICalculator as a service contract. WCF uses a wide range of Declarative Programming modes based on Custom attributes (Custom Attribtue). We apply aspx "target = _ blank> System on the interface. serviceModel. serviceContractAttribute defines an interface as a service contract. When applying the ServiceContractAttribute feature, you can also specify the name and namespace of the service contract. As for the meaning and role of the contract name and namespace, I wrote chapter 4th of "WCF technical analysis (Volume 1, here we set the contract name and namespace to CalculatorService and http://www.artech.com /).

After an interface is defined as a service contract by applying the ServiceContractAttribute feature, the method members of the interface cannot automatically become service operations. In this regard, WCF adopts an Explicit selection (Explicit it Opt-in) policy: we need to explicitly apply the OperationContractAttribute feature on the corresponding operation methods.

1: using System. ServiceModel;
2: namespace Artech. WcfServices. Contracts
3 :{
4: [ServiceContract (Name = "CalculatorService", Namespace = "http://www.artech.com/")]
5: public interface ICalculator
6 :{
7: [OperationContract]
8: double Add (double x, double y );
9:
10: [OperationContract]
11: double Subtract (double x, double y );
12:
13: [OperationContract]
14: double Multiply (double x, double y );
15:
16: [OperationContract]
17: double Divide (double x, double y );
18 :}
19 :}

Step 3: create a service

When a service contract is successfully created, you must implement the service contract to create a specific WCF Service. The WCF Service CalculatorService is defined in the Services project. It implements the service contract interface ICalculator and implements all service operations. CalculatorService is defined as follows:

   1: using Artech.WcfServices.Contracts;
   2: namespace Artech.WcfServices.Services
   3:

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.