1

I'm developing an application using cefSharp, all is good now i need to maintain history for all browser tabs opened and closed by user and providing a option for opening closed tabs.

Is there a way to store Chromium Web Browser object into memory or database and retrieve it from there. I've used below code to store objects into database

var objBrowserTest = (ChromiumWebBrowser)browserControl;

MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(objBrowserTest);

string sql = @"
INSERT INTO tblBrowserObj(cromObj)
VALUES(@VarBinary);
SELECT @@identity";

string connectionstring;
connectionstring = ConfigurationManager.ConnectionStrings[0].ConnectionString;
SqlConnection objConn = new SqlConnection(connectionstring);
if (objConn.State == ConnectionState.Open)
{
    objConn.Close();
}
objConn.Open();
SqlCommand sqlCmd = new SqlCommand(sql, objConn);
sqlCmd.Parameters.Add("@VarBinary", SqlDbType.VarBinary, Int32.MaxValue);
sqlCmd.Parameters["@VarBinary"].Value = memStream.GetBuffer();
string strID = sqlCmd.ExecuteScalar().ToString();
if (objConn.State == ConnectionState.Open)
{
    objConn.Close();
}    

Table structure

CREATE TABLE [dbo].[tblBrowserObj](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [cromObj] [varbinary](max) NULL
) ON [PRIMARY]

My problem is that the memStream.GetBuffer() always shows byte[0].

Can any one tell me how can i store and retrieve Chromium Web Browser object from memory or database, or is there another way to do this type of stuff.

Ashish Rathore
  • 2,546
  • 9
  • 55
  • 91

1 Answers1

0

1 - Read/Write from memory

From Writing to then reading from a MemoryStream I extract some steps you should follow:

  1. Don't forget to flush after sw.Write(objBrowserTest);
  2. You have to reset the position of the stream with memStream.Position = 0; before read.
  3. Wrap the stream with a StreamWriter and use the ReadToEnd() method to read all bytes of the stream.

Don't use GetBuffer(), it doesn't get the content of the stream, it takes the buffer of a buffered stream.

2 - Read/Write an Object from Byte[]

When you want to serialeze and object you have 2 classes to do it DataContractSerializer (DCS) and BinaryFormatter (BF).

2.1 - Serialize

When you want to serialize to a byte[] you can use the DCS 'WriteObtect' or the BF 'Serialize'. Both methods uses an stream (your memory stream) and an object.

2.2 - Deserialize

To deserialize an stored byte[] from it, you can use the DCS ReadObject and BF Deserialize (casting to the class). Both methods argument is the Stream to read from.

What you need to do

  1. Serialize with one
  2. Use the memStream.ToArray() to get the byte[] to store it.
  3. When retrieving, use a a memory stream to write the array

Tips

Use the MemoryStream constructor with a byte[] argument and then Position = 0.

This code maybe helps you:

public byte[] ObjectToBytes(Object obj)
{
    try
    {
        using (MemoryStream memStream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(memStream, obj);

            return memStream.ToArray();
        }
    }
    catch (Exception) { }  // do what you want

    return null;
}

public T BytesToObject<T>(byte[] array) where T : class
{
    try
    {
        using (MemoryStream memStream = new MemoryStream(array) { Position = 0 })
        {
            BinaryFormatter formatter = new BinaryFormatter();
            T obj = formatter.Deserialize(memStream) as T;

            return obj;
        }
    }
    catch (Exception) { }  // do what you want

    return null;
}

Simply use sqlCmd.Parameters["@VarBinary"].Value = ObjectToBytes(objBrowserTest ); and when retrieving use BytesToObject<ChromiumWebBrowser>(bytes);

Sources

http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Byte-array-to-object.html

How deserialize Byte array to object - Windows 8 / WP 8

https://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx

Community
  • 1
  • 1