【问题标题】:WCF and data contract with templateWCF 和带有模板的数据契约
【发布时间】: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


    【解决方案1】:

    如果这个错误发生在.net和mono之间,那么很可能是由wcf mono bug引起的

    Mono 的 nettcpbinding 实现错误地编码了可变长度字段。这意味着,对于 .net 客户端,某些接口方法调用将起作用,而其他接口方法调用将在处理来自单声道的响应时抛出“以下元素未关闭:信封”错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2011-04-09
      相关资源
      最近更新 更多