WCF和数据与模板签订合同

时间:2016-06-14 13:39:55

标签: wcf datacontract

我刚刚开始使用WCF并面临一个我无法处理的问题。我有一些代码如下。

数据

[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}";
}

和服务

[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;
    }
}

当我尝试在客户端调用GetPoints时,我得到一个异常

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)

这似乎是某种序列化问题,我试图增加net tcp绑定缓冲区(MaxBufferSize,MaxReceivedMessageSize),但它没有帮助。有什么想法如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果.net和mono之间发生此错误,则可能是由此wcf mono bug

引起的

Mono的nettcpbinding实现是/错误地编码可变长度字段。这意味着,对于.net客户端,一些接口方法调用将起作用,而其他人将抛出&#34;以下元素未关闭:Envelope&#34;处理来自mono的响应时出错。