Introduction to the basic development of WCF

Source: Internet
Author: User
Tags msmq

Http://www.cnblogs.com/myg2006/archive/2008/03/09/1097189.html

WCF --- Windows Communiction Foundation is a technology that Microsoft has been introducing for a long time. For a long time, many people have written many articles about WCF, such as the WCF series. They are all good articles. I just want to talk about some basic things, and then write an example to give people who have just started to learn about WCF a perceptual knowledge, hoping to bring some practical use to their learning, at the same time, it is a process of deep understanding of WCF.

WCF is a unified, secure, and reliable service-oriented application development platform. WCF is a unified framework for building secure and reliable transactional services. It is a very rich technical basis for building a distributed service-oriented system. It unifies the message style and RPC [Remote Procedure Call] style, in addition, the platform is optimized through binary and open-standard-based communication.

It integrates. all technologies related to distributed systems, such as ASP. NET Web Service (ASMX), WSE ),. net Remoting, Enterprise Service, and Microsoft Message Queue (MSMQ ).

To understand WCF, you must first understand the basic components of WCF:

  • Message --- Soap Message, WCF not only supports XML format, but also supports more efficient Binary format
    • Header --- the Header of the Message, which is usually the affiliated information. It can contain zero or multiple
    • Body --- Body of the Message, usually subject information, which can be zero or multiple
  • Channel-generally, multiple channels can be created for Message transmission, which usually includes the following four parts of information, but we do not need to specify them, but specify them in Service/Endpoint configuration.
    • Security --- Transmission Security
      • Message Securty --- Message security. Generally, the Authentication methods include Windows Authentication, X.509, or Custom.
        • Authentication --- Verification
        • Integrity --- message Integrity
        • Confidentiality --- message Confidentiality, encryption and decryption
        • Auditing --- Review
      • Transport Security --- Security of Transport
        • Https-Secure Transmission over Http
        • Other
    • Interoperability --- interaction. In my understanding, it is a service type that can be replaced.
      • WebService
      • WSE
      • . Net Remoting
      • Enterprise Service
      • MSMQ
      • Other
    • Message pattern --- Message transmission method
      • Simplex --- unidirectional transmission, such as A --> B
      • Duplex --- bidirectional transmission, for example, A <----> B, A first sends information to B, B returns A status, then A sends A Message, then B Response
      • Request-Respose --- Request/reply, A --> B, B -->
    • Transport --- transmission type, in which form Message is transmitted
      • Http --- no need to save the connection status
      • Tcp --- you need to save the connection status. During Exchange Data, a State is maintained.
      • MSMQ --- usually used when reliable Message transmission is required
      • Named Pipes --- usually used for communication between different processes in a single PC
  • Service
    • Service
      • Contract --- Contract
        • Data Contract-a Data Contract that tells the program that the Data can be used for WCF transmission and is usually used to specify a custom Model object. It is a type of serializable and the namespace is System. runtime. serialize.
        • Service Contract --- Service Contract, telling the program that this is the WCF Service
        • Operation Contract --- Operation Contract, which tells the program that this Operation can be performed externally exposed by the Service
      • Implementation --- specific Implementation of Operation Contract
    • Endpoint --- in the outside world, the connection address of the service
      • Address --- the specific service Address. It can be Http or Tcp based on the Transport type.
      • Binding --- the icons below are different types of Binding
      • Contract-interface exposed externally
    • Behavior --- a Behavior to be executed when the Service is executed, such as security verification
      • Throttling --- determines the number of threads that a Service can use at the same time, the number of Service instances, and the number of messages passed.
      • Security --- determines the Security characteristics of the Service
      • Instancing --- determines the number of instances that can be created for the Service implementation class
        • PerCall --- each Request from the client generates an InstanceContext
        • PerSession --- generates an InstanceContext Based on the Session of each client, and has the same lifecycle as this Session.
        • Single --- Singleton Mode
      • Error Handling --- processing method when the Service encounters an exception and needs to return information
      • Concurrency --- controls how many threads a InstanceContext can span
        • Multiple --- can span Multiple threads
        • Single --- can only be executed within a Single trip
        • Reentrant --- Each instance of the service can only process messages one at a time but can accept re-entrant operation CILS
      • Transactions --- determines whether the Service can accept and execute Transactions from the client. Note that the transaction is created by the client. Therefore, whether the transaction is completed is determined by the client. Of course, the same is true for the lifecycle.
      • Custom --- Custom Behavior
    • Host --- the Host of the WCF, that is, the place where the Service is running
      • Console Application
      • Web Application
      • WinForms Application
      • IIS
      • Service

The following is a simple example to illustrate the process of developing and calling a WCF.

First, we create a Console Application called WCFDemo.

2. Now that we want to develop WCF, we need to introduce a WCF Service Application called Service. If you do not want to create a separate project, you can add the Item of the WCF Service to another project. IService and Service are automatically generated when you create a WCF Project. svc, usually IService, this Service Contract is not required, because the Client that calls the Service also needs to be used, so we will put them in a unified place, in this example, the object is called Interface. The simple Service. svc is a very simple class that implements the IService interface. There is nothing special about it,

   1:  namespace Service
   2:  {
   3:      // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
   4:      public class Service : IService
   5:      {
   6:          public Computer GetComputer()
   7:          {
   8:              Computer computer = new Computer();
   9:              computer.ComputerName = "Ralax - PC";
  10:              return computer;
  11:          }
  12:      }
  13:  }

It should be noted that the ServiceClient inheritance relationship and constructor

After ServiceClient is complete, it is called by the Client, as follows:

   1:  namespace WCFDemo
   2:  {
   3:      class Program
   4:      {
   5:          static void Main(string[] args)
   6:          {
   7:              using(ServiceClient sClient = GetService())
   8:              {
   9:                  Console.WriteLine(sClient.GetComputer().ComputerName);
  10:                  Console.Read();
  11:              }            
  12:          }
  13:   
  14:          public ServiceClient GetService()
  15:          {
  16:              WSHttpBinding binding = new WSHttpBinding();
  17:              EndpointAddress address = new EndpointAddress(new Uri("http://localhost/WCFDemo/Service.svc"));
  18:              ServiceClient client = new ServiceClient(binding, address);
  19:              return client;
  20:          }
  21:      }
  22:  }
 
This example, I have packed it, can be downloaded here: http://files.cnblogs.com/myg2006/WCFDemo.rar

Wcf Development Learning: http://www.cnblogs.com/jillzhang/category/121346.html

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.