将图像保存为varbinary(第3部分)

时间:2011-11-26 18:01:10

标签: c# silverlight linq image varbinary

这又是一个问题,我将Silverlight中的图像保存到我的数据库中,我认为我已经完成所有工作,直到我用不同的图像尝试了...

我使用以下方法将图像保存到我的数据库中。 我首先将图像转换为byte数组,然后将其发送给我的服务。

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

在我的服务中,我这样做:

    [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();

    }

现在在测试过程中这一切都有效,但我只用了一张图片进行测试...... 所以当我刚才尝试使用不同的图像时,我收到以下错误:

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)

我真的无法从那个错误中读到任何东西所以再一次,我被困在这里。 我为使用这些电路板而道歉,但如果不是那么需要,我真的不会。

我已将发送的最大值设置为较高的数字,但仍无效。

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

您可以在此处找到我的web.config:http://pastebin.com/whMs5h1w

感谢您的帮助,我真的很感激。 托马斯

编辑:我设法在启用跟踪时获得更可读的错误,希望这有助于任何人:)

1 个答案:

答案 0 :(得分:1)

WCF内置了各种限制。一个是maxReceivedMessageSize,默认为65536字节,另一个是maxArrayLength(不确定默认值是什么)。你有可能超过两个(或两个)中的一个。您可以更改服务配置中的那些。 This article on MSDN包含一些示例配置。

同样enabling tracing for your service可能会让您更深入地了解哪些限制受到了限制。

顺便说一句:有File.ReadAllBytes方法。

修改:显然有一个名为Fiddler的工具可以帮助您跟踪这些问题。