How to: Publish Metadata for a WCF Service.(What is the Metadata Exchange Endpoint purpose.)
MSDN Note:
Publishing Service Metadata
Windows Communication Foundation (WCF) services publish metadata by publishing one or more metadata endpoints. Publishing service metadata makes the metadata available using standardized protocols, such as WS-MetadataExchange (MEX) and HTTP/GET requests. Metadata endpoints are similar to other service endpoints in that they have an address, a binding, and a contract, and they can be added to a service host through configuration or imperative code.
Publishing Metadata Endpoints
To publish metadata endpoints for a WCF service, you first must add the ServiceMetadataBehavior service behavior to the service. Adding a System.ServiceModel.Description.ServiceMetadataBehavior instance allows your service to expose metadata endpoints. Once you add the System.ServiceModel.Description.ServiceMetadataBehavior service behavior, you can then expose metadata endpoints that support the MEX protocol or that respond to HTTP/GET requests.
The System.ServiceModel.Description.ServiceMetadataBehavior uses a WsdlExporter to export metadata for all service endpoints in your service. For more information about exporting metadata from a service, see Exporting and Importing Metadata.
The System.ServiceModel.Description.ServiceMetadataBehavior adds a ServiceMetadataExtension instance as an extension to your service host. The System.ServiceModel.Description.ServiceMetadataExtension provides the implementation for the metadata publishing protocols. You can also use theSystem.ServiceModel.Description.ServiceMetadataExtension to get the service's metadata at runtime by accessing theSystem.ServiceModel.Description.ServiceMetadataExtension.Metadata property.
MEX Metadata Endpoints
To add metadata endpoints that use the MEX protocol, add service endpoints to your service host that use theIMetadataExchange service contract. WCF includes an IMetadataExchange interface with this service contract name that you can use as part of the WCF programming model. WS-MetadataExchange endpoints, or MEX endpoints, can use one of the four default bindings that the static factory methods expose on the MetadataExchangeBindings class to match the default bindings used by WCF tools such as Svcutil.exe. You can also configure MEX metadata endpoints using your own custom binding.
NOTE:
The Metadata Exchange Endpoint (MEX) is a special endpoint in WCF that exposes metadata used to describe a service.
the code looks like blow.
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Metadata.Samples
{
[ServiceContract]
public interface ISimpleService
{
[OperationContract]
string SimpleMethod(string msg);
}
class SimpleService : ISimpleService
{
public string SimpleMethod(string msg)
{
Console.WriteLine("The caller passed in " + msg);
return "Hello " + msg;
}
}
class Program
{
static void Main(string[] args)
{
ServiceHost svcHost = new ServiceHost(typeof(SimpleService), new Uri("http://localhost:8001/MetadataSample"));
try
{
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = svcHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
svcHost.Description.Behaviors.Add(smb);
// Add MEX endpoint
svcHost.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex"
);
// Add application endpoint
svcHost.AddServiceEndpoint(typeof(ISimpleService), new WSHttpBinding(), "");
// Open the service host to accept incoming calls
svcHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
svcHost.Close();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.Read();
}
}
}
}
or the config looks like below.
<system.serviceModel>
<services>
<service
name="Metadata.Example.SimpleService"
behaviorConfiguration="SimpleServiceBehavior">
<endpoint address=""
binding="wsHttpBinding"
contract="Metadata.Example.ISimpleService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SimpleServiceBehavior">
<serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
and the code with the config looks like :
{
ServiceHost host = new ServiceHost(typeof(SimpleService),
new Uri("http://localhost:8001/MetadataSample"));
try
{
// Open the service host to accept incoming calls
host.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
host.Close();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.Read();
}
}
HTTP GET Metadata Endpoints
To add a metadata endpoint to your service that responds to HTTP/GET requests, set the HttpGetEnabled property on the System.ServiceModel.Description.ServiceMetadataBehavior to true. You can also configure a metadata endpoint that uses HTTPS by setting the HttpsGetEnabled property on the System.ServiceModel.Description.ServiceMetadataBehaviorto true.
WCF services publish metadata by exposing one or more metadata endpoints. Publishing service metadata makes service metadata available using standardized protocols, such as MEX and HTTP/GET requests. Metadata endpoints are similar to other service endpoints in that they have an address, a binding, and a contract. You can add metadata endpoints to a service host in configuration or in code.
To publish metadata endpoints for a WCF service, you must first add an instance of the ServiceMetadataBehavior service behavior to the service. (otherwise when creating the mex endpoint. you will got a error message shows blow in light blue fonts. )Adding a System.ServiceModel.Description.ServiceMetadataBehavior instance to your service augments your service with the ability to publish metadata by exposing one or more metadata endpoints. Once you add the System.ServiceModel.Description.ServiceMetadataBehavior service behavior you can then expose metadata endpoints that support the MEX protocol or metadata endpoints that respond to HTTP/GET requests.
To add metadata endpoints that use the MEX protocol, add service endpoints to your service host that use the service contract named IMetadataExchange.WCF defines the IMetadataExchange interface that has this service contract name. WS-MetadataExchange endpoints, or MEX endpoints, can use one of the four default bindings exposed by the static factory methods on the MetadataExchangeBindings class to match the default bindings used by WCF tools, such as Svcutil.exe. You can also configure MEX metadata endpoints using a custom binding.
The ServiceMetadataBehavior uses a System.ServiceModel.Description.WsdlExporter to export metadata for all service endpoints in your service. For more information about exporting metadata from a service, see Exporting and Importing Metadata.
The ServiceMetadataBehavior augments your service host by adding a ServiceMetadataExtension instance as an extension to your service host. The System.ServiceModel.Description.ServiceMetadataExtension provides the implementation for the metadata publishing protocols. You can also use the System.ServiceModel.Description.ServiceMetadataExtension to get the service's metadata at runtime by accessing the Metadata property.
If you add a MEX endpoint in your application configuration file and then attempt to add the ServiceMetadataBehaviorto your service host in code you get the following exception:
System.InvalidOperationException: The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service Service1. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract. You can work around this issue by either adding the ServiceMetadataBehavior in the configuration file or adding both the endpoint and ServiceMetadataBehavior in code. For an example of adding ServiceMetadataBehavior in an application configuration file, see the Getting Started Sample. For an example of adding ServiceMetadataBehavior in code, see the Self-Host sample.
|
When publishing metadata for a service that exposes two different service contracts in which each contain an operation of the same name an exception is thrown. For example, if you have a service that exposes a service contract called ICarService that has an operation Get(Car c) and the same service exposes a service contract called IBookService that has an operation Get(Book b), an exception is thrown or an error message is displayed when generating the service's metadata. To work around this issue do one of the following:
|
see also
http://msdn.microsoft.com/en-us/library/ms734765.aspx
posted on 2012-06-24 17:17 malaikuangren 阅读(2164) 评论(0) 编辑 收藏 举报