WCF-Metadata
The metadata of WCF describes how the client interacts with the service. Using metadata, we can use some tools to generate proxyCodeFor example, svcutil.exe, client programming is basically based on these proxies
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 the metadata exchange method, which often uses a dedicated endpoint, called the metadata exchange endpoint. Similar to other endpoints, the metadata exchange endpoint still contains addresses, bindings, and contracts. However, the service contract used is the interface imetadataexchange provided by WCF.
In fact, the two metadata publishing methods represent two different standard protocols, the former being HTTP/get requests, and the latter being WS-metadataexchange (MEX ). In WCF, The metadataexchangeclientmode Enumeration type indicates the two metadata exchange modes:
Public Enum metadataexchangeclientmode {metadataexchange, httpget}
1. HTTP-GET
When metadata is released in this way, the client can view and confirm the metadata in a browser. Same as previous articlesArticleAs described above, there are two ways to enable it: editing and configuration.
Configuring metadata publishing is implemented by adding servicebehavior. The example is as follows:
<System. servicemodel> <services> <service name = "mynamespace. myservice "behaviroconfiguration =" mexget "> <endpoint contract =" mynamespace. imyservice "binding =" wshttpbinding "address =" http: // localhost: 8000/myservice "/> </service> </services> <behaviors> <servicebehaviors> <behavior name =" Mexico get "> <servicemetadata httpgetenabled =" true "/> </Behavior> </servicebehaviors> </behaviors> </system. servicemodel>
Programming Method:
Servicehost host = new servicehost (typeof (myservice); servicemetadatabehavior metadatabehavior = host. description. behaviors. find <servicemetadatabehavior> (); If (metadatabehavior = NULL) {metadatabehavior = new servicemetadatabehavior (); metadatabehavior. httpgetenabled = true; host. description. behaviors. add (metadatabehavior);} binding wshttpbinding = new wshttpbinding (); host. addserviceendpoint (typeof (imyservice), wshttpbinding, new uri ("http: // localhost: 8086/myservice/"); host. open ();... host. close ();
2. Metadata-exchange
This method publishes metadata by defining a specific endpoint, which is called the metadata Endpoint or Mex endpoint. The ABC of WCF for the Mex endpoint provides their respective definitions.
- A-similar to a common endpoint address.
- B-binding Based on HTTP, https, TCP, and ICP protocols. For example, mextcpbinding, mexnamepipebinding, and mexhttpbinding ....
- C-imetadataexchange interface. The implementation is automatically provided by WCF.
The following is an example of how to configure the Mex endpoint:
<System. servicemodel> <services> <service name = "mynamespace. myservice "behaviroconfiguration =" mex "> <endpoint contract =" mynamespace. imyservice "binding =" wshttpbinding "address =" http: // localhost: 8000/myservice "/> <endpoint contract =" imetadataexchange "binding =" mexhttpbinding "address =" http: // localhost: 8000/MEX "/> </service> </services> <behaviors> <servicebehaviors> <behavior name =" mex "> <servicemetadata/> </behavior> </servicebehaviors> </behaviors> </system. servicemodel>
You can also start the Mex endpoint by programming:
Bindingelement = new tcptransportbindingelement (); custombinding binding = new custombinding (bindingelement); Uri tcpbaseaddress = new uri ("net. TCP: // localhost: 9000 "); servicehost host = new servicehost (typeof (myservice), tcpbaseaddress); If (metadatabehavior = NULL) {metadatabehavior = new servicemetadatabehavior (); host. description. behaviors. add (metadatabehavior);} host. addserviceendpoint (typeof (imetadataexchange), binding, "mex"); host. open ();... host. close ();