【问题标题】:Passing 'Stream' to client throws error将“流”传递给客户端会引发错误
【发布时间】: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


【解决方案1】:

传输文件的另一种方法是将文件内容转换为字节并在数据合同中添加文件名数据成员。

[DataContract]
public struct TestInfo
{    
    [DataMember]
    public string FileName;   
    [DataMember]
    public byte [] FileStream;
}

在系统中写入文件

using(var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
    stream.CopyTo(fileStream);
}

注意:如果您注意到我将 Serializable 注释更改为 DataContract,因为它比 Serializable 有几个优点。想pass large data的也可以考虑这几个设置。

如果您真的想在管道中传递流,则网络中的几个链接可以输出其他链接。

例如。

【讨论】:

  • 我实际上不想将文件从客户端传输到服务器。我正在尝试在服务器上打开文件访问点并尝试将其发送给客户端。然后客户端应该将文件写入该文件访问点(而不是将文件传输到服务器)。我之前提到了第二个链接,这就是我决定传递流并获得上述错误的方式。我会尝试使用 [DataContract] 或 [MessageContract] 看看它是如何工作的。
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
相关资源
最近更新 更多