Why can't my operation contracts in my wcf client take interfaces as parameters?
Asked Answered
C

2

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.

Cerium answered 15/9, 2010 at 18:13 Comment(0)
P
13

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

Pacian answered 15/9, 2010 at 18:15 Comment(5)
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);Cerium
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.Wolenik
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....Bangs
@md5sum - What do you mean by "know a small portion of the keys."Cerium
@Cerium - 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.Wolenik
W
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.

Wolenik answered 15/9, 2010 at 18:18 Comment(1)
Thanks would you mind taking a look at my comment to John Saunders.Cerium

© 2022 - 2024 — McMap. All rights reserved.