【问题标题】:Saving image to database as varbinary (part 3)将图像作为 varbinary 保存到数据库(第 3 部分)
【发布时间】:2011-11-26 18:01:10
【问题描述】:

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

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

编辑:我设法在启用跟踪时得到一个更易读的错误,希望这对任何人都有帮助:)

【问题讨论】:

    标签: c# silverlight linq image varbinary


    【解决方案1】:

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

    另外enabling tracing for your service 可能会让您更深入地了解哪些限制已达到。

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

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

    【讨论】:

    • 那我该怎么办?我无法使用 WCF 将图像保存到我的数据库中?
    • @ThomasSchoof:不,您需要增加配置中的限制。对不起,在答案中说得更清楚了。
    • 我已经不得不增加 maxArrayLength 因为我之前遇到的一个错误,并且那个人说我必须扩大它。 (stackoverflow.com/questions/8275398/…) 我可以只使用 File.ReadAllbytes 方法将图像转换为字节数组吗?
    • @ThomasSchoof ReadAllBytes 不会转换任何内容。它只是将文件内容读入一个字节数组,就像你现在在代码中做的一样。
    • 谢谢,但是我应该如何处理尺寸?我已经用&lt;readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/&gt; 增加了它们,但它不起作用,或者说时间不够长?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多