9

I have a simple wcf Service Contract that works fine if I use the concrete implementations for the interfaces, but if I want to use the interfaces in the paramaters instead of the concrete classes, it gives me the error which I will display below.

Here is code:

[ServiceContract]
public interface IClientUserRegistration
{
    [OperationContract]
    void RegisterClientUser(ClientUser clientUser);

    [OperationContract]
    List<ClientUser> GetUsers();
}

If I replace ClientUser with IClientUser, the WCF Test Client says that RegisterClientUser operation is not supported because it uses the type System.Object. If I replace the return value of GetUsers with List, it says that this operation is not supported because it uses the type System.Object[]. Why does it give these errors?

The reason why I was trying to use IClientUser is that I could implement different user types that implement the IClientUser interface and pass them into RegisterClient, but if I am only able to pass ClientUser, then I have to create a bunch of RegisterClient overrides that take different types of Users.

Maslow
  • 17,665
  • 17
  • 96
  • 181
Xaisoft
  • 42,877
  • 83
  • 270
  • 415

2 Answers2

13

SOAP has no concept of interfaces. That would tend to make deserialization difficult.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
  • If it is a REST based service, can I use interfaces or is it the same. I'm new to this so forgive me. Regarding my the other part of my question since you mentioned that SOAP has no concept of interfaces. If I wanted to register different clients, would I have to override the RegisterClientUser method, for example, RegisterClientUser(SuperUser superUser); – Xaisoft Sep 15 '10 at 18:20
  • 3
    Yeah, it's the same regardless. Imagine trying to make an XML representation of ALL the values within an object, but you only know a small portion of the keys. You wouldn't be able to complete your task. WCF looks at it the same way. – Nathan Wheeler Sep 15 '10 at 18:25
  • 4
    Spot on - WCF calls are **NOT** .NET function calls - they are serialized messages being passed from client to server (and back) and interfaces don't lend themselves to serialization.... – marc_s Sep 15 '10 at 18:26
  • @md5sum - What do you mean by "know a small portion of the keys." – Xaisoft Sep 15 '10 at 18:30
  • 3
    @Xaisoft - An interface only contains (or rather POSSIBLY only contains) a portion of the values for the whole object. There could be several interfaces implemented by a single class (Like IMyInterface, IDisposible, IEnumerable), but if you only pass IMyInterface, you're only providing a small portion of the object as a whole for deserialization. – Nathan Wheeler Sep 15 '10 at 18:34
12

This is because the objects have to be serialized for passing between the client and the server. You are not allowed to pass an "object" type. All types must be concrete so that they can be properly serialized and deserialized. An Interface is nothing more than an "object" type with a sub-type of the Interface. The object as a whole cannot be deserialized and serialized this way, only the interface members can be. This would make for a really messy implementation on both sides.

Nathan Wheeler
  • 5,726
  • 2
  • 26
  • 47