Question

I have project that uses wcf. Currently it uses NetDataContractSerializer. I want to migrate to protobuf-net. My service has folowing contract:

interface IRemotingServer
{
    [OperationContract]
    TypeConfig GetTypeConfig(string typename);

    [OperationContract]
    object ExecuteMethod(InstanceIdentifier instance, string methodName, object[] parameters);

    [OperationContract]
    object ExecuteGenericMethod(InstanceIdentifier instance, string methodName, object[] parameters, string[] genericTypes, string returnType);

    [OperationContract]
    object GetRemoteProperty(InstanceIdentifier instance, string propertyName);

    [OperationContract]
    void SetRemoteProperty(InstanceIdentifier instance, string propertyName, object value);      

    [OperationContract]
    ObjectDataPacket GetObject(InstanceIdentifier instance);

    [OperationContract]
    bool Connect();

    [OperationContract]
    bool Disconnect();
}

things goes fine, until i try call GetRemoteProperty method of my contract which return type is object. Using server traces i found, that server blows up with folowing message

There was an error while trying to serialize parameter http://www.mersoft.am/Remoting:GetRemotePropertyResult. The InnerException message was 'Type 'Mersoft.Remoting.InstanceIdentifier[]' with data contract name 'ArrayOfInstanceIdentifier:Mersoft.Remoting' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

So, how can i fix it?

Was it helpful?

Solution

Basically, returning object is going to be a huge problem for any serializer that isn't BinaryFormatter or NetDataContractSerializer; returning object just doesn't work and is inappropriate on a general purpose WCF boundary. protobuf-net, in common with most serializers (including DataContractSerializer, XmlSerializer, JSON.NET, etc) wants to know more than just object; simply, that is not enough information to deserialize in most scenarios.

As long as you return object, that just isn't going to work well with protobuf-net (or, as noted, most serializers). At the moment, ProtoOperationBehavior inherits from DataContractSerializerOperationBehavior; one possible option would be to write a version of this that defaults to NetDataContractSerializer rather than DataContractSerializer - this would mean that unknown types would be serialized by NetDataContractSerializer instead; of course, it still wouldn't use protobuf-net for object, because it can't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top