tags:

views:

86

answers:

2

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.

+4  A: 

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

John Saunders
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
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.
md5sum
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
@md5sum - What do you mean by "know a small portion of the keys."
Xaisoft
@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.
md5sum
+2  A: 

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.

md5sum
Thanks would you mind taking a look at my comment to John Saunders.
Xaisoft