1. Concepts
WCF is a software development kit for developing and deploying services based on the Windows platform. WCF provides a runtime environment for services, allowing developers to publish the CLR type as a service and use the service in the CLR type. You do not need to use WCF to create a service. However, using WCF can make the task of creating a service get twice the result with half the effort. WCF is the implementation of Microsoft's definition of a series of industrial standards, including service interaction, type conversion, envelope aling, and management of various protocols. WCF also provides developers with a majority of applicationsProgramBasic function modules. Such as hosting, service instance management, asynchronous call, reliability, transaction management, offline queue call, and security.Most functions of WCF are included in a separate assembly system. servicemodel. dll. The namespace is system. servicemodel.
2. Service
A service is a set of functions. The service can be local or remote, and multiple participants can use any technology for development. The Service has nothing to do with the version, and can even be executed at the same time in different time zones. The service contains many concepts such as language, technology, platform, version, and framework. Interactions between services are only allowed in the established communication mode. The service client is only the party that uses the service functions.
All messages in WCF are soap messages.Messages are irrelevant to the transmission protocol. Since the creation of a service is not transparent to the outside world, the WCF Service usually describes available functions and possible communication modes of the service in the form of public metadata (metadata.A non-WCF client can import metadata as a local type to the local environment. the WCF client can also import metadata of non-WCF services and then call it using local CLR classes and interfaces.
WCF does not allow clients to directly interact with services. The Client Always uses a proxy to forward the call to the service.The Operations published by the proxy are the same as those of the service, and some methods for managing the proxy are also added. WCF allows clients to communicate with services across execution boundaries. On the same machine, the client can call services in the same application domain, or call services across application domains or even processes in the same process. Clients can also interact with services across the boundaries of the Intranet or the Internet.
3. location transparency
Similar to other distributed technologies, WCF requires the client to maintain a consistent programming model without considering the service location, but the implementation method is different. Even if the object is local, WCF still uses the remote programming model instantiation method and uses the proxy.Since all interaction operations are performed by proxy and the same configuration and hosting mode is required, for both local and remote methods, WCF only needs to maintain the same programming model.This allows developers not to affect the client because of service location changes, but also greatly simplifies the programming model of the application.
4. Address
Each service in WCF has a unique address ). The address contains two important elements:Service Location and transmission protocol(Transport Protocol ). The service location includes the target machine name, site or network, communication port, pipeline or queue, and an optional specific path or Uri.
Five transmission styles: HTTP, TCP, peer network (peer network), IPC (internal process Communication Based on the named pipeline), and MSMQ.
For example, http: // localhost: 8001/
Net. TCP: // localhost: 8002/myservice
Net. Pipe: // localhost/mypipe
Net. MSMQ: // localhost: 8002/myservice
5. Contract
All the services in WCF are published as contract ). The contract has nothing to do with the platform.
Four types of contracts:
Service Contract: a service contract describes the service operations that the client can perform.
Data contract: a data contract defines the data type that interacts with a service.
Error contract: (fault contract): the error contract defines the error thrown by the Service.
Message contract: (Message contract): a message contract allows services to directly interact with messages.
Service Contract
Servicecontract features expose CLR interfaces (or classes) as WCF contracts.The WCF contract has nothing to do with the type access restriction, because the type access restriction belongs to the CLR concept.
Operationcontract features expose methods in the WCF Service Contract,This feature can only be applied to methods.,It is not allowed to be applied to attributes, indexers, and events of the same CLR concept.WCF can only recognize operations as logical functions (operation ).
Contract operations do not allow reference objects as parameters, but only basic types or data contracts.
Avoid using constructors with parameters for service classes, because only the default constructor can be used for WCF. Similarly, although the class can use internal attributes, indexers, static members, and so on, the WCF client cannot use them.
7. Hosting)
The WCF Service class cannot exist out of thin air,Each WCF Service must be hosted (hosting) in a Windows Process, which is called a host process ).A single host process can host multiple services, and the same service type can also be hosted in multiple host processes.
Three hosting methods:
IIS:It is only applicable to the HTTP protocol. The advantage is that the host process can be automatically started when the client submits the first request.
Was:Windows activation service of Vista. The main difference between was and IIS is that was is not limited to HTTP. It supports all available WCF transfer protocols.
Self-managed:Write your own console, winform, and Windows services to manage the lifecycle of the host process. The process must run before the client call. The host process must explicitly register the service type at run time and open the host.
The servicehost class is used to create a host.. Each servicehost instance is related to a specific type of local service. If the host process needs to run multiple service types, you must create multiple matching servicehost instances, the main thread can call the Open and Close methods to control the host.When the host is opened, a worker thread is started to monitor incoming requests during the WCF runtime.Because a working thread is introduced, the main thread of the process can execute the blocking operation after opening the host.
We can also register multiple hosts for the same type as long as these hosts use different base addresses.
Servicehost implements the icommunicationobject interface. If it takes a long time to open or close the host, you can call the beginopen and beginclose Methods asynchronously.
8. Bind
A single service supports multiple bindings on each address. WCF defines nine standard states.
9. endpoint)
WCF uses an endpoint to represent such a composite relationship. The endpoint is the mixed finished product of the address, contract, and binding. The address defines the service location, the binding defines the service communication mode, and the contract defines the service content. Each endpoint contains these three elements, and the host is responsible for making the endpoint public.Logically, an interface is equivalent to an endpoint.Each service must expose at least one business endpoint. Each endpoint has only one contract. All endpoints on a service contain a unique address, and a single service can publish multiple endpoints. These endpoints can use the same or different States to publish the same or different contracts.There is absolutely no association between different endpoints provided by each service.
You can use the configuration file in administrative mode (administratively) or programmatically to configure the endpoint.
10. Metadata Exchange
There are two solutions for a service to publish its own metadata. One is to provide metadata based on the http-Get protocol. The other is to use the dedicated endpoint Mex.
11. Client Programming
To call a service operation, the client first needs to import the service contract to the local description (native representation) of the client. If the client uses WCF, a common method to call an operation is to use a proxy. Proxy is a CLR class. If the Service supports multiple contracts, the client requires a proxy to correspond to multiple contract types. The proxy not only provides the same operations as the service contract, but also includes methods for managing object lifecycles and service connections. The proxy completely encapsulates every aspect of the Service: service location, implementation technology, runtime platform, and communication transmission.
12. Client Configuration
The client information is exactly the same as the information obtained from the service endpoint. Client configuration can be implemented through management and programming. We recommend that you never use the svcutil tool to generate a configuration file. Because it automatically sets the default value for key states, it leads to confusion in the entire configuration file.
WCF provides the configuration file editor: svcconfigeditor.
13. Proxy
The proxy class is derived from the clientbase <t> class. The clientbase <t> class identifies service contracts encapsulated by a proxy through generic type parameters. The channel attribute type in this class is the type of the generic parameter. The clientbase <t> subclass calls the service contract method it points to through the channel. A channel object is a proxy object equivalent to a service contract. through it, you can call related methods of the service contract.
The best practice is to disable the agent after the client calls the agent. Because disabling proxy will terminate the service session and close the connection.
14. call timeout
Each call of the WCF client must be completed within the configured timeout value. For whatever reason, once the call time exceeds this time limit, the call will be canceled and the client will receive a timeoutexception exception.You can modify the timeout value by modifying the sendtimeout attribute of binding.
15. programming and management configuration
The two technologies have their own strengths and are consistent with each other. The management configuration method allows developers to modify the main features of services and clients after deploying services without re-compiling or re-deploying them. The main defect is that it does not have type security. Configuration errors can only be detected during running.
If the configuration decisions are completely dynamic, you can use the programming configuration method. It can process the service configuration at runtime based on the current input or conditions.