【发布时间】:2016-06-14 13:39:55
【问题描述】:
我刚刚开始使用 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),但没有帮助。有什么想法可以解决这个问题吗?
【问题讨论】:
标签: wcf datacontract