Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF Blazor cannot be started at boot. Error: There is no content at #19359

Closed
JiuLing-zhang opened this issue Dec 12, 2023 · 11 comments
Closed
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/windows 🪟 s/duplicate 2️⃣ This issue or pull request already exists t/bug Something isn't working

Comments

@JiuLing-zhang
Copy link

Description

I have created a WPF Blazor via the MS sample , but it doesn't be started at boot.

Microsoft.AspNetCore.Components.WebView.Wpf is used in the example, which points to this repository in nuget.

Steps to Reproduce

  1. Clone https://github.com/JiuLing-zhang/TestAutostart.git
  2. Publish project. (dotnet publish -c Release -r win-x64 -p:PublishReadyToRun=true --self-contained false)
  3. Add startup via registry. (计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run)
    regedit.png
  4. Restart computer.
  5. The application error.
    AutostartError.png

Link to public reproduction project repository

https://github.com/JiuLing-zhang/TestAutostart.git

Version with bug

7.0.101

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 11

Did you find any workaround?

No response

Relevant log output

No response

@JiuLing-zhang JiuLing-zhang added the t/bug Something isn't working label Dec 12, 2023
@jfversluis jfversluis added the area-blazor Blazor Hybrid / Desktop, BlazorWebView label Dec 12, 2023
@Eilon
Copy link
Member

Eilon commented Dec 12, 2023

Hi @JiuLing-zhang , this could be due to the 'current directory' of the app being the %system32% (or similar) folder, which happens with autostart apps. When you launch an app from Explorer or the Start Menu the 'current directory' is normally set to the folder containing the app.

However, the BlazorWebView control does try to force loading its content from wherever the app is on disk so the current directory shouldn't matter. This is the code that does that: https://github.com/dotnet/maui/blob/main/src/BlazorWebView/src/Wpf/BlazorWebView.cs#L258

Can you confirm that all of the app's file are actually on disk in the expected location? That is, go to wherever the published EXE is on disk and check that the wwwroot and related files/folders are all there?

@JiuLing-zhang
Copy link
Author

Hi @JiuLing-zhang , this could be due to the 'current directory' of the app being the %system32% (or similar) folder, which happens with autostart apps. When you launch an app from Explorer or the Start Menu the 'current directory' is normally set to the folder containing the app.

However, the BlazorWebView control does try to force loading its content from wherever the app is on disk so the current directory shouldn't matter. This is the code that does that: https://github.com/dotnet/maui/blob/main/src/BlazorWebView/src/Wpf/BlazorWebView.cs#L258

Can you confirm that all of the app's file are actually on disk in the expected location? That is, go to wherever the published EXE is on disk and check that the wwwroot and related files/folders are all there?

Hi @Eilon , all documents are complete. I guess it maybe about the path, because the same problem occurred when I started it with cmd.

  1. It can run normally through exe.
  2. cmd can run normally using the current directory.
  3. cmd cannot run when using other directories.

I recorded a gif for this:
ErrorGif.gif

@Eilon
Copy link
Member

Eilon commented Dec 13, 2023

@JiuLing-zhang thank you for confirming, this does look like a bug.

Could you try adding this line at the start of your app's Program.cs file?

Environment.CurrentDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location);

If that works, it should be a good workaround for you to use until we can investigate further.

@JiuLing-zhang
Copy link
Author

@JiuLing-zhang thank you for confirming, this does look like a bug.

Could you try adding this line at the start of your app's file?Program.cs

Environment.CurrentDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location);

If that works, it should be a good workaround for you to use until we can investigate further.

Hi @Eilon ,

This path is the real path during build, it won't work on other computers. My project type of WPF.

// use App, not Program
Path.GetDirectoryName(typeof(App).Assembly.Location);

I temporarily solved the problem by doing the following:

Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

Thanks.

@JiuLing-zhang
Copy link
Author

@JiuLing-zhang thank you for confirming, this does look like a bug.
Could you try adding this line at the start of your app's file?Program.cs

Environment.CurrentDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location);

If that works, it should be a good workaround for you to use until we can investigate further.

Hi @Eilon ,

This path is the real path during build, it won't work on other computers. My project type of WPF.

// use App, not Program
Path.GetDirectoryName(typeof(App).Assembly.Location);

I temporarily solved the problem by doing the following:

Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

Thanks.

I made a mistake, typeof(App).Assembly.Location takes effect at runtime, but the value is null, so it's not work.

@Eilon
Copy link
Member

Eilon commented Dec 15, 2023

@JiuLing-zhang oh interesting. Perhaps because of the publish/ReadyToRun, that causes the assembly to no longer exist in its original form, so that ends up being null. That is probably the root cause of this issue. I think that gives us enough info to try to come up with a more permanent solution.

Does the workaround always work in your case?

@JiuLing-zhang
Copy link
Author

@Eilon I changed some computers for test and it works.

Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

@Mr-Technician
Copy link

I am also running into this issue when publishing a single-file Winforms app with Blazor in Webview. The issue does not occur for a normal (not single-file) build.

@Eilon
Copy link
Member

Eilon commented Jan 30, 2024

I am also running into this issue when publishing a single-file Winforms app with Blazor in Webview. The issue does not occur for a normal (not single-file) build.

@Mr-Technician does the CurrentDirectory workaround above work for you? It's likely the same issue.

@Mr-Technician
Copy link

@Eilon This is my current solution:

blazorWebView1.HostPage = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "wwwroot\\index.html");

It is essentially the same as setting Environment.CurrentDirectory.

@mkArtakMSFT
Copy link
Member

This was fixed by #21750

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2024
@mkArtakMSFT mkArtakMSFT added the s/duplicate 2️⃣ This issue or pull request already exists label Apr 19, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/windows 🪟 s/duplicate 2️⃣ This issue or pull request already exists t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants