Summary of WCF server development and client reference, and summary of wcf server reference
1. server development (including boarding)
1.1 create a WCF Service
Creating a WCF Service always creates a service interface and a service interface implementation. Generally, there are two creation methods based on the service host.
(1) create a WCF Application
By creating a WCF Service Application, you can generate a file with the. svc suffix. This file is similar to the aspx file in webform and has post-code, which is the implementation of service interfaces. The created WCF Service is hosted on IIS or WAS.
(2) create a WCF Service Library
The WCF Service library is a class library. As described above, it contains a service interface and Service implementation, the created WCF Service can be hosted on a host such as a windows client, a windows service, or a console program as needed.
1.2 WCF Service Protocol
The Protocol defines the functions that the server can provide and the functions that the client can use. It is independent of the service implementation code and can be divided into the following five protocol types.
(1) Data agreements
It is mainly used to define complex data types transmitted between the server and the client. Defining data protocols for complex data types is mainly applicable when the client shares a protocol assembly with the server. If the client creates a Service proxy by adding a reference method, it is not necessary to define a data protocol. The classes of data protocols are marked with the [DataContract] feature. The attributes to be serialized must be marked with the [DataMember] feature.
Note: webservice uses XML serialization. This serialization only serializes public fields and attributes. Therefore, this serialization method is used if data protocols are not specifically defined for complex data types.
(2) Service Agreements and operation agreements
Service Agreements and operational agreements are the two most important agreements. Creating a WCF Service must have these two agreements.
A service contract is used to define the WSDL of a service. It is usually an interface (or a class) and is marked with the [ServiceContract] feature. In the service agreement, the Operation Agreement defines the operation of the service and marks it with the [OperationContract] feature.
ServiceContract and OperationContract have several important attributes:
Coordination |
Genus |
Description |
ServiceContract |
SessionMode |
Defines whether the service agreement allows a session. The value is defined using SessionMode enumeration, including Allowed, NotAllowed, and Required. |
OperationContract |
IsOneWay |
Whether to perform one-way operations. If one-way operations are performed, the client does not wait for the response from the server. Generally, they can be used for operations without return values. |
IsInitiating |
Whether the current operation starts a new session. |
IsTerminating |
Whether to disable the session in the current operation. |
(3) Message protocols
You can specify which data is transmitted in the soap header and the body. [MessageContract] indicates the data class of the Message Protocol, and [MessageHeader] and [MessageBodyMember] indicate the members of the class in the soap header and body.
(4) incorrect agreement
By default, for the sake of security, errors on the server end are not returned to the client, but errors containing useful information can also be returned to the caller. To return an error to the client, set the includeExceptionDetailInFaults attribute of serviceDebug in webConfig to true. A strongly typed soap error protocol can be defined by a data protocol. The [FaultContract] feature is used in the corresponding operation protocol, and the type of the error is indicated in the attribute of this feature.
1.3 WCF Service host
The Service hosting of WCF is rich, including WAS (Windows Activation Service, Windows Activation Service), IIS, Windows applications, Windows Services, and console applications.
When creating a WCF Service, if a WCF application is created, its service suffix is. svc, and its host is IIS or WAS. If a WCF Service library is created, it can host on any host by instantiating ServiceHost and calling the open () method of its object, the related binding and Endpoint configurations can be implemented by writing code (assigning values to ServiceHost object attributes) without configuring in webConfig ().
1.4 server configuration file of the WCF Service
The configuration of the WCF server and client can be found in the "system. serviceModel" section of the application configuration file. The configuration items in this section mainly include bindings, behaviors, and services. bindings and behaviors serve the services Section.
(1) bindings
The bindings section defines binding. The defined binding can be added to the bindingConfiguration attribute of the endpoint Section of the service. Of course, this section can also be removed directly. Because. net predefines some bindings, you can directly use these predefined bindings in the endpoint section.
(2) behaviors
The behaviors section defines whether the client can obtain metadata by adding references. When the server reports an error, the client can also see the error.
(3) services
The services section defines the provided services, which can have multiple services. Each service has two subnodes: host and endpoint. Host can define the base address of the service. If the service must support multiple transmission protocols, it can also have multiple base addresses. The service endpoint needs to configure the relative address, binding, and service agreement of the service (if special binding and behavior are defined, these special binding and behavior need to be added ). If the service is a self-hosted service, you need to add another mex endpoint for the client to obtain metadata.
* In addition to manual configuration in the application configuration file, VS also provides a visualization tool (the Wcf Service configuration Editor, under the tool menu) for configuration.
2. Client development
2.1 Client Reference
The client generates a proxy for the WCF Service to call some methods provided by the Service. There are two ways for the client to generate a proxy:
(1) generate a proxy class by obtaining the wsdl: the client adds a reference through vs to access the server's WSDL document and generates a proxy class.
(2) generate a proxy through the shared protocol Assembly: the client references the assembly of the service agreement and uses the ChanelFactory <TChanel> class to write code to generate the Service proxy, thus calling the service method. The constructor of the ChanelFactory <TChanel> class needs to pass the binding information and Endpoint address to instantiate and create a channel. After the service is used up, its object also needs to call the close () method to close the channel.
2.2 client Configuration
The client must configure an endpoint that is consistent with the server (in the client Section). The properties to be configured in the endpoint include the service address, Service binding, and service agreement.