ListenUriMode.Unique
It basically solves the challenge of generating a "unique" address for an endpoint when you don't know what's already in use. Here's how it works:
<system.serviceModel> <services> <service name="WcfTests.MathService"> <endpoint listenUriMode="Unique" address="net.tcp://localhost/math" binding="netTcpBinding" contract="WcfTests.MathService"/> </service> </services> </system.serviceModel> |
Using this configuration with TCP endpoints causes WCF to automatically choose a "free" port number -- it also appends a unique GUID to the end of the address to ensure uniqueness. For HTTP and named pipes it only appends the GUID to the address, it doesn't generate a unique port number.
When I run this configuration, the listener address for this endpoint is:
net.tcp://localhost:39619/math/908ae8dc-7427-407a-9ea4-8aeecdfa62be
When the value of the ListenUriMode is set to Unique the transport is responsible for creating a unique URI. Different transports used by Windows Communication Foundation (WCF) generate this unique URI differently:
- For TCP in exclusive mode (PortSharingEnabled is false) this means binding to a uniquely available port number.
- For TCP in port sharing mode (PortSharingEnabled is true) and for all of the other existing WCF transports, this means appending a unique path (a GUID) to the end of the ListenUri.
When using ListenUri.Unique the discovery capabilities of WCF 4.0 are a perfect match.
On the server side the ListenURI will be set automatically to be unique and the client will find about the chosen ListenURI using WS discovery.
After the client had learned about the unique URI from the discovery information
(var viaUri = discoveredEndpoint.ListenUris[0]) all it has to do is to point the proxy to the right spot using : clientProxy.Endpoint.Behaviors.Add(new ClientViaBehavior(viaUri)
Simple and clean.