WS-Duck for Windows Communication Foundation
Last week I blogged about Arjen's WS-Duck Typing and how Spring-WS enables "duck typed" web services. Its apparent that I have been spending too much time with either Java or "older" .NET technologies, because Arjen was the one who pointed out that duck typing is an intrinsic part of WCF.
The easiest way to expose a duck typed WCF endpoint is to define a service contract with a single operation contract that matches a wildcard action.
[ServiceContract]
public interface IDuckService
{
[OperationContract(Action = "*", ReplyAction ="*")]
Message Process(Message request);
}
This contract defines a generic request-reply contract that will accept any message as input.
You'll also need to provide an implementation of the duck typed contract.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class OrderServiceEndpoint : IDuckService
{
private IOrderService orderService;
public IOrderService OrderService
{
get {return orderService; }
set { orderService =value; }
}
public Message Process(Message request) {
XmlDocument body =new XmlDocument ();
body.Load(request.GetReaderAtBodyContents());
string customerId =
body.SelectSingleNode("/*/*[local-name()='Id']").InnerText;
IList<Order> orders =
orderService.GetOrdersForCustomer(customerId);
Message response =
Message.CreateMessage(MessageVersion.Soap11,
string.Concat(request.Headers.Action,"Response"),
orders);
return response;
}
}
This example shows a C# implementation of the Spring-WS order service from my previous post. It makes use of an XPath expression to extract the customer ID from the XML message, rather than deserializing the message to a strongly typed object model. The orders are retrieved from an externally configured order domain service and the resulting list of orders is marshaled to a response message using the "out-of-the-box" XML encoding features in WCF.
Unless you see yourself stuck with the good old ASMX web services for the foreseeable future, there is no need for a Spring.NET-WS...