开发者

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

开发者 https://www.devze.com 2022-12-19 01:21 出处:网络
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 s

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, Ad开发者_Python百科riana


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", i.e. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消