WCF review 1. Basic Concepts and application scenarios
I. WCF description
As a service-oriented communication framework Platform, wcf is widely used in Distributed frameworks. It takes only a few minutes to get started with a complete wcf program. In fact, wcf is a technology with many concepts and requires a lot of effort for in-depth research.
Ii. Advantages of WCF 1. Integrate various communication technologies
WCF is an integration of Web Service, Net Remoting, Enterprise Service, WSE, MSMQ, and other technologies.
The presentation layer can be used for soap XML transmission and rest http calls.
The transport layer can be based on http https tcp pipe msmq (when both of them can meet the requirements, I personally prefer tcp's comprehensive consideration in terms of performance and flexibility)
2. Adopt service-oriented implementation to make programming easier
Software Development Method: process-oriented programming-> object-oriented-> component-oriented development->Service-Oriented Architecture
Service-oriented advantages:
In a project, if there are multiple styles of technology, third-party DLL, or two technologies that cannot run in the same system, this situation will become difficult to maintain and code indecent, however, a service project that separates all these functions is called through one service interface and released in multiple environments, which not only decouples but also improves cohesion, the code is boring.
III. Basic concepts of WCF
WCF programs are dividedServerAndClient, The WCF program passesHostHost in process, readConfigurationAnd start the service.ProxyClass Implementation call.
1 contract:
(1) Service Contract: A group of interfaces exposed to the client. You must add the [ServiceContract] feature before the client can call the service.
(2) operation contract: Add [OperationContract] to the function name before the service can call this function.
[ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataByData(DataClass dc); }
(3) Data contract: the class passed by the client and the server must be marked with the attribute [DataContract] as a data contract, and the shared Member is marked with the DataMember feature in the member attribute of the class.
Note: Even if DataContract is not added, it is still DataContract by default. Only data contracts can be left empty.
[DataContract]public class DataClass{ [DataMember] public double TotalPrice { get; set; } }
(4) Message contract: This is interesting. There are a lot of online sayings and few are about application scenarios.
My understanding: first, the message contract and data contract both support the stream, and the returned stream can carry other information. These data contracts do the same thing with just a few more attributes.
The message contract focuses on the message. The data features consistency. You are "1", I am also "1 ", however, different messages can be said to be "1" or "1". The difference lies in diversity.
The message contract specifies the transmitted data to the soap head or soap body. You can use order to sort the data in multiple headers or bodies. You can also customize message formats to diversify the transmission formats, supports receiving by more devices and head encryption.
When all data in a data contract is together, we cannot control what the generated soap looks like.
Note that each contract has its own characteristic parameters. We will not introduce them here.
2 address
Each service can have multiple transmission protocols, and each transmission protocol can have only one call address.
For example:
Http: // 192.168.1: 800/Service1
Net. tcp: // localhost: 8080/Service1
3 proxy
Right-click a project to add a service reference. You can enter a service address to generate a proxy service conveniently. You can also use a tool to generate a proxy or hand-written proxy.
4. WCF configuration file
It can be divided into three bindings, services, and behaviors.
(1) services:
There can be multiple services. One service represents one service,
A service can have multiple endpoints. An endpoint mainly serves addresses, contracts, and bindings.
For example, an endpoint binding an http address is http: // 192.168.1.1: 80/service1. The service contract is IService.
(2) behaviors:
A service can have a behavior that specifies the service behavior, such as whether a service error outputs a detailed error or whether metadata is displayed (metadata is the WCF browser page)
(3) bindinds:
A service can have a bingding used to configure the transmission protocol and transmission parameter settings of the service.
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. web> <compilation debug = "true" targetFramework = "4.0"/> </system. web> <system. serviceModel> <bindings> <netTcpBinding> <binding name = "tcpBind" portSharingEnabled = "true" transferMode = "Buffered"> <readerQuotas maxDepth = "32" bytes = "8192" maxArrayLength = ""16384" maxBytesPerRead = "4096" maxNameTableCharCount = "16384"/> <security mode = "None"> </security> </binding> </NetTcpBinding> </bindings> <services> <service behaviorConfiguration = "MyServiceTypeBehaviors" name = "WcfService. sayService "> <endpoint binding =" netTcpBinding "bindingConfiguration =" tcpBind "contract =" WcfService. ISayService "/> <endpoint address =" mex "binding =" mexTcpBinding "contract =" IMetadataExchange "/> </service> </services> <behaviors> <serviceBehaviors> <behavior name = "MyServiceTypeBehavior S "> <! -- Publish metadata and delete it before publishing --> <serviceMetadata httpGetEnabled = "true"/> <! -- Output error, delete before release --> <serviceDebug includeExceptionDetailInFaults = "true"/> </behavior> </serviceBehaviors> </behaviors> </system. serviceModel> </configuration>
========================================================== ====================================== Purely personal understanding ==================== ========================================================== ===