I would like to start a service (with Mex enabled with mex TCP Binding) on port 0 - say for contract "IHelloWorldOne" on a end point using net TCP binding again.
From my service's App.config
<service name="Service.One.HelloWorldOne">
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" name="Mex" kind="mexEndpoint" listenUriMode="Unique" />
<endpoint name="Discovery" kind="udpDiscoveryEndpoint" />
<endpoint address="1/tcp" binding="netTcpBinding" name="Service.One" contract="Service.One.IHelloWorldOne" listenUriMode="Unique" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:0/service" />
</baseAddresses>
</host>
</service>
Note that, port is 0 - this basically lets windows give me an open port for the end point and this would mean that "mex" endpoint could get a random port.
On my client:
FindCriteria findCriteria = FindCriteria.CreateMetadataExchangeEndpointCriteria(typeof(IHelloWorldOne));
//FindCriteria findCriteria = new FindCriteria(typeof(IHelloWorldOne));
findCriteria.MaxResults = 1;
findCriteria.Duration = TimeSpan.FromSeconds(5);
findCriteria.Scopes.Add(new Uri(string.Format("net.tcp://sharedservice/{0}/", "SHAREDCONTEXT")));
FindResponse response = discoveryClient.Find(findCriteria);
try
{
if (response.Endpoints.Count > 0)
{
var mexClient = new MetadataExchangeClient(MetadataExchangeBindings.CreateMexTcpBinding());
var contracts = new List<ContractDescription>() { ContractDescription.GetContract(typeof(IHelloWorldOne)) };
EndpointAddress address = new EndpointAddress(response.Endpoints[0].ListenUris[0]);
var endpoints = MetadataResolver.Resolve(contracts, address, mexClient);
Note: I am using the ListenURI to get the metadata exchanged. However, when I try that - I run into this exception
Exception
System.InvalidOperationException: Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:50294/service/mex'. --->
System.InvalidOperationException: <?xml version="1.0" encoding="utf-16"?><Fault xmlns="http://www.w3.org/2003/05/soap-envelope"><Code><Value>Sender</Value><Subcode><Value xmlns:a="http://www.w3.org/2005/08/addressing">a:DestinationUnreachable</Value></Subcode></Code><Reason><Text xml:lang="en-US">
The message with To 'net.tcp://localhost:50294/service/mex' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher.
Check that the sender and receiver's EndpointAddresses agree.</Text></Reason></Fault>
I am able to see the port for mex endpoint on the listenURI but unable to connect to it.
The other approach I tried was to leave the mex end point as a constant and discover the service port - I was not able to get this done as well as the port is not available on the listenURI of the endpoint from the response.
So my question is, is there a way to bind to port 0 in WCF for Mex and service end points and also discover them? If so, how?