mike g
mike g

Reputation: 109

asp.net core blazor: Images / files on network share work for 1 minute, then stop working

My app stores and retrieves photo images from a network share. My dev machine is Windows 2018 running Visual Studio 2019 on VMWare.

Images serve great when the app first opens. Then after around 1 minute (or so), the images 404.

Basically, I'm trying to implement some like classical asp.net's virtual directory in IIS. This allows read/writing files over a network share. My understanding is that asp.net core breaks old school virtual directories...

I tried many things like changing where on the network the images are located (different physical machines). No luck. I also tried various implementation of NetworkConnection (see the code below). Nada.

I also tried using UseStaticFiles instead of UseFileServer:

app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(
                    Path.Combine("\\\\DESKTOP-QRDNRCJ\\Images\\Photos2")),
                    RequestPath = new PathString("/Photos2")
                });

And finally, I tried IP address instead of machine network name.


To simplify debugging for stackoverflow, I made a completely new Blazor solution with a single page that only displays 3 images. It also fails after about 1 minute.

Interesting: Sometimes 2 out of 3 images render for a while. But eventually all 3 go 404.

Also, occasionally when I save a file over the network (using my full app which is not below), it saves the name of the file, but it's file size = 0 k.

It's frustrating because the app kinda works for a short time. So I'm not making a typo or other error (as far as I know).

Here are some references that helped me get started:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0&tabs=aspnetcore2x#serve-files-outside-of-web-root

https://thecodeblogger.com/2021/05/17/network-path-for-static-files-in-asp-net-core-web-applications/

https://www.c-sharpcorner.com/article/virtual-directory-inside-of-asp-net-core-app-in-iis/

And here's my code:

Simple page with 3 images:

@page "/index"
@page "/"
<img src="Photos2/birthday.gif" /><img src="Photos2/bo.jpg" /><img src="Photos2/dotnet.png" />

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication2.Data;
using System.Net;
using Microsoft.Extensions.FileProviders;
using System.IO;
using Microsoft.AspNetCore.Http;

namespace WebApplication2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            var sourceCredentials = new NetworkCredential { Domain = "192.168.1.162", UserName = "ImageManager", Password = "qwer1234)" };

            using (new NetworkConnection("\\\\192.168.1.162\\Images\\Photos2", sourceCredentials))
            {
                app.UseFileServer(new FileServerOptions()
                {
                    FileProvider = new PhysicalFileProvider(
                        Path.Combine("\\\\192.168.1.162\\Images\\Photos2")),
                    RequestPath = new PathString("/Photos2"),
                    EnableDirectoryBrowsing = true
                });

            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

The class for NetworkConnection.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Net;

public class NetworkConnection : IDisposable
{
    readonly string _networkName;

    public NetworkConnection(string networkName, NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

        var userName = string.IsNullOrEmpty(credentials.Domain)
            ? credentials.UserName
            : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

        var result = WNetAddConnection2(
            netResource,
            credentials.Password,
            userName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result, "Error connecting to remote share");
        }
    }

    ~NetworkConnection()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        WNetCancelConnection2(_networkName, 0, true);
    }

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource,
        string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags,
        bool force);

    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }

    public enum ResourceScope : int
    {
        Connected = 1,
        GlobalNetwork,
        Remembered,
        Recent,
        Context
    };

    public enum ResourceType : int
    {
        Any = 0,
        Disk = 1,
        Print = 2,
        Reserved = 8,
    }

    public enum ResourceDisplaytype : int
    {
        Generic = 0x0,
        Domain = 0x01,
        Server = 0x02,
        Share = 0x03,
        File = 0x04,
        Group = 0x05,
        Network = 0x06,
        Root = 0x07,
        Shareadmin = 0x08,
        Directory = 0x09,
        Tree = 0x0a,
        Ndscontainer = 0x0b
    }
}

screenshots of images rendering, then not..

Upvotes: 0

Views: 1873

Answers (1)

mike g
mike g

Reputation: 109

I think I solved. Posting in case anyone trips over something similar...

I needed to remove "using". So this ...

    using (new NetworkConnection("\\\\192.168.1.162\\Images\\Photos2",sourceCredentials))
            {
                app.UseFileServer(new FileServerOptions()
                {
                    FileProvider = new PhysicalFileProvider(
                        Path.Combine("\\\\192.168.1.162\\Images\\Photos2")),
                    RequestPath = new PathString("/Photos2"),
                    EnableDirectoryBrowsing = true
                });

            }

became this:

    new NetworkConnection("\\\\192.168.1.162\\Images\\Photos2", sourceCredentials);

   app.UseFileServer(new FileServerOptions()
   {
       FileProvider = new PhysicalFileProvider(
                Path.Combine("\\\\192.168.1.162\\Images\\Photos2")),
                RequestPath = new PathString("/Photos2"),
                EnableDirectoryBrowsing = true
   });

The only difference is the "using" and brackets are removed.

Why does this work? I guess the NetworkConnection object needs to be persisted. If I used "using", it's not. That was my problem, after 30-60 seconds, my network connection didn't work.

Just wish I'd realized this simple fix faster!

Upvotes: 3

Related Questions