Skip to main content

Microsoft Silverlight

Answered Question Error trying to upload using WCF serviceRSS Feed

(0)

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Error trying to upload using WCF service

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
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Error trying to upload using WCF service

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.

 


 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Error trying to upload using WCF service

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.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Error trying to upload using WCF service

 What is your file size?

[Edit]

Never mind. I saw it in your last post.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Error trying to upload using WCF service

Right now it's choking on a 29mb file.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Error trying to upload using WCF service

Read this article. You might got session timeout.

 http://www.websupergoo.com/helpupload50/source/2-tech_notes/3-web.config.htm

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Error trying to upload using WCF service

 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>

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Error trying to upload using WCF service

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;

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Error trying to upload using WCF service

 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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Re: Error trying to upload using WCF service

By the way, Can you upload file of 1MB?


Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Error trying to upload using WCF service

Yeah, I tried a 4mb file with no problems.  My timeout is set really high, so that shouldn't be an issue.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Error trying to upload using WCF service

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.


 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

So maybe the problem is on the client trying to prepare the file?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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.

 

 

 

 


sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

That doesn't sound good.  Is there a workaround for that?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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.

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

Do you know of an example showing how to break them up?  I have no idea how to do that.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

I just created one. You can download from the following link:

http://www.2shared.com/file/3535556/a86cad70/SilverlightUpload.html

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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;       
        }

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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 Smile

I guess that's why I was so confused by it.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Answered Question

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

You are right. That was a wrong file. Sorry about it.

 http://www.2shared.com/file/3535556/a86cad70/SilverlightUpload.html

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

Thank goodness, because I was looking at the other application thinking I have no idea what I'm supposed to do.

Jhorra
Jhorra

Member

Member

522 points

475 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

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

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

microsoft_kc
microsof...

Contributor

Contributor

2904 points

566 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Error trying to upload using WCF service

Thanks for sharing the FileUpload code. This is really very helpful. Thanks once again.

 

 

Remember: Please click on "Mark As Answer", if this answered your query partially or fully.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities