I was digging into WCF’s implementation of WS-Discovery today and was somewhat appalled by how long it took to discover a service from a client when both endpoints lived on the same machine. I setup tracing and message logging to dig into why things were taking so long. Inside the messages, I found this nugget in the WS-Discovery probe messages:
<s:Body> <Probe xmlns="http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01"> <d:Types
PT20S</Duration> </Probe> </s:Body>
I thought to myself, “That looks like a TimeSpan. I wonder how I can set it.” If you haven’t used WS-Discovery on the client in WCF, let me walk you through the basic few lines of code that get things set up. You need a DiscoveryClient which knows how to send the probe messages and extract endpoints from the results. When the DiscoveryClient sends out a request, it sends out the request asking for services that implement a specific type. In our case, we are looking for a type in the http://tempuri.org/ XML Namespace where the type is named ITest.You state what you are looking for using a FindCriteria object. The code looks like this:
var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); var criteria = new FindCriteria(typeof (ITest)); var findResponse = discoveryClient.Find(criteria);
Looking at the data which appeared in the Probe, I thought it looked at awful lot like FindCriteria since FindCriteria was the only thing I told about the type I wanted to talk to. I took a quick look at the FindCriteria object via IntelliSense and found a member called Duration. For grins, I set it to 100 milliseconds:
var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); var criteria = new FindCriteria(typeof (ITest)); criteria.Duration = TimeSpan.FromMilliseconds(100); var findResponse = discoveryClient.Find(criteria);
Suddenly, discovery ran faster. I also saw that the probe Duration was now set to PT0.1S.
So, what did I learn? I learned that, by default, Discovery on WCF will allow for 20 seconds to find all endpoints. If you know the endpoint is close and the collection of implementations is small, you can ratchet the discovery time down to something reasonable for your situation.