Handling Metadata with Indigo - Browsing and Exporting Metadata
(Page 4 of 4 )
To consume a service, clients require access to service metadata, including the service contract, any custom data types, and binding requirements. Earlier in this chapter, you learned how to enable the service metadata behavior and how to expose a metadata exchange endpoint to support proxy generation using SvcUtil. This lab illustrates how to view metadata in the browser and how to export that metadata to files for offline consumption. This capability is useful for a few reasons:
- For debugging purposes, it can be helpful to view the WSDL document, for example when trying to solve interoperability issues between platforms.
- Allowing client developers to eat up web server resources by browsing to dynamically created metadata is suboptimal. Instead, once the contract is stable, you should export it and allow developers to browse static files.
- It may be helpful to send developers the WSDL document via some other delivery mechanism such as email. This way they can generate proxies while offline.
Browsing metadata
Services may expose one or more endpoints, all of which are included in the service metadata. When a WSDL document is generated, for example, this document describes the contracts exposed across all endpoints. In other words, the WSDL document is one-to-one with the service. You can browse to any service if they have an HTTP base address. In the case of self-hosting environments, the <host> section of the service configuration can supply the base address.
For services hosted in IIS, the base address is the application directory in IIS with the .svc endpoint. For example, in this lab you would browse to http://localhost/IISHostedService/Service.svc.
When you browse to a service's base address you are presented with the service help page. The service model dynamically generates this for you. If you haven't enabled the metadata behavior, the help page will still be presented with instructions on how to do this. If you have enabled the metadata behavior but have forgotten to enable browsing, you'll receive the same instructions. In configuration, if you sethttpGetEnabledtotrue, the help page will produce a link to the WSDL document (Figures 1-27 and 1-28):
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
The service metadata behavior is required if you expose a metadata exchange endpoint for generating proxies, but you may want to explicitly disable both the help page and metadata browsing by adding the service debug behavior withhttpHelpPageEnabledset tofalseand by settinghttpGetEnabledtofalse:
<behavior name="serviceBehavior">
<serviceDebug httpHelpPageEnabled="false" />
<serviceMetadata httpGetEnabled="false" />
</behavior>
The WSDL document is dynamically generated each time metadata is accessed. During development this is a useful feature to have, but once you publish your service to production, it may be desirable to suppress dynamic generation to reduce overhead on the web server. But what if you want to provide a link to static metadata? An alternative is to leave metadata browsing enabled and provide a static file where the WSDL document can be retrieved. This is achieved by providing a value for theexternalMetadataLocationattribute:
<behavior name="serviceBehavior">
<serviceDebug httpHelpPageEnabled="false" />
<serviceMetadata httpGetEnabled="true" externalMetadataLocation="http://localhost/
IISHostedService/www.thatindigogirl. com.2006.06.wsdl"/>
</behavior>
Exporting metadata
To produce a static WSDL document, you can export service metadata using SvcUtil, as illustrated in this lab. SvcUtil uses the mex endpoint to retrieve service metadata and save it to a WSDL document that can be stored on the filesystem.
The command switch for SvcUtil to export metadata is/t:metadata. This command dumps the service metadata to several .wsdl and .xsd files in the specified directory:
svcutil
/d:<YourLearningWCFPath>\Labs\Chapter1\IISHostedService /t:metadata http://
localhost/IISHostedService/service.svc
The service model spreads the service description across multiple files. These files have a hierarchical relationship where a root .wsdl imports child .wsdl and .xsd files. In reality they are all one service description if you denormalize the output. With this output, you can still use SvcUtil to generate code for client applications as this lab illustrates.
Please check back next week for the continuation of this series.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter one of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.
|
|