Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF and data contract with template

I've just started to work with WCF and faced with a problem which I cannot handle. I have some code like below.

Data

[DataContract]
public class DataValue<T>
{
    [DataMember] public string Name        { get; set; }
    [DataMember] public T Value            { get; set; }
}

[DataContract]
public class Point : DataValue<float>
{
    public override string ToString()   => $"{Name}\t\t{Value:F2}";
}

and Service

[ServiceContract(SessionMode=SessionMode.Required, CallbackContract = typeof(IServiceCallback))]
[ServiceKnownType(typeof(Point))]
public interface IService
{
    [OperationContract]
    Point[] GetPoints();
}

public sealed class Service : IService
{
    public Point[] GetPoints()
    {
        var p = new [] { new Point { Name = "point_1", Value = 1.1F } };
        return p;
    }
}

When I try to call GetPoints on client side I get an exception

Unhandled Exception: System.ServiceModel.CommunicationException: Error in deserializing body of reply message for operation 'GetPoints'. Unexpected end of file.
 Following elements are not closed: Envelope. ---> System.Xml.XmlException: Unexpected end of file. Following elements are not closed: Envelope.
   at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
   at System.Xml.XmlExceptionHelper.ThrowUnexpectedEndOfFile(XmlDictionaryReader reader)
   at System.Xml.XmlBaseReader.MoveToEndOfFile()
   at System.Xml.XmlBinaryReader.ReadNode()
   at System.Xml.XmlBinaryReader.Read()
   at System.Xml.XmlBaseReader.ReadEndElement()
   at System.ServiceModel.Channels.Message.ReadFromBodyContentsToEnd(XmlDictionaryReader reader, EnvelopeVersion envelopeVersion)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   --- End of inner exception stack trace ---

Server stack trace:
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc&rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

It seems like some kind of serialization problem, I tried to increase net tcp binding buffers (MaxBufferSize, MaxReceivedMessageSize), but it does not help. Are there any ideas how to fix this?

like image 527
wince Avatar asked Nov 20 '25 19:11

wince


1 Answers

If this error occurs between a .net and mono, then its likely caused by this wcf mono bug

Mono's nettcpbinding implementation was/is wrongly encoding a variable length field. Meaning, that for a .net client, some interface methods calls will work, while others will throw the "Following elements are not closed: Envelope" error, while processing the response from mono.

like image 120
Tom Avatar answered Nov 22 '25 16:11

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!