WCF Datacontract free serialization (3.5 SP1)

Has anyone really got this? There is no documentation on how to enable this function, and I get the absence of attribute exceptions, despite the presence of a 3.5 SP1 project.

+3
source share
6 answers

I found that it does not work with internal / private types, but at the same time my public type works fine. This means that anonymous types will not: (

Using a reflector, I found the ClassDataContract.IsNonAttributedTypeValidForSerialization (Type) method, which seems to make a decision. This is the last line that seems to be a killer, the type should be visible, so internal / private types are not allowed :(

internal static bool IsNonAttributedTypeValidForSerialization(Type type)
{
    if (type.IsArray)
    {
         return false;
    }
    if (type.IsEnum)
    {
        return false;
    }
    if (type.IsGenericParameter)
    {
        return false;
    }
    if (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type))
    {
        return false;
    }
    if (type.IsPointer)
    {
        return false;
    }
    if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false))
    {
        return false;
    }
    foreach (Type type2 in type.GetInterfaces())
    {
        if (CollectionDataContract.IsCollectionInterface(type2))
        {
            return false;
        }
    }
    if (type.IsSerializable)
    {
        return false;
    }
    if (Globals.TypeOfISerializable.IsAssignableFrom(type))
    {
        return false;
    }
    if (type.IsDefined(Globals.TypeOfDataContractAttribute, false))
    {
        return false;
    }
    if (type == Globals.TypeOfExtensionDataObject)
    {
        return false;
    }
    if (type.IsValueType)
    {
        return type.IsVisible;
    }
    return (type.IsVisible && (type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, Globals.EmptyTypeArray, null) != null));

}

+1
source

, ...

:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    CompositeType GetData(int value);

}


public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

:

public class Service1 : IService1
{
    public CompositeType GetData(int value)
    {
        return new CompositeType()
        {
            BoolValue = true,
            StringValue = value.ToString()
        };
    }

}
+1

WCF : , XML . ? , , , datacontact. , ?

0

, , .

0

Yes, this could be due to abstract classes and inheritance. Sometimes this can ruin serialization. In addition, it may be the visibility of classes and the class hierarchy, if everything is not publicly available.

0
source

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


All Articles