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

IIS published Blazor .NET8 app fails to deliver styles and scripts on first device connection #292

Open
ynnob opened this issue Nov 23, 2023 · 2 comments

Comments

@ynnob
Copy link

ynnob commented Nov 23, 2023

Hello!

We have a Blazor app running .NET 8 on a Windows Server published through Visual Studios IIS publish option.

We used WebOptimizer in our Program.cs and then started noticing that

  1. After Successful Publishing the webpage thats been opened by Visual Studio does not display any styles or scripts (F12 shows that the files are not delivered by the server 404|500)
  2. Devices like phones that open a link to this blazor app for the first time will also fail to load the script and styles
  3. When reloading the page (F5) the styles and scripts can be loaded

After some digging in the Windows Error logs i saw several errors popping up for each of the failed files that the browser tried to download.

App.razor:
<link rel="stylesheet" href="/css/app.min.css" />

Example for app.min.css

Category: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
EventId: 1
SpanId: 2ea13da234f83e9f
TraceId: 54fdcae62c87d46c65c9a3e720204362
ParentId: 0000000000000000
RequestId: 800005f7-0000-ef00-b63f-84710c7967bb
RequestPath: /css/app.min.css

An unhandled exception has occurred while executing the request.

Exception: 
System.UnauthorizedAccessException: Access to the path 'C:\inetpub\sites\<IIS_PROJECT_FOLDER>\obj\WebOptimizerCache' is denied.
   at System.IO.FileSystem.CreateDirectory(String fullPath, Byte[] securityDescriptor)
   at System.IO.Directory.CreateDirectory(String path)
   at WebOptimizer.AssetResponseStore.AddAsync(String bucket, String cachekey, AssetResponse assetResponse)
   at WebOptimizer.AssetBuilder.BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
   at WebOptimizer.AssetMiddleware.HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.<Invoke>g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

This causes a chain reaction and prevents the download of all files.

The published Blazor page does not contain an \obj\WebOptimizerCache folder and WebOptimizer fails to create this folder.

FIX:
Manually adding this folder solves all the problems but i wonder what is causing this because the IIS User has all the permissions required. If this is all expected behaviour the documentation should be updated and reflect that it is necessary to create this folder structure to use the WebOptimizer.

@A9G-Data-Droid
Copy link

My first time using this library and I hit the same issue today. I'm using the latest version of everything. After creating the missing path the error goes away and the site becomes usable.

Strange note: No cache files are ever created there! So it's throwing an unrecoverable error that crashes the site for all users and then it's not even using the cache folder. I would not consider this exceptional. It needs to log the error and then have a graceful fallback to a different method. Especially when this folder isn't in use!

@aliajboy
Copy link
Contributor

aliajboy commented Sep 10, 2024

I have the same on Plesk Shared Hosting (Not blazor, Asp.Net Core MVC 8). I asked server and CDN supportand they didn't found any error and told me that my code has problem. but I wonder What is the problem! I searched everywhere and then for testing, I commented WebOptimizer from program.cs and everything works fine. a fix would be nice!

Here is the Image after the user opens the page for the first time, no matter desktop or mobile user:

Screenshot 2024-09-10 164618

as you can see HTML is rendered and images and other static files loaded except CSS and JS files with error 500. After reloading everything is going to be fine and fast.

another thing I found is that after IIS Idle timeout, this problem happens again for the users. (because it's going to start again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants