WCF is a framework product on the. net platform launched by MS based on SOA (Service Oriented Architecture. WCF is a unified framework for building and running service-oriented applications using managed code. It enables developers to build a secure, reliable, and transactional solution across platforms, and to be compatible with existing systems. WCF is a mass developer of MS distributed application development. It integrates all technologies related to distributed systems on the. Net platform.
Benefits of WCF:
1. Uniformity
WCF is the integration of some technologies, and it is fully written by managed code. Therefore, there is no big difference between developing a WCF application and developing other. Net applications.
2. Interoperability
The most basic communication mechanism of WCF is SOAP, which ensures the interoperability between systems, even when different contexts are run. This communication can be based on. net. net Communication. cross-process, cross-machine, or cross-platform communication is supported as long as standard WebService is supported. applications can run in windows.
3. Security and trustworthiness
WS-ReliableMessaging is added to the Soap header to allow trusted end-to-end communication.
4. Compatibility
Installing WCF does not affect the original technology (ASMX, Remoting)
A WCF Service is composed of an Endpoints set. Each Endpoint is the entry for communication. The client and server exchange information through the Endpoint.
Address is the network Address of the Endpoint. It indicates the destination of the message.
Binding describes how to send messages. (For example, the transmission protocol TCP or HTTP for sending messages)
Contract describes the content of a message and the message organization and operation methods. (One-way, duplex, request/reply)
The meanings of ABCs in Endpoint are: where, how, and what.
When a message is sent by WCF, the address knows the address of the message to be sent, the binding knows how to send it, and the contract knows what the message is sent.
In WCF, ServiceEndpoint indicates an Endpoint. The EndpointAddress, binding, and ContractDescription types in the class correspond to the Address, Binding, and Contract of the Endpoint respectively.
The EndPointAddress class also contains URI, Idnentity, and an optional headers set.
The uniqueness of Endpoint security is usually identified by its URI value.
(However, to avoid URI duplication in some special cases, Identity is introduced to the URI to ensure the uniqueness of the Endpoint address)
Optional AddressHeader provides additional information, especially when multiple endpoints use the same URI address information, AddressHeader is necessary.
The Binding class contains the Name, Namespace, and BindingElement sets.
Binding Name and Namespace are the unique identifiers of service metadata.
BindingElement is the binding method for WCF communication.
For example:
SecurityBindingElement: the Endpoint uses the SOAP message security mode.
ReliableSessionBindingElement: the Endpoint uses trusted messages to ensure message transmission.
TcpTransportBindingElement: the Endpoint uses TCP as the communication transmission protocol.
ContractDescription is used to describe the Contracts of WCF and Their operations.
In the ContractDescription class, each Contract operation has a corresponding OperationDescription to describe the operation type.
OperationDescription also contains the MessageDecription set to describe Message.
In the WCF programming model, ContractDescription is usually created in the interface or class that defines Contract.
This interface or class type is marked with ServiceContractAttribute, while its Operation method is marked with OperationContractAttribute.
* Of course, we can also create them manually without using the CLR Attribute.
Contract also contains a set of ContractBehavior. The ContractBehavior type can be used to modify or extend the behavior of Contract.
The diagram is as follows:
ContractDescription
--- Name
--- NameSpace
--- OperationDescription
---- MessageDescription
--- IContractBehavior
In fact, WCF provides Behavior, which can modify or expand some functions of the client and server.
Security behavior is also used to control Security and authorization.
Transaction behavior controls transactions
Since WCF needs to manage the communication between the server and the client.
Server: WCF provides ServiceDescription class to describe a WCFservice
Client: WCF manages the communication Channel used to send messages. The ChannelDescription class describes such a client Channel.
ServiceDescription
---- Service Type
---- IServiceBehavior
---- ServiceEndpoint
---- EndpointAddress
---- Binding
---- ContractDescription
We can use the code to Create ServiceDescription object, or using WCF's Attribute, or using tool--SvcUtil.exe.
Next we will use Hello World to end the first section of WCF.
Using System. ServiceModel;
// Mark [ServiceContract], which makes the Class a wcf Service
[ServiceContract]
Public class HelloWorld
{
// Mark [OperationContract] to become an Operation of the Service
[OperationContract]
Public void Hello ()
{
Console. WriteLine ("Hello World! ");
}
}
However, it is recommended that the interface be defined as a Service, which makes the WCF Service more flexible.
After all, an interface can be implemented by multiple classes at the same time, which also means that multiple ServiceContract implementations can be implemented.
The above code can be modified:
[ServiceContract]
Public interface IHello
{
[OperationContract]
Void Hello ();
}
The HelloWorld class implements the IHello interface:
Public class HelloWorld: IHello
{
Public void Hello ()
{
Console. WriteLine ("Hello World! ");
}
}
The last WCFService must have a host as its running environment.
The Host can be Asp.net, Windows service, and common applications.
The following is the Host code:
Using System. ServiceModel
Public class HostApp
{
Static void Main (string [] args)
{
ServiceHost host = new ServiceHost (typeof (HelloWorld), new Uri (http: // localhost: 8080/HelloService ));
Host. AddServiceEndpoint (typeof (IHello), new BasicHttpBinding (), "Svc ");
Host. Open ();
Console. WriteLine ("Start Your Service .");
Console. ReadKey ();
Host. Close ();
}
}
We have created a ServiceHost object, through which we can create a WCF Runtime, which is a group of objects responsible for receiving and sending messages.
ServiceHost can create a SerivceDescription object. ServiceDescription and ServiceHost can be used to create an EndpointListener for each ServiceEndPoint.
ServiceHost
--- ServiceDescription
--- EndpointListener
* The type parameter passed during ServiceHost creation cannot be interface.
So here I pass in typeof (HelloWorld ).
The AddServiceEndpoint () method adds an Endpoint to the Host.
The parameter is exactly the three parts of Endpoint (Address, Binding, Contract ),
In this case, IHello is ServiceContract, and its method Hello is OperationContract.
The Open () method is used to create and Open the Service runtime;
Close () method to Close this runtime. (In fact, this method can not be called, because the system will automatically Close the Host after the application ends .)
However, you all need to develop a good habit. WCF still requires explicit calling of the Close () method.
When the Service is running, it uses a Channel to transmit messages. When open service runtime, using a Channel in System, invoke back, we need release using for channel. so maybe use "using" to manage resource of the ServiceHost release.
Well, after defining a WCF Service and running it on the host, how can it communicate with the client?
See the next article :)