The "WCF" Foundation

Source: Internet
Author: User
Tags msmq

What is WCF

WCF is the SDK for Windows platform-based switches and deployment services. WCF provides a runtime environment for services that enable developers to expose CLR types as services and to consume services in a CLR type. WCF is Microsoft's implementation of a range of product standards definitions, including service interactions, type conversions, marshaling, and the management of various protocols. Because of this, WCF is able to provide interoperability between services.

Service

A service is a set of features that are exposed. From the perspective of software design, the concept of software design has undergone several changes from function development to object, from object development to component, from component development to service. Service Orientation (serviceorientation so) is an abstraction of a set of principles and is a best practice for creating service-oriented applications.
Services can be local or remote, and can be developed by multiple parties using any technology. The client of the service is simply the party that uses the service functionality. Theoretically, the client can be arbitrary.
The client interacts with the service through the sending and receiving of the message. Messages can be passed directly between the client and the service, or through intermediaries. Messages in WCF are typically SOAP messages that are not related to the transport protocol.
Because the creation of a service is opaque to the outside world, WCF services typically describe the available functionality and how the service might communicate by exposing metadata. The publication of metadata can be pre-defined, regardless of the specific technology.

Execution boundary of the service

WCF does not allow clients to interact directly with the service, even if it calls services in local machine memory. Instead, the client always forwards the call to the service using a proxy. The actions exposed by the agent are the same as the services, and some methods of managing agents are added.
WCF allows clients to communicate with services across execution boundaries. In the same machine, clients can invoke services in the same application domain, or they can be called across application domains in the same process, or even across processes. In different machine situations, clients can interact with the service across the Internet or intrannet boundaries.

WCF and location transparency

WCF requires clients to maintain a consistent programming model regardless of the location of the service. Because all interactions are done through the proxy, requiring the same configuration and hosting, WCF needs to maintain the same programming model for both local and remote methods.

Address

Each service in WCF has a unique address. The address contains two important elements: the service location and the transport protocol , or the service
The transport style of the communication. The service location includes the machine name, site or network, communication port, pipeline, or queue, and an optional specific path or URI.
TCP Address
TCP addresses are transmitted using the NET.TCP protocol.
HTTP Address
HTTP addresses are transmitted using the HTTP protocol, or they can be safely transmitted using HTTPS. HTTP addresses are typically used as external Internet-based services.
IPC Address
The IPC address is transmitted using net.pipe, which means that it will use the Windows named pipe mechanism. In WCF, a service that uses named pipes can only receive calls from the same machine. Therefore, you must specify an explicit local machine name or a direct name of localhost when you use it. Only one named pipe can be opened per machine.
MSMQ Address
The MSMQ address is transmitted using NET.MSMQ, which uses the Microsoft Message Queuing mechanism. You must specify a queue name for the MSMQ address when you use it. If you are processing private queues, you must specify the queue type, but for public queues, the queue type can be omitted.
Peer network Address
The peer address is transmitted using NET.P2P, which uses the Windows Peer network transport mechanism.

Contract

All WCF services are exposed as contracts. Contracts are platform-independent and are a standard way of describing service functionality. WCF defines four types of contracts.
Service Contracts , which describe the service operations that the client can perform.
A data contract that defines the type of data that interacts with the service.
Error contracts , which define the errors that the service throws, and how the service handles errors and passes errors to the client.
A message contract that allows a service to interact directly with a message. A message contract can be either typed or untyped. Requiring the use of a message contract means customizing the application context so that it can be implemented by using a custom message header.

Service contract

ServiceContractAttribute is defined as follows:

false)]publicsealedclass ServiceContractAttribute : Attribute{    publicgetset; }    publicgetset; }    //更多成员}

The ServiceContract attribute can map a CLR interface to a technology-independent service contract that exposes a CLR interface (or Class) as a WCF contract. The WCF contract is independent of the access qualification of the type. One principle of service orientation is that service boundaries should be clear .
All members of a type are not necessarily part of the contract. We must use the OperationContractAttribute attribute to explicitly indicate that those methods need to be exposed as part of the WCF contract. OperationContractAttribute is defined as follows:

[AttributeUsage(AttributeTargets.Method)]publicsealedclass OperationContractAttribute : Attribute{    publicgetset; }    //更多成员}

The service class also has some implementation constraints. We want to avoid using the parameter constructor, because WCF can only use the default constructor. Similarly, while classes can use internal properties, indexers, and static members, WCF clients cannot access them.

Name and Namespace

You can define namespaces for contracts. The namespace of the contract has a with. NET programming the same purpose: to determine the type scope of the contract to reduce the type of conflict probability.

Managed

WCF service classes cannot exist out of thin air. Each WCF service must be hosted (host) in a Windows process, which is referred to as the host process. A single host process can host multiple services, and the same service type can be hosted in multiple hosting processes.
self-hosted
When a process (or machine) boundary between a client and a service needs to be determined, using in-process hosting, when the service is in the same process as the client.
The configuration file for a managed application typically lists all the types of services that you want to host and expose

