OK, looks I like long blog post titles these days...
There is a nice feature implemented in WCF which I want to call dynamic endpoint negotiation.
This feature uses WS-MetadataExchange under the hood and is quite easy to implement on the consumer side.
You may want to use this feature e.g. if you do not want to rely on WCF endpoint configurations sitting either in your code or in a configuration file. For this you can walk up to a service's base address which by default exposes a ?wsdl and /mex address to retrieve metadata (yes, both can be disabled with a behavior setting).
So the following line gets the metadata from a service and gives us back a list of all endpoints - 'all' means all, i.e. also the endpoints that we might not be interested in as we usually want to see just the endpoints which implement a certain contract. More on this later.
string mexAddress = ConfigurationManager.AppSettings["MexAddress"].ToString();
ServiceEndpointCollection endpoints =
MetadataResolver.Resolve(new EndpointAddress(mexAddress));
In addition, with the following code snippet you can first 'download' the metadata and then decide on a contract base what to do. Then you just have to decide which endpoint (and all of those implement your given contract because you asked for just that) to actually use and talk to and off you go - very nice (the sample snippet below just holds the endpoints in a combobox for demo reasons).
And yes, this means a perf hit in the first place - but there are scenarios where you just don't care.
string mexAddress = ConfigurationManager.AppSettings["MexAddress"].ToString();
ServiceEndpointCollection endpoints =
MetadataResolver.Resolve(new EndpointAddress(mexAddress));
if (endpoints != null)
{
foreach (ServiceEndpoint ep in endpoints)
{
if(ep.Contract.Name.Equals(typeof(IDocumentManager).Name))
cbEndpoints.Items.Add(ep.Name); // cbEndpoints is a combobox in this sample
}
}
Well, the code is not too nice, a bit clumsy, costly loop, right? There is another way to reach our goal but this other code is really ugly and not worth to be presented right now :)
After talking to the Indigo team I have a good feeling that we can have a more polished API for v1.
BTW: Neither the MetadataResolver nor the MetadataTransferClient (which is used internally by MetadataResolver) support the DISCO protocol. To retrieve metadata using DISCO you have to use the System.Web.Services.Protocols.DiscoveryClientProtocol type.
Feel free to download a small demo application for WinFX February CTP that demonstrates dynamic endpoint negotiation.
Interesting stuff, Christian. A more interesting question, to me is: What kind of scenarios would this be useful for, and where would it be preffered to static endpoint configuration? I have a few ideas but I'd certainly like to hear your opinion :)
Posted by: Tomas Restrepo | 04/07/2006 at 02:16 AM