【发布时间】:2016-07-09 01:00:33
【问题描述】:
下面的结构是可序列化的,当服务器(运行 WCF Web 服务)返回这个对象时,客户端可以正确接收数据。
[Serializable]
public struct TestInfo
{
public string TestStr;
public int TestInt;
}
场景:服务器试图打开端点并将其发送给客户端,以便客户端将文件写入其中。我尝试在下面的结构中添加“Stream”,但它会引发错误。
[Serializable]
public struct TestInfo
{
public string TestStr;
public int TestInt;
public Stream TestStream;
}
错误:
An error occurred while receiving the HTTP response.
This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).
System.Net.WebException: The underlying connection was closed:
An unexpected error occurred on a receive. --->
System.IO.IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
知道我是否在这里遗漏了什么? 'Stream' 是可序列化的,我认为发送 Stream 会很好。如果“流”不是最好的方法,那么我可以尝试其他任何方法吗?
【问题讨论】:
-
我不知道为什么 Stream 被标记为可序列化,但实际上它的实际实现都不能被有意义地序列化。真的没有办法以一种在本地盒子之外有用的方式序列化本地文件访问 (
FileStream) 或本地网络连接 (NetworkStream/SslStream) 之类的东西。
标签: c# http serialization soap stream