我有一个非常基本的 WCF 服务来接收上传的文档。像这样定义时,它可以完美地工作:
<ServiceContract()>
Public Interface ITransferService
<OperationContract()>
<FaultContractAttribute(GetType(MyProcessingFault))>
Function UploadFile(ByVal request As RemoteFileInfo) As MyWebMethodResult
End Interface
<MessageContract()>
Public Class RemoteFileInfo
Implements IDisposable
<MessageHeader(MustUnderstand:=True)>
Public FileName As String
<MessageHeader(MustUnderstand:=True)>
Public Length As Long
<MessageBodyMember()>
Public FileByteStream As System.IO.Stream
Public Sub Dispose() Implements IDisposable.Dispose
If FileByteStream IsNot Nothing Then
FileByteStream.Close()
FileByteStream = Nothing
End If
End Sub
End Class
但是,我对使用 MessageContractAttribute 有点困惑,大多数作者建议使用 DataContract 来保持简单。然而,当我将上述内容更改为以下内容时,就像这样......
<ServiceContract()>
Public Interface ITransferService
<OperationContract()>
<FaultContractAttribute(GetType(MyProcessingFault))>
Function UploadFile(ByVal request As RemoteFileInfo) As MyWebMethodResult
End Interface
<DataContract()>
Public Class RemoteFileInfo
Implements IDisposable
<DataMember>
Public FileName As String
<DataMember>
Public Length As Long
<DataMember()>
Public FileByteStream As System.IO.Stream
Public Sub Dispose() Implements IDisposable.Dispose
If FileByteStream IsNot Nothing Then
FileByteStream.Close()
FileByteStream = Nothing
End If
End Sub
End Class
当我尝试上传到服务时,我收到以下 WCF 异常:
不应使用数据合同名称“FileStream:http://schemas.datacontract.org/2004/07/System.IO”键入“System.IO.FileStream”
研究这个,我读到流不能被序列化,但是,对于第一个例子来说,这肯定是不正确的吗?是否可以将 FileStream 与 DataContract 一起使用,或者我不应该尝试这种方法?