Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Saturday, May 21, 2011

Speeding up WCF Discovery

With the version 4.0 of the .NET framework Microsoft has enriched WCF with an implementation of the WS-Discovery protocol. Discovery allows WCF services to announce their availability and client applications to find these services at runtime without knowing the endpoint addresses. The WCF Discovery supports two modes the managed and the ad-hoc mode. In the managed mode there is proxy server that manages the information about available services. With the ad-hoc mode you don't even need a centralized server! In this mode services and client applications use multicast messages to announce their availability or to query available services. For more information about WCF discovery see WCF Discovery Overview on MSDN. This site also provides some usage examples.

In my opinion the second discovery mode (ad-hoc) is more interesting for loosely coupled distributed scenarios because you don't have to bother about a central point of failure (the discovery proxy). But playing around with WCF discovery I noticed that it takes some time on the client to discover suitable services. It's not really a big problem because you would usually do it only once e.g. during the application startup and cache the proxy or the endpoint address somehow. But if you only need only one service instance with a certain contract and you don't care which instance it is (if there are many of them) you can considerably reduce the time needed to discover the service address. You just tell the framework that you only need one result. To do so you have to setup the FindCriteria accordingly:
// Create DiscoveryClient
var discoveryClient = new DiscoveryClient(
                            new UdpDiscoveryEndpoint());
var criteria = new FindCriteria(typeof(IContract));
criteria.MaxResults = 1; // we only need one instance!
var findResponse = discoveryClient.Find(criteria);
If there is a service instance wich implements IWantedService the Find()-call would return much sooner than without adjusting the MaxResults property.

Monday, February 7, 2011

Dynamic base address for a WCF service

Last week I've learned a nice 'trick' that allows to specify a dynamic base address for a WCF service in the configuration file. When you want to run a WCF service you need to specify the base address for the service host. You can do this in a config file or directly in your code when creating a ServiceHost instance: http://msdn.microsoft.com/en-us/library/ms733749.aspx.
Now if you are deploying your service to many machines you must either adjust the host name for each machine during/after the installation or you set the host name to 'localhost'. The problem with the last option is that if you are also exposing metadata then the MEX client will get some endpoint addresses with 'localhost' as host name and will try to call this service on its local machine unless you adjust the host name while creating a proxy.
A handy solution for this problem is to use a so called 'weak wildcard' for the host name part of the base address:
<system.serviceModel>
  <services>
    <host>
      <baseAddresses>
        <add baseaddress="net.tcp://*:1234/svc" />
      </baseAddresses>
    </host>
  </services>
</system.serviceModel>
That's it. The framework will replace the wild card with the name of the host the service is actually running on.
Edit: The solution works only with base-addresses not with endpoint addresses!