Saving image to database as varbinary (part 3)

狂风中的少年 提交于 2019-12-24 08:59:49

问题


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 :)


回答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.



来源:https://stackoverflow.com/questions/8280423/saving-image-to-database-as-varbinary-part-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!