Types of Services in WCF

Oct 29, 2010 Posted by Lara Kannan

There are three types of services in WCF, and they are the following:
  • Typed
  • Untyped
  • Typed message
Typed Service

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.

Typed Message Service

A typed message service accepts and returns message information via custom classes that are defined by message contracts. 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;

}

Untyped Service

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);

}

Thanks : codesubmit.com

Share

Post a Comment