Creating a Service on "any free port"

By default, when creating a ServiceHost we require you to specify a base address (e.g. “http://kennyw.com/myService/”). This is because your clients needs to know where to send messages to. Sometimes you have a situation where you don’t need to know the exact address up front (let’s say you are using WS-Discovery or some other registry-based lookup). In that case, you want to indicate to the transport to “use any free port” (or “port 0” in sockets-speak). Fortunately, Indigo has this functionality baked into our addressing parameters.

To signal you want this behavior, set ServiceEndpoint.ListenUriMode to ListenUriMode.Unique (the default is ListenUriMode.Explicit). At build time this will propagate to BindingContext.ListenUriMode, which will then cause the Transport to “unique-ify” the ListenUriBaseAddress.

For exclusive TCP (where the process is directly listening on a socket) we will bind to a uniquely available port#. For shared TCP, Pipes, and HTTP we will append a GUID to the base address which will uniquely identify this endpoint.

4 thoughts on “Creating a Service on "any free port"

  1. Pingback: mattonsoftware.com : .NET Resources

  2. Ido Ran

    Finally, I have been looking for this feature since WSE2.
    I’m so glad you have post this message.

    Thank you,
    Ido.

    Reply
  3. Pingback: kennyw.com » Blog Archive » Building a “client-side” Listener

  4. Pingback: kennyw.com » Blog Archive » What IP Address(es) do we listen on?

Leave a Reply

Your email address will not be published. Required fields are marked *