How to use KnownType to include polymorphic return values ​​in a WCF service contract?

I am converting a remote interface to WCF, however I have a method that is declared as returning an “object” and can return several different types (basically different enumerations)

Where can I find an example to handle this?

(I use a generic contract assembly that contains all types, rather than generating a client proxy if that matters.)

+3
source share
5 answers

This is what I did at the end, but I really like to start using one of the solutions that Aaron Fisher gave in the long run.

/// <summary>
/// WCF can not cope with a method that returns an object so wrap the
/// untyped object in a typed object.  Then use KnowType to tell WCF 
/// about the typs that are wrapped
/// </summary>
[DataContract(), KnownType(typeof(MyType1)), KnownType(typeof(MyType2))]
public class UntypedObjectHolder
{
    [DataMember()]
    private object m_Value;

    public UntypedObjectHolder(object value)
    {
        m_Value = value;
    }

    public object Value
    {
        get { return m_Value; }
    }
}

Then in my interface

[OperationContract()]
UntypedObjectHolder GetValue(eGetValueType valueType);

, , , -

[ServiceContract]
[ServiceKnownType(typeof(PhotoCamera))]
[ServiceKnownType(typeof(TemperatureSensor))]
[ServiceKnownType(typeof(DeviceBase))]
public interface IHomeService
{    
   [OperationContract] IDevice GetInterface();
}
0

, KnownTypeAttribute. , , .

, , , , .

AFAIR, - WSDL/XSD, WCF. , - . .NET-, , .

, . System.Object, xs: any, , , .

WCF , , . , , , , .

+1

KnownTypeProvider silverlight NetDataContractSerializer, .

+1

, KnowType attr , ?

0

The February issue of MSDN Magazine contains " Known Types and a Common Resolver," which shows a good solution to this.

0
source

Source: https://habr.com/ru/post/1724382/


All Articles