我正在编写一个 WCF WebMethod 来将文件上传到,其中我从网络上获取了片段。WCF 界面如下所示:
<ServiceContract()>
Public Interface ITransferService
<OperationContract()>
Sub UploadFile(ByVal request As RemoteFileInfo)
End Interface
<MessageContract()>
Public Class RemoteFileInfo
Implements IDisposable
<MessageHeader(MustUnderstand:=True)>
Public FileName As String
<MessageHeader(MustUnderstand:=True)>
Public Length As Long
<MessageBodyMember(Order:=1)>
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
在 ASP.NET 中,当使用 web 方法时,由于某种原因,它仅在将接口用作实例化的一部分时才有效RemoteFileInfo
:
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
If fu.HasFile Then
Dim fi As New System.IO.FileInfo(fu.PostedFile.FileName)
' this is the line in question --------------
Dim cu As ServiceReference1.ITransferService = New ServiceReference1.TransferServiceClient()
' -------------------------------------------
Dim uri As New ServiceReference1.RemoteFileInfo()
Using stream As New System.IO.FileStream(fu.PostedFile.FileName, IO.FileMode.Open, IO.FileAccess.Read)
uri.FileName = fu.FileName
uri.Length = fi.Length
uri.FileByteStream = stream
cu.UploadFile(uri)
End Using
End If
End Sub
谁能告诉为什么不能TransferService
使用以下方法创建一个实例:
Dim cu As New ServiceReference1.TransferServiceClient()
如果我尝试上述方法,它将打破这一行:
cu.UploadFile(uri)
...并且 UploadFile 必须使用三个参数(文件名、长度、文件字节流)调用,即使没有使用此签名的方法。
请问创建这个类的实例时为什么需要接口?