0

I am using lightDB as a local database in my iOS and android app implemented in Xamarin Forms. I am trying to store my local liteDB in the cloud using Azure. We have implemented a REST api which can receive a byte[] but I am having problem getting the liteDB documents to a byte[]. If I try to read the file using File.ReadAllBytes(LiteDbPath) where we have stored the liteDB i get a System.IO.IOException: Sharing violation on path. I assume this is not the way to do this, but I am unable to figure out how to do this. Anyone have any suggestions on how to do this?

It is possible I am using this the wrong way, I am quite unexperienced in this area.

Update: More details to make it clearer what I have done and what I want to do.

This is our DataStore class (where we use LiteDB):

[assembly: Dependency(typeof(DataStore<Zystem>))]

namespace AirGlow_App.Services {
    class DataStore<T> {

        public void Close()
        {
            var db = new LiteRepository(LiteDbPath);
            db.Dispose();
        }

        public LiteQueryable<T> Get()
        {
            using (var db = new LiteRepository(LiteDbPath))
            {
                try
                {
                    return db.Query<T>();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception when doing Get. Exception = {ex.Message}.", TypeDescriptor.GetClassName(this));
                    //TODO : General Error Handling
                    return null;
                }
            }
        }

        public T Get(BsonValue id)
        {
            using (var db = new LiteRepository(LiteDbPath))
            {
                try
                {
                    return db.Query<T>().SingleById(id);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception when doing Get. Exception = {ex.Message}.", TypeDescriptor.GetClassName(this));
                    //TODO : General Error Handling
                    return default(T);
                }
            }
        }

        public void Add(T obj)
        {
            using (var db = new LiteRepository(LiteDbPath))
            {
                try
                {
                    db.Insert<T>(obj);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception when doing Add. Exception = {ex.Message}.", TypeDescriptor.GetClassName(this));
                    //TODO : General Error Handling
                }
            }
        }

        public void Delete(Guid Id)
        {
            using (var db = new LiteRepository(LiteDbPath))
            {
                try
                {
                    var o = new BsonValue(Id);

                    db.Delete<T>(o);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception when doing Delete. Exception = {ex.Message}.", TypeDescriptor.GetClassName(this));
                    //TODO : General Error Handling
                }
            }
        }

        public void Save(T obj)
        {
            using (var db = new LiteRepository(LiteDbPath))
            {
                try
                {
                    db.Update<T>(obj);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception when doing Save. Exception = {ex.Message}.", TypeDescriptor.GetClassName(this));
                    //TODO : General Error Handling
                }
            }
        }
    }
}

Then we are using it like this:

public class ZystemsViewModel : ObservableObject
{
    private DataStore<Zystem> DB = DependencyService.Get<DataStore<Zystem>>();

    public ZystemsViewModel()
    {
        MessagingCenter.Subscribe<ZystemAddViewModel, Zystem>(this, "Add", (obj, item) =>
        {
            var newItem = item as Zystem;
            Debug.WriteLine($"Will add {newItem.Name} to local database.", TypeDescriptor.GetClassName(this));
            DB.Add(newItem);
        });
    }
}

It was a colleague who is not working here anymore who did these parts. I think the reasoning for using it as a DependencyService was to be able to access it in all classes, pretty much as a singleton. This should probably be changed to a singleton class instead?

Using the database works fine the app. But I want to upload the entire database (file) to Azure and I am unable to get it to a byte[]. When I do

byte[] liteDbFile = File.ReadAllBytes(LiteDbPath);

I get a System.IO.IOException: Sharing violation on path. As some are suggesting it is probably due to the file is being locked, any suggestions on how to solve this?

slimjim10
  • 1
  • 1
  • It is unclear to me (and probably everyone else) what exactly you are doing and where you are doing it. Please give a better description, maybe starting with where the code `File.ReadAllBytes(LiteDbPath)` is running and some code for context. – Crowcoder May 09 '19 at 11:27
  • @slimjim,According to your error message, if you have use LiteDbPath before using File.ReadAllBytes[], please confirm if the file is locked by stream? – Cherry Bu - MSFT May 10 '19 at 06:46

1 Answers1

0

LiteDB is plain file database and has no running service to access data. If you create a REST API, you should locate you datafile in same machine (local disk) that are running your IIS.

Azure blob storage is another service and deliver file as request.

Think LiteDB as a simple FileStream class (that works with local file) with "super-powers" :)

mbdavid
  • 1,076
  • 7
  • 8