Freigeben über


WCF December CTP: Updated MetadataResolver with new proxies for each endpoint.

The following code fixes the earlier code that I leave posted just to confuse people. (Actually, so that you can see the difference between them.) Here I use a new factory or ClientBase proxy each time I change the endpoint the client will invoke. The proxy is fixed once used; the factory can be modified and return a new proxy to a new destination:

using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Design;
using System.ServiceModel.Channels;

public class Client
{
public static void Main()
{
try
{
SampleServiceProxy proxy;
// Client has an endpoint for metadata
EndpointAddress mexAddress
= new EndpointAddress(new Uri("https://localhost:8080/ServiceMetadata/mex"));
EndpointAddress httpGetAddress
= new EndpointAddress(new Uri("https://localhost:8080/ServiceMetadata?wsdl"));

      // Section 1.
// Get the endpoints for such a service
MetadataResolver resolver = new MetadataResolver(mexAddress);
ServiceEndpointCollection endpoints = resolver.RetrieveEndpoints();
ServiceEndpoint newEndpoint = null;
Console.WriteLine("Trying all available WS-MEX endpoints...");

      foreach (ServiceEndpoint point in endpoints)
{
if (point != null)
{
// Create a new proxy using retrieved endpoints.
using (proxy = new SampleServiceProxy(point.Binding, point.Address))
{
Console.WriteLine(
proxy.SampleMethod("Client used the "
+ point.Address.ToString()
+ " address.")
);
}
}
}
// Section 2.
// Get the endpoints for such a service using Http/Get request
resolver = new MetadataResolver(httpGetAddress);
endpoints = resolver.RetrieveEndpointsUsingHttpGet();
newEndpoint = null;
ISampleService serviceChannel;
Console.WriteLine(
"\r\nTrying all endpoints from HTTP/Get and with direct service channels...");
foreach (ServiceEndpoint point in endpoints)
{
if (point != null)
{
newEndpoint = point;
ChannelFactory<ISampleService> factory = new ChannelFactory<ISampleService>(
newEndpoint.Binding,
newEndpoint.Address
);

          serviceChannel = factory.CreateChannel();
Console.WriteLine(
serviceChannel.SampleMethod("Client used the "
+ point.Address.ToString()
+ " address.")
);
factory.Close();
}
}
// Section 3.
// Get other information.

      Console.WriteLine("URI of the metadata documents retreived:");
Collection<MetadataDocument> otherDocs = resolver.ResolvedMetadata;
foreach (MetadataDocument doc in otherDocs)
Console.WriteLine(doc.Dialect + " : " + doc.Identifier);

      Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
}
catch (UnknownFaultException unkException)
{
Console.WriteLine(unkException.Message);
Console.ReadLine();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
}
}
}