首页 > 解决方案 > 将 liteDB 文档存储在云(天蓝色)blob 存储中

问题描述

我在 Xamarin Forms 中实现的 iOS 和 android 应用程序中使用 lightDB 作为本地数据库。我正在尝试使用 Azure 将本地 liteDB 存储在云中。我们已经实现了一个可以接收的 REST api,byte[]但是我在将 liteDB 文档获取到byte[]. 如果我尝试使用File.ReadAllBytes(LiteDbPath)存储 liteDB 的位置读取文件,我会得到一个System.IO.IOException: Sharing violation on path. 我认为这不是这样做的方法,但我无法弄清楚如何做到这一点。有人对如何做到这一点有任何建议吗?

我可能以错误的方式使用它,我在这方面非常缺乏经验。

更新:更多细节让我更清楚我做了什么以及我想做什么。

这是我们的 DataStore 类(我们使用 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
                }
            }
        }
    }
}

然后我们像这样使用它:

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);
        });
    }
}

完成这些部分的是一位不再在这里工作的同事。我认为将它用作 a 的DependencyService原因是能够在所有类中访问它,几乎就像一个单例一样。这可能应该改为单例类?

使用数据库可以正常工作的应用程序。但我想将整个数据库(文件)上传到 Azure,但我无法将其上传到byte[]. 当我做

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

我得到一个System.IO.IOException: Sharing violation on path. 正如一些人所暗示的那样,这可能是由于文件被锁定,关于如何解决这个问题的任何建议?

标签: c#xamarin.formslitedb

解决方案


LiteDB 是纯文件数据库,没有运行服务来访问数据。如果您创建 REST API,您应该在运行 IIS 的同一台机器(本地磁盘)中找到您的数据文件。

Azure Blob 存储是另一种服务,并根据请求提供文件。

将 LiteDB 视为FileStream具有“超能力”的简单类(与本地文件一起使用):)


推荐阅读