<system.serviceModel>    <services>        "Namespace.Service">        ...        </service>    </services></system.serviceModel>

In addition, the host process must explicitly register the service type at run time and open the host for the client's call, so we require the host process to run before the client call arrives.
The method of creating the host is typically called the ServiceHost class in the main () method.
When you create a ServiceHost object, you need to provide the service type for the ServiceHost constructor, which is optional for the default base address.
Each ServiceHost instance is related to a specific service type, and if the host process needs to run multiple service types, you must create multiple ServiceHost instances that match it. In the host program, you can allow the call to pass in by calling the open () method, and you complete the call in the process by calling the close () method to terminate the host instance.
The Icommunicationobject interface implemented by ServiceHost defines some advanced features

 Public Interfaceicommunicationobject{voidOpen ();voidClose ();voidAbort ();EventEventHandler Closed;EventEventHandler Closing;EventEventHandler Faulted;EventEventHandler opened;EventEventHandler Opening;    IAsyncResult Beginclose (AsyncCallback callback, Object State); IAsyncResult Beginopen (AsyncCallback callback, Object State);voidEndclose (iansycresult result);voidEndopen (iansycresult result); Communicationstate State {Get; }} Public enumcommunicationstate{Created, Opening, opened, Closing, Closed, Faulted}

servicehost< T > class
The servicehost< T > class can improve the ServiceHost class provided by WCF.

 Public classServicehost<t>: servicehost{ Public ServiceHost() :Base(typeof(T)) {} Public ServiceHost(paramsString[] baseaddresses):Base(typeof(T), baseaddresses)) {} Public ServiceHost(paramsUri[] (baseaddresses)):Base(typeof(T), baseaddresses)) {}StaticUri[] Convert (string[] baseaddresses) {converter<string, uri> Convert = (address) = = {return NewUri (address); };returnBaseaddresses.convertall (convert); }}
Binding

The modes of communication between services are varied and there are many possible modes of communication.
A synchronous request/reply message, or an asynchronous "send and discard" message;
Two-way message;
Instant message or queue message;
Persistent queue or variable queue.
There are many possible transport protocols for the message. It also includes some possible message encoding formats: Plain text encoding format, binary encoding format, MTOM encoding format.
The security of the message multiple policies: No security policy is implemented, only the security policy of the transport layer is provided, and the privacy protection and security policy of the message. WCF also includes a variety of security policies for client authentication and authorization.
A binding encapsulates a collection of related options such as transport protocols, message encodings, communication patterns, reliability, security, things propagation, and interoperability, so that they remain consistent. Ideally, we want to free all the complex basic functional modules from the service code, allowing the service to focus only on the implementation of the business logic. Bindings enable developers to use the same service logic based on different basic functional modules.

Common bindings

The basic binding is provided by the BasicHttpBinding class. Basic binding exposes WCF services to traditional ASMX Web services, enabling older clients to collaborate with new services. Basic bindings make your service look like a traditional Web service that can communicate based on basic Web service information.
The TCP binding is provided by the NetTcpBinding class. TCP bindings use the TCP protocol to enable cross-machine communication across the Internet. TCP bindings support a variety of features, including reliability, transactional, security, and optimization of communication between WCF. The premise is that it requires both the client and the service to use WCF.
The IPC bindings are provided by the NetNamedPipeBinding class. It uses named pipes to transmit traffic to the same machine. This is the safest way to bind because it cannot receive calls from outside the machine. It is also the best-performing binding because the IPC protocol is lighter than TCP.
Web Service (WS) bindings are provided by the Wshttpbinding class. WS-Bindings are transmitted using HTTP or HTTPS, providing multiple features (such as reliability, transactional, and security) for Internet-based communications that follow the WS-* standards.
ws bidirectional binding is provided by the Wsdualhttpbinding class. WS-Bidirectional binding is similar to WS-Binding, but it also supports bidirectional communication from service to client. There is no industry standard for setting callbacks, so wsdualhttpbinding is not interoperable.
The MSMQ binding is provided by the NetMsmqBinding class. Use MSMQ for transport to provide support for disconnected queue calls.

name Transport Protocol encoding Format Interoperability
BasicHttpBinding Http/https Text,mtom Yes
NetTcpBinding Tcp Binary No
NetNamedPipeBinding Ipc Binary No
Wshttpbinding Http/https Text,mtom Yes
Wsdualhttpbinding HTTP Text,mtom No
NetMsmqBinding Msmq Binary No
End point

Services are related to addresses, bindings, and contracts. Where the address defines the location of the service, the binding defines how the service communicates, and the contract defines the content of the service. An endpoint is a mix of addresses, contracts, and bindings.
The address is the memory address of the type virtual table, the binding is the CLR, and the contract represents the interface itself.

WCF Foundation

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.