I sorted out several basic knowledge points in WCF:
1. Message in WCF
The Message object in WCF is stateful. Its status is MessageState enumeration. There are several types:
Public enum MessageState
{
Created,
Read,
Written,
Copied,
Closed
}
Message objects can be read only in the Create State. Otherwise, an exception occurs during reading. Message
Many methods such as Write are also defined. These Write methods can only be used for messages in the Created state.
In WCF, you may need to read or perform other operations on messages. However, the read operation will change the Message
Status. In this case, you can use the CreateBufferedCopy method defined by Message.
Its signature is as follows:
Public MessageBuffer CreateBufferedCopy (int maxBufferSize );
It returns the MessageBuffer object. It can Create multiple times and return the Message object, and the Message status is Created.
2. message encoding formats in WCF: Text, MTOM, and Binary. The Text format is platform-independent. MTOM, based on WS-* MTOM specifications, is
The Binary data of large data volumes is in the optimized format used during SOAP transmission and is not related to the platform. Binary data is encoded in Binary format,
Only for. Net platforms.
3. Metadata Exchange Terminator metadata exchange Terminator is a special Terminator. It also becomes a MEX endpoint and supports metadata exchange standards;
The service can publish its own metadata based on it.
For WebService, metadata is published by means of WSDL. For WebService,
Is to automatically publish the metadata of the service, so we can use the WSDL tool to generate the WebService proxy class.
In WCF, a service can choose not to publish metadata, even if it supports cross-platform HTTP protocol.
However, we can generate a Service proxy by publishing metadata exchange endpoints.
WCF automatically provides the IMetadataExchange interface for the service host. For the metadata exchange endpoint,
WCF provides dedicated binding elements to support different protocols (such as HTTP, TCP, and IPC. For example, HTTP corresponds
MexHttpBinding; for Tcp, mexTcpBinding; for IPC, mexNamedPipeBinding.
The configuration of metadata exchange endpoints under HTTP, TCP, and IPC protocols is as follows:
<System. serviceModel>
<Behaviors>
<ServiceBehaviors>
<Behavior name = "mex">
<ServiceMetadata/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
<Services>
<Service name = "Service. CalculatorService" behaviorConfiguration = "mex">
<Host>
<BaseAddresses>
<Add baseAddress = "net. tcp: // 127.0.0.1: 3636/mexTcp"/>
<Add baseAddress = "http: // 127.0.0.1: 6363/mexHttp"/>
<Add baseAddress = "net. pipe: // 127.0.0.1"/>
</BaseAddresses>
</Host>
<Endpoint address = "net. tcp: // localhost: 3636/SessionfulCalculator" binding = "netTcpBinding" contract = "Contract. ICalculator"> </endpoint>
<Endpoint address = "http: // localhost: 6363/SessionfulCalculator" binding = "wsHttpBinding" contract = "Contract. ICalculator"> </endpoint>
<Endpoint address = "netpipe" binding = "netNamedPipeBinding" contract = "Contract. ICalculator"> </endpoint>
<Endpoint address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange"> </endpoint>
<Endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"> </endpoint>
<Endpoint address = "mex" binding = "mexNamedPipeBinding" contract = "IMetadataExchange"> </endpoint>
</Service>
</Services>
</System. serviceModel>
In this way, regardless of the HTTP, TCP, and IPC methods supported by the service, we can generate the proxy class through the SVCUtil tool.
Then access the service.
In WCF, for the HTTP protocol, we can directly configure the service behavior through httpGetEnabled = "true"
And does not support other protocols. For other protocols, we want to publish metadata,
Configuration is obviously a good method.
Configure the metadata exchange endpoint, for example, <endpoint address = "mex" binding = "mexNamedPipeBinding"
Contract = "IMetadataExchange"> </endpoint>
If the address configuration is empty or the name configuration in behavior is the same, you can directly add
BaseAddress: Generate proxy class. For example:
If the name configuration in address and behavior is different, when the proxy class is generated, SVCUtil needs to add baseAddress +
<Endpointaddress = "mex" binding = "mexNamedPipeBinding" contract = "IMetadataExchange"/>
The attribute value of the address in. For example:
When metadata exchange endpoints are used, if the endpoints supported by the Service Support HTTP binding, regardless of the following configuration: <behavior name = "mex">
<ServiceMetadata httpGetEnabled = "false"/>
</Behavior>
The proxy class can be generated based on the value of httpGetEnabled in.