How to invoke the Metadata Resolver and resolve metadata without using Visual Studio Plug-Ins?

Scenario: You want to get an adapter generated WSDL / XML Schema for a subset of operations.

The following code snippets show how to retrieve a ServiceDescription (WSDL) for a given set of operation identifiers.

Method 1 – Using WCF provided MetadataExchangeClient

Ensure you have the metadata exchange client endpoint defined in the WCF configuration.

<system.serviceModel>

<client>

<endpoint binding="sampleAdapterBinding" contract="IMetadataExchange" name="sample" />

</client>

</system.serviceModel>

Create a sample console application and use MetadataExchangeClient to retrieve metadata.

class Program

{

static void Main(string[] args)

{

// Get the WSDL one operation at a time

string op1 = "{OperationIdentifier1}";

string op2 = "{OperationIdentifier2}";

// Retrieve WSDL using MEX

EndpointAddress mexAddress = new EndpointAddress("sample://localhost:10000/DB2OLEDB/MyDB?wsdl" + "&op=" + op1 + "&op=" + op2);

// Create a metadata exchange client to retrieve metadata as per WS-MEX standard

MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);

// Obtain the metadata

MetadataSet metadataSet = mexClient.GetMetadata(mexAddress);

// Retrieve the first service description found

System.Web.Services.Description.ServiceDescription wsdl =

(System.Web.Services.Description.ServiceDescription) metadataSet.MetadataSections[0].Metadata;

// Write the WSDL to a file

String fileName = @"c:\temp\sample.wsdl";

Console.WriteLine("Writing WSDL to " + fileName);

wsdl.Write(fileName);

}

}

Method 2 – Using Adapter SDK provided MetadataRetrievalClient

WCF LOB Adapter SDK provides a WCF pre-defined proxy class called MetadataRetrievalClient (that implements interface IMetadataRetrievalContract). This class has three key methods –

· public MetadataRetrievalNode[] Browse(string nodeId, int childStartIndex, int maxChildNodes)

· public MetadataRetrievalNode[] Search(string nodeId, string searchCriteria, int maxChildNodes)

· public ServiceDescription GetMetadata(MetadataRetrievalNode[] nodes)

We will use the GetMetadata() method to retrieve the metadata as shown below.

class Program

{

static void Main(string[] args)

{

// create the binding

SampleAdapterBinding binding = new SampleAdapterBinding();

// create the endpoint address

EndpointAddress address = new EndpointAddress("sample://host:100/DB2OLEDB/MyDB?Protocol=TCPIP&Driver=Test");

// create metadata retrieval client

MetadataRetrievalClient metadataProxy = new MetadataRetrievalClient(binding, address);

// Show the nodes on the console

MetadataRetrievalNode op1 = new MetadataRetrievalNode();

op1.NodeId = "{OperationIdentifier1}";

op1.IsOperation = true;

MetadataRetrievalNode op2 = new MetadataRetrievalNode();

op2.NodeId = "{OperationIdentifier2}";

op2.IsOperation = true;

List<MetadataRetrievalNode> nodes = new List<MetadataRetrievalNode>();

nodes.Add(op1);

nodes.Add(op2);

// Get the WSDL

ServiceDescription wsdl = metadataProxy.GetMetadata(nodes.ToArray());

// Write the WSDL to a file

wsdl.Write(@"c:\temp\sampleadapter.wsdl");

// close the client

metadataProxy.Close();

}

}

These methods can come in quite handy while troubleshooting/debugging issues with the adapter metadata generation without using the Add Adapter Service Reference Visual Studio Plug-In/Consume Adapter Service BizTalk Project Add-In.