Search code examples
wcfmessage

WCF: Why to use System.ServiceModel.Channels.Message class?


I have a general question. Why sometimes should we use System.ServiceModel.Channels.Message class instead of some concrete class when we create WCF service?

For example:

1) We can use the following:

public Person GetPersonById(int id)
{
    Person person = Employees.CreateEmployees().First(e => e.Id == id);
    return person;
}

2) But we can use the following as well:

public Message GetPersonById(Message id)
{
    string firstName = Employees.CreateEmployees().First(e => e.Id == id.GetBody<int>()).FirstName;
    Message response = Message.CreateMessage(id.Version, ReplyAction, firstName);
    return response;
}

What's the difference? Will I have the same result in both cases?


Solution

  • Short answer - it is higher level of abstraction.
    For exampe if your service must undersend expandable list of message types without actualy changing its contract or imagine a load balancing WCF proxy.

    Also in message processing pipeline it is impossible to create generic mechanisms for handling messages without this kind of abstraction.