简体   繁体   中英

How can I get the listening address/port of a WCF service?

I have a WCF service listening on a dynamic allocated port in windows Service Registry. How can i find the listening address of this service from another c# application? Or at least the port of this service?

Thanks, Adriana

You can dump the actual "Listeners" from inside your service implementation after the ServiceHost is open, using the "ChannelDispatcher" property.

For example:

foreach (var channelDispatcher in serviceHost.ChannelDispatchers)
{
            Console.WriteLine(channelDispatcher.Listener.Uri);
}

A listener's URI will contain the TCP/IP port the service is listenting on. Note that this is of course only true for such bindings that are based on TCP/IP in the first place. Note also that obviously each service could have multiple listeners (or listener ports), hence the "ChannelDispatchers" property may return multiple listeners.

You may also want to look at / dump the value of the "State" property to make sure that the respective channel dispatcher is actuall "Open", ie listening.

Edit: You may also want to have look into enabling WMI for WCF . Though I never looked into it, it might reveal such information as well.

If you cannot modify the service's code, or don't want to, you need to resort to tools like "Process Explorer" or "netstat" (the later again assuming you're using some TCP/IP based binding for the service's endpoints). Use netstat's "-b" option to display the PID and executable name for each port. That will give you a hint to your service (executable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM