Loading App Data

Problem

You need to load app data files on all platforms. Traditional file access methods (System.IO.File.Read*, EmbeddedResource) do not work for Content files on WASM.

Solution

Use the Windows.Storage.StorageFile API to read files from your app package. This works the same way on all Uno Platform targets, including WASM.

  • Move your data files to be included as Content in your project, and use StorageFile.GetFileFromApplicationUriAsync to load them by path.

    <ItemGroup>
        <Content Include="AppData\*.json" />
    </ItemGroup>
    
  • Loading a JSON File Using StorageFile

    public abstract class BaseMockEndpoint(ISerializer serializer, ILogger<BaseMockEndpoint> _logger)
    {
        protected async Task<T?> LoadData<T>(string fileName)
        {
            try
            {
                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///AppData/{fileName}"));
                var json = await FileIO.ReadTextAsync(file);
                return serializer.FromString<T>(json);
            }
                catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to load {FileName}", fileName);
                return default;
            }
        }
    }
    
  • Using LoadData in MockRecipeEndpoints

    public class MockRecipeEndpoints(string basePath, ISerializer serializer, ILogger<BaseMockEndpoint> logger) : BaseMockEndpoint(serializer, logger)
    {
        public async Task<string> HandleRecipesRequest(HttpRequestMessage request)
        {
            var savedList = await LoadData<List<Guid>>("SavedRecipes.json") ?? [];
            var allRecipes = await LoadData<List<RecipeData>>("Recipes.json") ?? [];
            ...
        }
    }
    

Source Code

Documentation