dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

"FileSystem" doesn't work on .net maui 9 preview blazor hybrid "Web App" project #23718

Closed alsuomi closed 3 months ago

alsuomi commented 3 months ago

Discussed in https://github.com/dotnet/maui/discussions/23661

Originally posted by **alsuomi** July 17, 2024 **SCENARIO** I am using .Net Maui 9.0 Preview, Blazor Hybrid Web App. I need to initiate some code before the project starts (or in parallel) but it must display on home page after fetching it from some files. This code must work as **Maui App** (for Android, iOS, Mac) and as a **Web App**. That is why, I am initiating the services in the _MainLayout.razor.cs_ ( I added _MainLayout.razor.cs_) file, because I didn't find any where else to start (entrypoint to the apps)!?! **THE PROBLEM** Couple of tasks which must be completed first, It is working fine on Maui Apps side. But it gives an error when running it on Web App. **ERROR** **Microsoft.Maui.ApplicationModel.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.** (Full Log is attached) [ErrorLog.txt](https://github.com/user-attachments/files/16273466/ErrorLog.txt) Full Code is attahced [MauiApp5.zip](https://github.com/user-attachments/files/16275396/MauiApp5.zip) **CODE** public class MyFileAccess { private string jsonDataFileName = "Resources/Raw/languages.json"; public async Task> ReadJsonDataAsync() { ObservableCollection? genericTypeList = new(); Debug.WriteLine(FileSystem.AppDataDirectory); var isExist = await FileSystem.Current.AppPackageFileExistsAsync(jsonDataFileName); if (isExist) { var stream = await FileSystem.Current.OpenAppPackageFileAsync(jsonDataFileName); var reader = new StreamReader(stream); var contents = await reader.ReadToEndAsync(); genericTypeList = JsonSerializer.Deserialize>(contents); } return genericTypeList!; } }
github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Eilon commented 3 months ago

Hi @alsuomi , the error you're seeing is because the MAUI Essentials APIs are not generally available outside of .NET MAUI apps. So, in a Blazor Web app (running on ASP.NET Core), the API throws an exception. As such, this issue is a duplicate of #17317.

To make this work in your app, consider having an interface such as ILanguageLoader that has an API async Task<...> ReadLanguageJsonAsync(); with two implementations: a .NET MAUI implementation that uses MAUI Essentials, and an ASP.NET implementation that uses appropriate APIs for that platform.

You can look at this pattern in the project template with the IFormFactor sample interface and the two implementations of it.

alsuomi commented 3 months ago

Thanks for the reply @Eilon .

I believe that we are talking about Maui Blazor "Hybrid". It means that one implementation must work everywhere. It must be handled and dealt (interface implementation) on the Maui APIs side. Not on the user side.

Bug problem for developers, those who haven't got experience on neither Maui nor Blazor

Eilon commented 3 months ago

@alsuomi the request to make MAUI Essentials APIs work outside of .NET MAUI is tracked by this related issue: https://github.com/dotnet/maui/issues/17317

As of right now it's 'by design' because the MAUI Essentials API were only designed to workin within MAUI. But it's certainly something that we could reconsider.