Sunday, July 13, 2008

Multiple interfaces in a WCF contract

Well most of the time we have requirement of having one ServiceContract for an endpoint. But sometimes we get into a situation when the implementation is spread across multiple interfaces and we need to include the functionality spread into both the implementations into our WCF class. To suffice such a requirement we can use the concept of Interface Aggregation. Suppose we have two interfaces
  • ITemperature
  • IRainfall

The former returns the temperature of a particular location and the latter returns rainfall measure. Now suppose we have another interface called IWeather which will hold the functionality of returning weather forecast. The below code shows how to achieve this functionality using C#

namespace AggregatedContracts{
[ServiceContract]
public interface ITemperature
{
[OperationContract]
double GetTemperature(double latitude, double longitude);
}

[ServiceContract]
public interface IRainfall
{
[OperationContract]
double GetRainfall(double latitude, double longitude);
}

ServiceContract]
public interface IWeather : ITemperature,IRainfall{};
{
[OperationContract]
string GetWeatherForecast(double latitude, double longitude);
}

public class WeatherInfo : IWeather{
//implement ITemperature


//implement IRainfall
}
}






No comments: