Service Types in WCF

In Windows Communication Foundation there are three types of services, ranging from simple to somewhat complex. The three types of services are as follows:

  • Typed
  • Untyped
  • Typed message

Typed

A typed service is the simplest of services, and is very similar to the methods in a class, in that both the parameters and method return values can be both simple or complex data types. A typed service can also return a value or return a modified parameter value. The following example illustrates a typed service in which typed operations are used:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IBookOrder
{
    [OperationContract]
    void PlaceOrder(string title, decimal cost);
}

This next example shows the use of the ref parameter to return a modified parameter value:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IBookOrder
{
    [OperationContract]
    void PlaceOrder(string title, decimal cost, ref ordernumber);
}

Data contracts provide the ability to extend this functionality and accept complex data types as parameters or returned results.
This is very similar to traditional object-oriented programming methodology, so this should not seem unfamiliar.

Untyped

Untyped services let you do your work at message level, so this is the most complex of the service types. At this level the service operation accepts and returns message objects so it is recommended that you use this type of service only when you need this level of control, that is, working directly with the message. The following example shows an operation contract passing a message object as an untyped service operation:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IBookOrder
{
    [OperationContract]
    void PlaceOrder(message IncomingMessage);
}

Typed Message

A typed message service accepts and returns message information via custom classes that are defined by message contracts. You learn about message contracts later in this chapter, but for now suffice it to say that this type of service type allows you to handle incoming requests and corresponding responses as messages, providing more structure to the data.
With a typed message service the operation accepts a custom message parameter in the form of a message class, and the responsibility of deciphering the message falls upon the service operation.
The following example shows a service contract along with a defined typed message that is used as a parameter in a typed service operation:

[ServiceContract]
public interface IBookOrder
{
    [OperationContract]
    void PlaceOrder(Contract MyContract);
}
[MessageContract]
public class MyContract
{
    [MessageHeader]
    string Title;
    [MessageBodyMember]
    decimal cost;
}
Tagged . Bookmark the permalink.

Leave a Reply