1

This is yet again a problem I have with saving images in silverlight to my database, I thought I had it all working untill I tried it out with a different image...

I save images to my database with following method. I first convert the image to an array of byte and then send it to my service.

 private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            //nieuwe instantie van de klasse "Afbeelding", om later door te sturen naar service
            Afbeelding a = new Afbeelding();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "JPEG files|*.jpg";

            if (openFileDialog.ShowDialog() == true)
            {
                //Afbeelding ophalen via open dialoog
                Stream stream = (Stream)openFileDialog.File.OpenRead();
                string fileName = openFileDialog.File.Name;

                //Converteren naar bytes
                //byte[] bytes = BinaryConverter.convertToByte(stream);
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);

                //aan de instantie de Binary waarde van de afbeelding meegeven om naar de database te sturen
                a.id = 1;
                a.source = new Binary { Bytes = bytes };
            }

            EditAfbeeldingServiceClient client = new EditAfbeeldingServiceClient();

            client.UpdateAfbeeldingCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateAfbeeldingCompleted);
            client.UpdateAfbeeldingAsync(a);
        }

And in my service I do this:

    [OperationContract]
    public void UpdateAfbeelding(Afbeelding a)
    {
        var query = (from p in dc.Afbeeldings 
                     where p.id == a.id
                     select p).SingleOrDefault();

        query.source = a.source;
        dc.SubmitChanges();

    }

Now during my testing this all worked, but I only used one image to test... So when I tried just now with a different image, I get the following error:

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (400) Bad Request. In Silverlight, a 404 response code may be reported even when the service sends a different error code. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndUpdateAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndUpdateAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndUpdateAfbeelding(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

I can't really read anything out of that error so once again, I'm stuck here. I apologise for using these boards so much, but I really wouldn't if it wasn't needed so much.

I have set the maximum to send through to a high number, but it still doesn't work.

<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />

You can find my web.config here: http://pastebin.com/whMs5h1w

Thank you for your help, I really appreciate it. Thomas

Edit: I managed to get a more readable error with enabling tracing, hope this helps anyone :)

Schoof
  • 2,715
  • 5
  • 29
  • 41

1 Answers1

1

WCF has various limits built in. One is the maxReceivedMessageSize which is 65536 bytes by default and another one is maxArrayLength (not sure what the default is). There is a good chance you have exceeded one of the two (or both). You can change those in your service configuration. This article on MSDN contains some example configurations.

Also enabling tracing for your service might provide you with some more insight of which limits are hit.

Btw: There is a File.ReadAllBytes method.

Edit: Apparently there is a tool called Fiddler which can help tracking these issues down.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • So what am I supposed to do? I can't save images to my database with WCF? – Schoof Nov 26 '11 at 18:22
  • @ThomasSchoof: No, you need to increase the limits in your config. Sorry, made it clearer in the answer. – ChrisWue Nov 26 '11 at 18:24
  • I already had to increase the maxArrayLength because of a previous error I got, and that one stated I had to enlarge it. (http://stackoverflow.com/questions/8275398/saving-image-to-database-as-varbinary-arraylength-part-2/8275697#8275697) And I could just use the File.ReadAllbytes method to convert the image to an array of byte? – Schoof Nov 26 '11 at 18:28
  • @ThomasSchoof `ReadAllBytes` does not convert anything. It just reads the file content into a byte array, same thing you are doing in your code right now. – ChrisWue Nov 26 '11 at 18:33
  • Thank you, but what am I supposed to do with sizes? I already increased them with `` And it does not work, or is that nog long enough? – Schoof Nov 26 '11 at 18:37
  • @ThomasSchoof If you already increased them then they are probably not the problem. Try enabling WCF tracing and Fiddler. – ChrisWue Nov 26 '11 at 18:40
  • I really have no idea what I'm doing with all the debugging, I enabled WCF and installed Fiddler but it doesn't really tell me anything... This is getting way out of my league – Schoof Nov 26 '11 at 19:00
  • I increased the max of nearly everything to the maximum but I'm still getting the same error: `` – Schoof Nov 26 '11 at 19:09
  • Try enabling tracing (server side), as ChrisWue suggested; the traces on the server will have some information why the request was rejected. – carlosfigueira Nov 27 '11 at 16:05
  • I just did so, I editted the error I am getting, thanks for the help so far :) I appear to be a getting a [protocol exception](http://msdn.microsoft.com/en-us/library/bb907249.aspx) – Schoof Nov 27 '11 at 16:09