0

这是我之前的问题的后续,该问题已得到解决(谢谢),但现在我陷入了另一个错误。

我正在尝试将图像保存在我的数据库中(称为“Afbeelding”),为此我制作了一个表格,其中包含:

  • 编号:整数
  • 来源:varbinary(最大)

然后我创建了一个 wcf 服务来将“Afbeelding”保存到数据库中。

    private static DataClassesDataContext dc = new DataClassesDataContext();

    [OperationContract]
    public void setAfbeelding(Afbeelding a)
    {
        //Afbeelding a = new Afbeelding();
        //a.id = 1;
        //a.source = new Binary(bytes);

        dc.Afbeeldings.InsertOnSubmit(a);
        dc.SubmitChanges();
    }

然后,我在我的项目中引用了该服务,当我按下按钮时,我尝试将其保存到数据库中。

    private void btnUpload_Click(object sender, RoutedEventArgs e)
    {

        Afbeelding a = new Afbeelding();

        OpenFileDialog openFileDialog = new OpenFileDialog();

        openFileDialog.Filter = "JPEG files|*.jpg";

        if (openFileDialog.ShowDialog() == true)
        {
                //string imagePath = openFileDialog.File.Name;
                //FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                //byte[] buffer = new byte[fileStream.Length];
                //fileStream.Read(buffer, 0, (int)fileStream.Length);
                //fileStream.Close();

            Stream stream = (Stream)openFileDialog.File.OpenRead();
            Byte[] bytes = new Byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);
            string fileName = openFileDialog.File.Name;


            a.id = 1;
            a.source = new Binary { Bytes = bytes };

        }


        EditAfbeeldingServiceClient client = new EditAfbeeldingServiceClient();

        client.setAfbeeldingCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_setAfbeeldingCompleted);
        client.setAfbeeldingAsync(a);
    }

    void client_setAfbeeldingCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
            txtEmail.Text = e.Error.ToString();
        else
            MessageBox.Show("WIN");
    }

但是,当我这样做时,我收到以下错误:

    System.ServiceModel.FaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :a. 
    The InnerException message was 'There was an error deserializing the object of type OndernemersAward.Web.Afbeelding. 
    The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'.  
    Please see InnerException for more details.    
    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)    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)  
  atOndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndsetAfbeelding(IAsyncResult result)
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndsetAfbeelding(IAsyncResult result)
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndsetAfbeelding(IAsyncResult result)    
    at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

我不确定是什么原因造成的,但我认为这与我将图像写入数据库的方式有关?(数组长度太大了,真不知道怎么改)

谢谢你的帮助,托马斯

4

1 回答 1

1

看起来您需要更改绑定中的默认阅读器配额,将长度设置为适合您的值:

 <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/>
于 2011-11-26T01:45:18.987 回答