Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3939676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:19:41+00:00 2026-05-20T00:19:41+00:00

We have been successfully using protobuf-net v1 in a compact framework application to handle

  • 0

We have been successfully using protobuf-net v1 in a compact framework application to handle serializing our objects for storage in a sql server ce database.

Recently we hit a roadblock apparently due to using too many types (if we don’t serialize as many types the error goes away.) Ref: http://code.google.com/p/protobuf-net/issues/detail?id=50#c6

In desperation (we’re supposed to be releasing soon) we downloaded v2 and have been using it (without pre-compiling the serializers). However, we are occassionally getting strange errors when deserializing data — unknown wire-type 6 and an error reading an int-32 — somehow it is getting an overflow error when casting to an int which doesn’t make sense due to the fact that it was previously serialized using the same method…) It appears to me that we are getting some corruption of the binary data — but we are simply storing in a varbinary field in sql server ce and pulling it back out.

Does anyone have any ideas how the binary data could be corrupted? (See code below)

FINAL FIX:

Please read Marc’s answer for some background. The best I can tell the problem was with how the SetBinary method works — it does not appear to clear out or truncate existing data — so if the binary data being saved is smaller than the previous data junk is left at the end.

We fixed it by changing this:

if (buffer.Length > 0)
{
    record.SetBytes(insertSet.GetOrdinal(SerializedDataColumnName), 0, buffer, 0, buffer.Length);
}

to this:

if (buffer.Length > 0)
{
    record.SetValue(insertSet.GetOrdinal(SerializedDataColumnName), null);
    record.SetBytes(insertSet.GetOrdinal(SerializedDataColumnName), 0, buffer, 0, buffer.Length);
}

Thank you.

UPDATE:
Code used to serialize to the DB (code suggestions welcome as well as problem areas):

command.CommandType = CommandType.TableDirect;
MemoryStream ms = null;
using (SqlCeResultSet insertSet = command.ExecuteResultSet(ResultSetOptions.Updatable))
{
    foreach (var item in items)
    {
        ms = new MemoryStream();
        Serializer.Serialize<T>(ms, item);
        var record = insertSet.CreateRecord();
        var buffer = ms.GetBuffer();
        if (buffer.Length > 0)
        {
            record.SetBytes(insertSet.GetOrdinal(SerializedDataColumnName), 0, buffer, 0, buffer.Length);
        }
        else
        {
            record.SetValue(insertSet.GetOrdinal(SerializedDataColumnName), null);
        }
        insertSet.Update();
    }
}
if (ms != null)
{
    ms.Dispose();
}

Code used to deserialize:

using (var ms = new MemoryStream())
{
    using (SqlCeResultSet recordSet = command.ExecuteResultSet(ResultSetOptions.Scrollable))
    {
        //var serializer = null; //ServiceDepository.TryGetProvider<TypeModel, T>();
        while (recordSet.Read())
        {
            if (!recordSet.IsDBNull(recordSet.GetOrdinal(SerializedDataColumnName)))
            {
                var count = recordSet.GetBytes(recordSet.GetOrdinal(SerializedDataColumnName), 0, null, 0, 1);
                var bytes = new byte[count];
                recordSet.GetBytes(recordSet.GetOrdinal(SerializedDataColumnName), 0, bytes, 0, (int)count);
                if (bytes.Length > 0)
                {
                    var ms2 = new MemoryStream(bytes);
                    item = Serializer.Deserialize<T>(ms2);
                }
            }
            if (item == null)
            {
                //handle 'empty' items -- there were no properties
                //  that needed to be serialized
                item = new T();
            }
            list.Add(item);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:19:41+00:00Added an answer on May 20, 2026 at 12:19 am

    I can see a problem; you are asking the MemoryStream for GetBuffer and using the buffer’s length. A likely problem here is that GetBuffer returns the oversized backing buffer; you should either call .ToArray() on the MemoryStream to get a correctly sized buffer, or if you don’t want to allocate an extra array you can call GetBuffer() but you must only store the first memStream.Length bytes from that buffer; the rest should be considered garbage (it is most likely all zeros, but a leading zero is not valid in a protobuf field header).

    Now, this could just be part of the issue, but we should eliminate it first…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq-To-Sql based repository class which I have been successfully using. I
I have been using Hibernate very successfully, but today I noticed a bizarre phenomenon
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been looking into IKVMing Apache's FOP project to use with our .NET
I have been successfully using my code with the javascript library in the ANTLR
All these years i have been successfully using EnumServicesStatus in combination with OpenScManager (with
For a few months I have been successfully using David Justices Default Button example
I have a library that I have been using successfully with PHP 5.1.6 with
I have been using Selenium Webdriver successfully with JAR downloaded from their pages or
We have been using CruiseControl for quite a while with NUnit and NAnt. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.