asp.net core blazor:网络共享上的图像/文件工作 1 分钟,然后停止工作
Posted
技术标签:
【中文标题】asp.net core blazor:网络共享上的图像/文件工作 1 分钟,然后停止工作【英文标题】:asp.net core blazor: Images / files on network share work for 1 minute, then stop working 【发布时间】:2021-12-25 08:13:51 【问题描述】:我的应用程序从网络共享存储和检索照片图像。我的开发机器是在 VMWare 上运行 Visual Studio 2019 的 Windows 2018。
应用首次打开时,图片效果很好。然后大约 1 分钟(左右)后,图像 404。
基本上,我正在尝试在 IIS 中实现一些经典的 asp.net 虚拟目录。这允许通过网络共享读取/写入文件。我的理解是 asp.net core 打破了老派的虚拟目录......
我尝试了很多方法,例如更改图像在网络上的位置(不同的物理机器)。没有运气。我还尝试了 NetworkConnection 的各种实现(参见下面的代码)。纳达。
我也尝试过使用 UseStaticFiles 而不是 UseFileServer:
app.UseStaticFiles(new StaticFileOptions
FileProvider = new PhysicalFileProvider(
Path.Combine("\\\\DESKTOP-QRDNRCJ\\Images\\Photos2")),
RequestPath = new PathString("/Photos2")
);
最后,我尝试了 IP 地址而不是机器网络名称。
为了简化 *** 的调试,我制作了一个全新的 Blazor 解决方案,其中包含一个仅显示 3 张图像的页面。大约 1 分钟后它也会失败。
有趣:有时三分之二的图像会渲染一段时间。但最终所有 3 个都是 404。
此外,有时当我通过网络保存文件(使用不在下方的完整应用程序)时,它会保存文件的名称,但文件大小 = 0 k。
这很令人沮丧,因为该应用的运行时间很短。所以我没有犯错字或其他错误(据我所知)。
以下是一些帮助我入门的参考资料:
https://docs.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/
这是我的代码:
带有 3 张图片的简单页面:
@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");
);
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
【问题讨论】:
将单个路径传递给 Path.Combine 是没有意义的 是的,大声笑,点了。现在,关于我的实际问题/问题... 【参考方案1】:我想我解决了。发帖以防有人绊倒类似的东西......
我需要删除“使用”。所以这...
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
);
变成了这样:
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
);
唯一的区别是“使用”和括号被删除了。
为什么会这样?我猜 NetworkConnection 对象需要被持久化。如果我使用“使用”,则不是。那是我的问题,30-60 秒后,我的网络连接不起作用。
真希望我能更快地实现这个简单的修复!
【讨论】:
以上是关于asp.net core blazor:网络共享上的图像/文件工作 1 分钟,然后停止工作的主要内容,如果未能解决你的问题,请参考以下文章
Asp.NET Core进阶 第四篇 Asp.Net Core Blazor框架
Blazor 无法连接到 ASP.NET Core WebApi (CORS)
如何在 ASP .NET Core Web API 中映射回退,以便 Blazor WASM 应用程序仅拦截未发送到 API 的请求