WCF delegates and methods

I have successfully developed a great file upload method using WCF. Now I would like to report on the progress for each unique file that is uploaded. In my boot method, I have the following code:

while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0) { outfile.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; if (this.ProgressChanged != null) this.ProgressChanged(this, new ProgressArgs(totalBytesRead, request.FileByteStream.Length)); } 

What uses deleget ProgressEventHandler as below:

 public delegate void ProgressEventHandler(object sender, ProgressArgs e); public event ProgressEventHandler ProgressChanged; 

I do not use delegates so much (but I'm trying to learn) and got this far following examples on the Internet. The ProgressArgs class was missing in the example, but I assume that the calculation is happening and returns? Something like this: return Convert.ToInt32((totalBytesRead * 100) / fileSize) ?

So my questions are:

1) Did I help to declare and raised the ProgressChanged event correctly , what should I do with ProgressArgs ?

2) How to report progress on the client side ? My WCF method call currently has a return type of "void":

upload.UploadFile (fileinfo, m_objFile.InputStream);

Do I need to run a concurrent JavaScript method that calls the WCF JSON method or something like that? A detailed explanation of how to view progress from a client will be seriously appreciated.

Thanks!

PS - I am using ASP.NET 2.0 / Framework 3.5 / C # / and now I am hosting.

+4
source share
1 answer

Wow, you are asking about many different layers in one question.

Callbacks, not events

You are trying to do something specific to .NET and are not directly tied to web service programming. You cannot use events / delegates in web services. Instead, you should define what is called a "callback contract." This is another interface on which there are methods that represent the notifications you want to inform the caller and apply to your service contract as follows:

 public interface MyCallbackContract { void ProgressChanged(float percentComplete); } [ServiceContract(CallbackContract = typeof(MyCallbackContract)] public interface MyServiceContract { void UploadFile(Stream stream); } 

Callbacks now require two-way (duplex) communication, so if you just use basicHttpBinding now, you will need to switch to using wsDualHttpBinding or not binding to the http address at all, which will interop this service out of the water. Assuming your service is completely intranet, this should not be showstopper.

Learn a lot more about the callback programming model, which I will not go into. Instead, I will give you the amazing MSDN article by Yuval Lowy that details this issue.

How does this help me in the browser?

The second part of your question is how you can get this information from the browser, and, firstly, we must admit that there is definitely no way for it to communicate directly with the service provided. Instead, you will need to create an AJAX bridge service. Since the browser here is pure HTTP, you must upload files using POST to the REST-based WCF service. If you want to simultaneously receive status about this download, you will need to make polling requests in order to receive status.

Honestly, this is one of those places where the browser still just sucks like a platform. The browser knows how much data it sent, you should be able to report progress only on the basis of this, but it is not displayed anywhere. That's why people are turning to the Flash-based download utility because Flash really reveals this data, and so you don’t need to poll the server to make progress.

+6
source

All Articles