PV17

Hi.

I have a hard time figuring out how to resolve a problem I am faced with. So, here is the scenario:

Client:

1) IBase interface defined as follows:

Public Interface IBase

Property BaseName() As String

End Interface

2) IDerived defined as follows:

Public Interface IDerived

Inherits IBase

Property DerivedName() As IExtra

End Interface

3) IExtra defined as follows:

Public Interface IExtra

Property ExtraMethod() As Single

End Interface

4) Client have implemented IDerived in class named Derived.

5) Client wants to call a WCF service that I am to develop that would pass IDerived as parameter. For example Foo(ByVal obj as IDerived)

Now, since I am not very experienced with WCF, I doní»t know if and how am I to define DataContract(). Since IDerived has a IBase a sa a base interface and also has a property that returns IExtra, I am completely lost and I doní»t know for which interface do I need to create DataContract(). In addition, Derived is implementing IDerived, so it will be actually object of Derived that will be passed in, so I am not sure if I should create a data contract for Derived as well.

The last thing is that client has a DLL that defines all of those types (for the client purpose), and I am guessing that I doní»t need a reference to that DLL, right

Please help and thanks in advance.

P.



Re: Windows Communication Foundation (Indigo) Interfaces as parameters in WCF OperationContract - please help

Jarrod M.

I'm afraid this is not possible. Data is passed between services based on the schema of a type. With an interface, that's not possible. It needs to know what object to deserialize the data as/to. You can't do that if all you have is an interface.

Imagine a method: UpdateOrders(IList orders)

You're wanting to pass in a collection of orders...not just any collection but a collection that implements IList interface.

Now, how many classes in the .NET Framework implement IList How does the service know which one to deserialize to Its not in the information coming across - all thats there is that its a representation of IList...which is an interface, no underlying implementation code or description. There is no way to decorate your object and tell the DataContractSerializaer that you're passing this as an IList but when its deserialized it needs to be a SortedList or it needs to be a custom implementation of a linked list you wrote.

You need to work with abstract base classes and mark the concrete types as KnownType with the KnownType attribute.

You can also decorate the operation with KnownType (but only one on the operation).

Code Block

[KnownType(typeof(MyConcreteClass))]

[DataContract]

public abstract class MyBaseClass

{

[DataMember]

public virtual string MyName

{

get { return string.Empty; }

}

}

[DataContract]

public class MyConcreteClass:MyBaseClass

{

[DataMember]

public override string MyName

{

get { return "MyName"; }

}

}

You could then call a method of IMyService::UpdateMyClass(MyBaseClass class) as

UpdateMyClass(new MyConcreteClass());

Now, if you REALLY need this and don't care at all about interoperability...

NetDataContractSerializer will do what you want.

http://www.thoughtshapes.com/WCF/UsingInterfacesAsParametersTwo.htm





Re: Windows Communication Foundation (Indigo) Interfaces as parameters in WCF OperationContract - please help

ericrtodd

Out of the box, WCF does not support sending/receiving interface values. If you are working in a homogenous environment (.NET to .NET), however, you can make use of the NetDataContractSerializer. While it is also an XML based serializer, it serializes the fields of an object (shared type vs. shared contract).

Aaron Skonnard gives an excellent example of implementing the use of the alternate serializer. Some concern, however, was raised as to when the best time to replace the serializer operation behavior.





Re: Windows Communication Foundation (Indigo) Interfaces as parameters in WCF OperationContract - please help

PV17

Thanks for your reply.