1

这又是我在将 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

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

编辑:我设法通过启用跟踪获得了更具可读性的错误,希望这对任何人都有帮助:)

4

1 回答 1

1

WCF 内置了各种限制。一个是maxReceivedMessageSize默认为 65536 字节,另一个是maxArrayLength(不确定默认值是什么)。您很有可能已经超过了两者之一(或两者)。您可以在服务配置中更改它们。MSDN 上的这篇文章包含一些示例配置。

此外,为您的服务启用跟踪可能会让您更深入地了解哪些限制受到了影响。

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

编辑:显然有一个名为Fiddler的工具可以帮助追踪这些问题。

于 2011-11-26T18:18:15.663 回答