Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Error trying to upload using WCF service
25 replies. Latest Post by microsoft_kc on August 4, 2008.
(0)
Jhorra
Member
522 points
475 Posts
07-02-2008 11:26 AM |
I get an exception when I try to upload a file that's more than a few mbs. When I look at the inner exception it says "Operation is not valid due to the current state of the object. Any idea what could becausing this?
This is my bind on the client I'm using:bind.MaxBufferSize = 2000000;bind.MaxReceivedMessageSize = 2000000;
And this is from the webconfig on the service:<httpRuntime maxRequestLength="2000000" executionTimeout="600000" />
sladapter
All-Star
17445 points
3,173 Posts
07-02-2008 11:35 AM |
HI, the default maxRequestLength setting in httpRuntime tag is 4096 which means 4MB. I don't know if 2000000 is even a valid number for that setting. Try some smaller number like from 8192 then see how you can bump that number up without error.
07-02-2008 11:37 AM |
It's valid, when I had it set to high originally it gave me and error with the limits for it. I need it to be able to handle at least 1Gb size limit, 2Gb would be better.
07-02-2008 11:38 AM |
What is your file size?
[Edit]
Never mind. I saw it in your last post.
07-02-2008 11:45 AM |
Right now it's choking on a 29mb file.
07-02-2008 11:51 AM |
Read this article. You might got session timeout.
http://www.websupergoo.com/helpupload50/source/2-tech_notes/3-web.config.htm
07-02-2008 12:05 PM |
Hi,
Your binding size is too small: you set 2000000 which is only 2M. If you want 2G, set it to 2147483647
<binding name="BasicHttpBinding_Upload" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> </binding>
07-02-2008 12:09 PM |
I had actually brought it down when I posted this, but originally, and right now it's set back to: bind.MaxBufferSize = 2000000000; bind.MaxReceivedMessageSize = 2000000000;
bind.MaxBufferSize = 2000000000;
bind.MaxReceivedMessageSize = 2000000000;
07-02-2008 12:20 PM |
I tried to upload a 33MB file without problem. Then I got the same error as you do when trying to upload a 88MB file.
SteveWong
Contributor
6343 points
1,281 Posts
07-02-2008 12:31 PM |
By the way, Can you upload file of 1MB?
07-02-2008 12:33 PM |
Yeah, I tried a 4mb file with no problems. My timeout is set really high, so that shouldn't be an issue.
07-02-2008 12:40 PM |
When I tried 33MB file, it really does not take that long to connect and finish. If I set a break point at my service side, it will get hit.
But with 88MB file, it takes a long time just trying to connect (I think) then fails. My break point at service side never got hit.
07-02-2008 12:51 PM |
So maybe the problem is on the client trying to prepare the file?
07-02-2008 1:34 PM |
Well, the file is already read into a byte array. My service expect byte array so I just send the byte array back.
But I did find if I try to convert the byte array to string with this code:
string s = System.Convert.ToBase64String(fileBytes);
I got out of memory error when my file size is 88MB. With 33MB file this line won't error. With 33M bytes the converted string.Length is 45292204.
So I think there still is a memory allocation issue.
07-02-2008 1:46 PM |
That doesn't sound good. Is there a workaround for that?
07-02-2008 1:59 PM |
I don't know. Maybe we hit limit again (?).
You can always break the file into pieces and send them piece by piece. If you choose to do that way, just remember to wait for one piece complete before sending the following one. With asynchronous calls you can not guarantee the receiving order if you send them at the same time.
07-02-2008 2:01 PM |
Do you know of an example showing how to break them up? I have no idea how to do that.
07-02-2008 3:58 PM |
I just created one. You can download from the following link:
http://www.2shared.com/file/3535556/a86cad70/SilverlightUpload.html
07-02-2008 5:03 PM |
I appreciate the help, but I have no idea how to piece the file back together on the server side, and only partially understand how you're breaking it up in the first place. Is there a tutorial that walks through the process?
07-02-2008 5:14 PM |
The solution file already included the Upload Service code. Have you run the demo code? Read the code by putting break points to both sides (Silverlight/Service) so you will understand how it works.
Basically I break the file byte array in chunks with given chunk size and put them into a List. Then I send them one chunk at a time. After I send the chunk, I deleted from the List. So I always take the first item on the list to send until the list is empty.
For the first chunk, I set append parameter = false. So a new file will be created. For the following chunks, the append flag is true. So the byte array will be appended to the exiting file.
public UploadFile DoUpload(string filename, byte[] content, bool append) { string folder = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "FileStore")); if (!System.IO.Directory.Exists(folder)) System.IO.Directory.CreateDirectory(folder); FileMode mode = FileMode.Create; if (append) mode = FileMode.Append; using (FileStream fs = new FileStream(folder + @"\" + filename, mode, FileAccess.Write)) { fs.Write(content, 0, content.Length); } UploadFile file = new UploadFile { Name = filename, FileStoreUrl = "../FileStore/" + filename }; return file; }
07-02-2008 5:28 PM |
I think the project you upload was the earlier one you were working on which returned the file length. What you posted here isn't anywhere in the project
I guess that's why I was so confused by it.
07-02-2008 5:55 PM |
You are right. That was a wrong file. Sorry about it.
07-02-2008 6:06 PM |
Thank goodness, because I was looking at the other application thinking I have no idea what I'm supposed to do.
07-03-2008 1:41 AM |
Well, I had to keep the chunks under 3 mb, but the upload worked great! Thanks for your help on this. You need a tip jar or somehing for all the issues you've helped me with.
07-31-2008 10:30 AM |
Jhorra,
I think there is a better way to do large file upload without having to break the file into chunks and mess up with the configurations. Just in case you want to try:
http://silverlight.net/forums/t/21513.aspx
microsof...
2904 points
566 Posts
08-04-2008 1:18 AM |
Thanks for sharing the FileUpload code. This is really very helpful. Thanks once again.