MSDN > Home page del forum > Windows Communication Foundation > WCF - Upload a file to a service with a progress bar in the client
Formula una domandaFormula una domanda
 

Con rispostaWCF - Upload a file to a service with a progress bar in the client

  • martedì 7 agosto 2007 19.28Freddy Mercury Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Hi all,

     

    I'm currently experimenting WCF, i'm pretty new to this whole thing actually. I'm using VB.NET (VS2005). So here's the thing: I'm building a small test application using WCF to upload a file to a service. I created 2 ways of uploading my file, both work perfectly. I can send the file in chunks or stream it. I tried to add a progress bar so I could see the progress on the client side. Works perfect with the chunk upload, but I can't seem to get it to work with streaming. I tried using a callback, but it seems that I won't receive any callback before the whole process is done. I do know that's normal since I didnt use "IsOneWay:=True"but I do not want to use this attribute because I don't want the process to continue before the file is totally uploaded.

     

    Is there any way to send some feedback from my service to my client to monitor progress?

     

    Here are parts of my code:

     

    Service

     

    <ServiceContract(CallbackContract:=GetType(IServiceUploadFichierCallBack), SessionMode:=SessionMode.Required)> _

    Public Interface IServiceUploadFichier

    <OperationContract()> Sub UploaderFichierStream(ByVal pfStream As Stream)

    End Interface

     

    Public Interface IServiceUploadFichierCallBack

    <OperationContract(IsOneWay:=True)> Sub MAJFloodStream(ByVal pintStream As Integer)

    End Interface

     

     

    Upload function

     

    Public Sub UploaderFichierStream(ByVal pfStream As System.IO.Stream) Implements IServiceUploadFichier.UploaderFichierStream

    Dim outstream As FileStream = File.Open(mstrDestination, FileMode.Create, FileAccess.Write)

    Dim content() As Byte

    ReDim content(0)

    Const bufferLen As Integer = 4096

    Dim buffer(bufferLen) As Byte

    Dim count As Integer = 0

     

    Callback = OperationContext.Current.GetCallbackChannel(Of IServiceUploadFichierCallBack)()

    count = pfStream.Read(buffer, 0, bufferLen)

     

    While (count > 0)

    Try

         Console.Write(".")

         outstream.Write(buffer, 0, count)

         Callback.MAJFloodStream(count)

         count = pfStream.Read(buffer, 0, bufferLen)

    Catch ex As Exception

         Call MsgBox(ex.Message)

    End Try

    End While

    outstream.Close()

    pfStream.Close()

    End Sub

     

     

     

    This is what I use on the client side:

     

    Public Class CallbackHandler

    Implements ServiceUploadFichier.IServiceUploadFichierCallback

    Public Sub MAJFloodStream(ByVal pintStream As Integer) Implements ServiceUploadFichier.IServiceUploadFichierCallback.MAJFloodStream

    Form1.ProgressBar2.Value = Form1.ProgressBar2.Value + pintStream

    End Sub

    End Class

     

    Private Sub cmdShareStream_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShareStream.Click

     

    Dim Callback As InstanceContext = New InstanceContext(New CallbackHandler)

    Dim service As New ServiceUploadFichier.ServiceUploadFichierClient(Callback)

    Dim infoFichier As New ServiceUploadFichier.MessageInfoFichier

    Dim fstream As FileStream = New FileStream(txtSource.Text, FileMode.Open)

    Dim dblAvant As Double

    Dim dblApres As Double

     

    service.Open()

     

    Try

    dblAvant = Timer

    infoFichier.Destination = txtDestination.Text

    service.InitialiserFichier("", "", infoFichier.Destination)

    ProgressBar2.Value = 0

    ProgressBar2.Maximum = fstream.Length

    service.UploaderFichierStream(fstream)

    dblApres = Timer

    lblTempsStream.Text = (dblApres - dblAvant).ToString("0.00")

    Call MsgBox("Source: " & txtDestination.Text & vbCrLf & "Destination: " & txtAdresseSharepoint.Text)

    Catch ex As Exception

    Call MsgBox(ex.Message)

    End Try

     

    fstream.Close()

    service.Close()

    End Sub

     

     

    I think I posted the most important parts, don't hesitate to ask I more is needed to find the problem Smile

     

    Thanks in advance,

     

    Fred