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.28k stars 1.76k forks source link

WebView2 tries to write files into Program Files #17827

Open angelru opened 1 year ago

angelru commented 1 year ago

Description

I just packaged my application without MSIX and got all the dlls and other files of my application. In my application I use a WebView and all it works fine, but if I move the obtained folder into program files or program files 86, the WebView stops working, the rest of the application seems to work fine. If I again move the folder to the desktop or to C:\ Works correctly.

Steps to Reproduce

  1. Create application with WebView
  2. Packaged application without msix
  3. dotnet publish -f net7.0-windows10.0.19041.0 -c Release -p:RuntimeIdentifierOverride=win10-x64 -p:WindowsPackageType=None

Link to public reproduction project repository

https://github.com/angelru/17827

No response

Version with bug

7.0.92

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

angelru commented 1 year ago

Has anyone found a solution to this problem? I need to package my application on a regular MSI because it needs other external dependencies and MSIX doesn't offer that.

daltzctr commented 1 year ago

Not a bug, open or convert to a discussion.

You cannot distribute msix without msix, it's an OS restriction. What you want is unpackaged mode, see the WinUI3 docs on unpackaged for docs on that. Note that I think MAUI unpackaged apps are bugged, so you might hit a deadend.

angelru commented 1 year ago

@daltzctr Do not turn this issue into a discussion.

Given the complexity of my application in terms of requirements, I think it is impossible to use .NET MAUI, and if I do not find a solution, I will have to migrate to AvaloniaUI.

Here I leave my question in case someone could guide me with .NET MAUI. https://learn.microsoft.com/en-us/answers/questions/1380458/execute-msix-with-administrator-privileges-and-run

I am considering the option of using Winx4 Toolset or Inno Setup, but I am encountering the problem that I have described in this issue, when creating my custom installer.

daltzctr commented 1 year ago

Issues are for bugs or feature requests. This is not a bug and is intended behavior.

I gave suggested options, it's your choice to ignore them. Migrating to Avalonia is a valid choice and I would recommend it.

angelru commented 1 year ago

Well, I don't agree that it's the intended behavior.

It is strange that a component does not work in the program file folders but if in C:\ or on the desktop, there will be some explanation.

Let's see if someone from the .NET MAUI team moves it up for discussion and someone can say something about it.

angelru commented 1 year ago

I found out that the webview uses a user folder and program files does not have permissions

https://stackoverflow.com/questions/76198109/how-to-set-allowsinglesignonusingosprimaryaccount-property-for-webview-in-maui

string userDataFolder = Path.Combine(Path.GetTempPath(), "WhatEver");
webView21.CreationProperties = new CoreWebView2CreationProperties()
{
    UserDataFolder = userDataFolder
};
await webView21.EnsureCoreWebView2Async();

Why is this not available in NET MAUI?

angelru commented 1 year ago

There are no alternatives to configure the folder?

           var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\AppName";
             var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
             await webView.EnsureCoreWebView2Async(env);
             webView.Source = new Uri("https://here.com") ;

This cannot be done in .NET MAUI

mobynet1 commented 11 months ago

@daltzctr, I agree with @angelru: This is an issue and not a discussion! The issue, as I am having the same frustrations, is that when you publish an unpackaged maui app and then take the results from the build output and use that in something like the WiX Toolset to build an MSI installer things break. Font Icons do not get rendered for example. When the app is run directly from within Visual Studio, font icons show up as expected, but when you run the output in a different folder (such as \Program Files\app) things do not get rendered - that is the bug.

In fact, I just tried to run the .exe file in the ...\bin\Debug...\net8.0-windows10.0.19041.0 it won't even start up. This is the entry from the Windows Logs/Application event log:

Application: MobyClient.exe
CoreCLR Version: 8.0.23.53103
.NET Version: 8.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException: The type initializer for '<Module>' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'WinRT.ActivationFactory`1' threw an exception.
 ---> System.Runtime.InteropServices.COMException (0x80040154): Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName)
   at WinRT.ActivationFactory`1..ctor()
   at WinRT.ActivationFactory`1..cctor()
   --- End of inner exception stack trace ---
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentInitializeOptions..ctor()
   at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.get_Options() in C:\Users\cimfr\.nuget\packages\microsoft.windowsappsdk\1.3.230724000\include\DeploymentManagerAutoInitializer.cs:line 44
   at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.AccessWindowsAppSDK() in C:\Users\cimfr\.nuget\packages\microsoft.windowsappsdk\1.3.230724000\include\DeploymentManagerAutoInitializer.cs:line 30
   at .cctor()
   --- End of inner exception stack trace ---

So, from my perch, this is a bug. And a glaringly huge bug for those of us who will never do an MSIX publish!!

fabien251 commented 11 months ago

There are no alternatives to configure the folder?

         var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\AppName";
           var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
           await webView.EnsureCoreWebView2Async(env);
           webView.Source = new Uri("https://here.com") ;

This cannot be done in .NET MAUI

try this :

var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\AppName"; Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder);

angelru commented 11 months ago

There are no alternatives to configure the folder?

         var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\AppName";
           var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
           await webView.EnsureCoreWebView2Async(env);
           webView.Source = new Uri("https://here.com") ;

This cannot be done in .NET MAUI

try this :

var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\AppName"; Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder);

at the start of the application?

fabien251 commented 11 months ago

There are no alternatives to configure the folder?

         var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\AppName";
           var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
           await webView.EnsureCoreWebView2Async(env);
           webView.Source = new Uri("https://here.com") ;

This cannot be done in .NET MAUI

try this : var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\AppName"; Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder);

at the start of the application?

Yes

beeradmoore commented 9 months ago

Thanks for all the info in this thread. In the first line of App.xaml.cs constructor I did this,

#if WINDOWS
var userDataFolder = Path.Combine(FileSystem.AppDataDirectory, "WebView2");
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder);
#endif

I should probably move it to Platforms\Windows\App.xaml.cs as it is only needed on Windows.

angelru commented 8 months ago

Thanks for all the info in this thread. In the first line of App.xaml.cs constructor I did this,

#if WINDOWS
var userDataFolder = Path.Combine(FileSystem.AppDataDirectory, "WebView2");
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder);
#endif

I should probably move it to Platforms\Windows\App.xaml.cs as it is only needed on Windows.

Do I have to restart the computer?

beeradmoore commented 8 months ago

Nope. That isn’t working for you?

angelru commented 8 months ago

Nope. That isn’t working for you?

its works!

mattleibow commented 1 week ago

I think this issue is discussing the fact that the app in program files fails to run because the webview tries to write some data into the program files?

Maybe we can set up some defaults in maui to use the local user settings?

But, why would the webview put data in the program files folder? That is not normal. It usually goes to application data somewhere.

mattleibow commented 1 week ago

I see blazor works around this: https://github.com/dotnet/maui/blob/962d96ffb5b34f01df0e752c74306aa451988b8e/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs#L407

So yeah, this is both super weird as why do apps write in their local install location??? What about multi-user support? But, we can fix it in MAUI and make it all good.