description | title | ms.date | dev_langs | ms.assetid | ||
---|---|---|---|---|---|---|
Learn more about: How to: Publish Metadata for a Service Using Code |
How to: Publish Metadata for a Service Using Code |
03/30/2017 |
|
51407e6d-4d87-42d5-be7c-9887b8652006 |
This is one of two how-to topics that discuss publishing metadata for a Windows Communication Foundation (WCF) service. There are two ways to specify how a service should publish metadata, using a configuration file and using code. This topic shows how to publish metadata for a service using a code.
Caution
This topic shows how to publish metadata in an unsecure manner. Any client can retrieve the metadata from the service. If you require your service to publish metadata in a secure manner. see Custom Secure Metadata Endpoint.
For more information about publishing metadata in a configuration file, see How to: Publish Metadata for a Service Using a Configuration File. Publishing metadata allows clients to retrieve the metadata using a WS-Transfer GET request or an HTTP/GET request using the ?wsdl
query string. To be sure that the code is working you must create a basic WCF service. A basic self-hosted service is provided in the following code.
[!code-csharphtPublishMetadataCode#0] [!code-vbhtPublishMetadataCode#0]
-
Within the main method of a console application, instantiate a xref:System.ServiceModel.ServiceHost object by passing in the service type and the base address.
[!code-csharphtPublishMetadataCode#1] [!code-vbhtPublishMetadataCode#1]
-
Create a try block immediately below the code for step 1, this catches any exceptions that get thrown while the service is running.
[!code-csharphtPublishMetadataCode#2] [!code-vbhtPublishMetadataCode#2]
[!code-csharphtPublishMetadataCode#3] [!code-vbhtPublishMetadataCode#3]
-
Check to see whether the service host already contains a xref:System.ServiceModel.Description.ServiceMetadataBehavior, if not, create a new xref:System.ServiceModel.Description.ServiceMetadataBehavior instance.
[!code-csharphtPublishMetadataCode#4] [!code-vbhtPublishMetadataCode#4]
-
Set the xref:System.ServiceModel.Description.ServiceMetadataBehavior.HttpGetEnabled%2A property to
true.
[!code-csharphtPublishMetadataCode#5] [!code-vbhtPublishMetadataCode#5]
-
The xref:System.ServiceModel.Description.ServiceMetadataBehavior contains a xref:System.ServiceModel.Description.MetadataExporter property. The xref:System.ServiceModel.Description.MetadataExporter contains a xref:System.ServiceModel.Description.MetadataExporter.PolicyVersion%2A property. Set the value of the xref:System.ServiceModel.Description.MetadataExporter.PolicyVersion%2A property to xref:System.ServiceModel.Description.PolicyVersion.Policy15%2A. The xref:System.ServiceModel.Description.MetadataExporter.PolicyVersion%2A property can also be set to xref:System.ServiceModel.Description.PolicyVersion.Policy12%2A. When set to xref:System.ServiceModel.Description.PolicyVersion.Policy15%2A the metadata exporter generates policy information with the metadata that" conforms to WS-Policy 1.5. When set to xref:System.ServiceModel.Description.PolicyVersion.Policy12%2A the metadata exporter generates policy information that conforms to WS-Policy 1.2.
[!code-csharphtPublishMetadataCode#6] [!code-vbhtPublishMetadataCode#6]
-
Add the xref:System.ServiceModel.Description.ServiceMetadataBehavior instance to the service host's behaviors collection.
[!code-csharphtPublishMetadataCode#7] [!code-vbhtPublishMetadataCode#7]
-
Add the metadata exchange endpoint to the service host.
[!code-csharphtPublishMetadataCode#8] [!code-vbhtPublishMetadataCode#8]
-
Add an application endpoint to the service host.
[!code-csharphtPublishMetadataCode#9] [!code-vbhtPublishMetadataCode#9]
[!NOTE] If you do not add any endpoints to the service, the runtime adds default endpoints for you. In this example, because the service has a xref:System.ServiceModel.Description.ServiceMetadataBehavior set to
true
, the service has publishing metadata enabled. For more information about default endpoints, see Simplified Configuration and Simplified Configuration for WCF Services. -
Open the service host and wait for incoming calls. When the user presses ENTER, close the service host.
[!code-csharphtPublishMetadataCode#10] [!code-vbhtPublishMetadataCode#10]
-
Build and run the console application.
-
Browse to the base address of the service (
http://localhost:8001/MetadataSample
in this sample) and verify that the metadata publishing is turned on. You should see a Web page displayed that says "Simple Service" at the top and immediately below "You have created a service." If not, a message at the top of the resulting page displays: "Metadata publishing for this service is currently disabled."
The following code example shows the implementation of a basic WCF service that publishes metadata for the service in code.
[!code-csharphtPublishMetadataCode#11] [!code-vbhtPublishMetadataCode#11]