我的 .net 核心应用程序有问题。这个问题是用户机密 json 文件无法读取。应用程序在开发中工作正常,但应用程序在托管服务器的发布版本上给了我错误。
错误代码如下
应用程序启动异常:System.ArgumentNullException:值不能为空。参数名称:C:\Users\myComp\source\repos\myApplicationCore\myApplicationCore.WebUI\Startup 中 myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services) 中 System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(String value) 的密码。 cs:第 42 行 --- 从先前引发异常的位置结束堆栈跟踪 --- 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 的 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize() --- 在 Microsoft.AspNetCore.Hosting.Internal.WebHost 处从先前引发异常的位置结束堆栈跟踪。BuildApplication() 暴击:Microsoft.AspNetCore.Hosting.Internal.WebHost[6] 应用程序启动异常 System.ArgumentNullException:值不能为空。参数名称:C:\Users\myComp\source\repos\myApplicationCore\myApplicationCore.WebUI\Startup 中 myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services) 中 System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(String value) 的密码。 cs:第 42 行 --- 从先前引发异常的位置结束堆栈跟踪 --- 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 的 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost。 http://127.0.0.1:25696应用程序已启动。按 Ctrl+C 关闭。应用程序正在关闭...
我的 Startup.cs 文件
public class Startup
{
public IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// 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)
{
var builder = new SqlConnectionStringBuilder(Configuration.GetConnectionString("DefaultConnection"));
builder.Password = Configuration["DbPassword"];
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(builder.ConnectionString,
b =>
{
b.MigrationsAssembly("myApplicationCore.WebUI");
b.ServerVersion(new Version(5, 6, 44), ServerType.MySql); // replace with your Server Version and Type
}
));
services.AddIdentity<ApplicationUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 3;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<IUnitOfWork, EfUnitOfWork>();
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/Sign");
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<HubSignal>("/hubCon");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
routes.MapSpaFallbackRoute(
name: "spa-fallback",
templatePrefix: "Portal",
defaults: new { controller = "Portal", action = "Index" });
});
SeedData.EnsurePopulated(app);
SeedData.CreateRoles(app.ApplicationServices).Wait();
SeedData.CreateIdentityUsers(app.ApplicationServices).Wait();
}
}
这是我的 Program.cs 文件
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
当我输入网址时,它给了我这个错误。
启动应用程序时发生错误。.NET Core 4.6.27817.03 X86 v4.0.0.0 | Microsoft.AspNetCore.Hosting 版本 2.2.0-rtm-35687 | 微软视窗 10.0.14393 | 需要帮忙?
我认为服务器找不到 secret.json 文件,但我不知道如何找到文件。
我怎么解决这个问题 ?
感谢您的帮助。