【问题标题】:UWP App With Azure Mobile Apps Backend Offline Data Sync IssueUWP 应用与 Azure 移动应用后端离线数据同步问题
【发布时间】:2018-03-08 15:58:57
【问题描述】:

我正在用 C# 开发一个 UWP 应用程序,它使用 Azure 后端从 Azure SQL 数据库同步到本地设备 SQLite 数据库。自最初实施以来,这一直运行良好。

现在我在尝试同步较大的图像时遇到了问题。小于 ~ 3MB 的图像成功地从 StorageFile 转换为 byte[] 并保存在 SQLite 数据库中并同步到 Azure DB。但是,大于 ~ 3MB 的图像存在同步问题。它们将转换为 byte[] 并正确显示,但尝试同步失败并出现以下错误:

“HTTP 请求未包含有效的实体主体。请确保请求中存在实体主体和关联的 Content-Type 标头。”

在服务器端,这表现为格式错误的语法错误。

所有图像的处理方式完全相同。 web.config 文件已更改为允许 requestLimits maxAllowedContentLength="2147483648"。 Azure 数据库中的列设置为 varbinary(MAX)。

此代码调用方法将所选文件转换为字节[]:

byte[] image = await ImageUtils.ChooseImageFile();

这里是转换方法:

public static async Task<byte[]> ChooseImageFile()
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");
        StorageFile file = await openPicker.PickSingleFileAsync();

        if (file == null)
            return null;

        byte[] bytes = null;
        using (var stream = await file.OpenReadAsync())
        {
            bytes = new byte[stream.Size];
            using (var reader = new DataReader(stream))
            {
                await reader.LoadAsync((uint)stream.Size);
                reader.ReadBytes(bytes);
            }
        }
        return bytes;
    }

然后调用方方法将 byte[] 分配给实体的 Image 属性。
在同步时,我使用以下方法:

public async Task SyncOfflineCacheAsync()
    {
        try
        {
            await InitializeAsync();

            await _client.SyncContext.PushAsync();

            await PullTables();
        }
        catch (MobileServicePushFailedException ex)
        {
            if (ex.PushResult != null)
            {
                foreach (var error in ex.PushResult.Errors)
                {
                    await ResolveConflictAsync(error);
                }
            }
        }
    }

在 PushAsync() 上失败并出现上述错误。

以这种方式同步图像是否存在某种隐式大小限制?我注意到 byte[] 的字符串表示对于较大的图像显示为 base64,对于较小的图像显示为十六进制,但我找不到任何关于这是否与问题有关的信息。 byte[] 本身似乎是正确的。

谢谢!

【问题讨论】:

    标签: c# azure uwp synchronization


    【解决方案1】:

    web.config 文件已更改为允许 requestLimits maxAllowedContentLength="2147483648"。

    maxAllowedContentLength指定IIS支持的请求中内容的最大长度,而maxRequestLength表示ASP.NET支持的最大请求大小,因此您需要同时设置才能上传大文件:较小的一个“优先”。

    maxRequestLength 的默认值为 4096 KB (4 MB),因此您可能还需要设置此值。

    <system.web>
      <compilation debug="true" targetFramework="4.7"/>
      <httpRuntime targetFramework="4.7" maxRequestLength="102400"/>
    </system.web>
    

    更多详情请见this thread

    【讨论】:

    • 完美,这正是我所需要的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多