c# - System.Configuration.ConfigurationSettings.AppSettings 在生产中不可访问

标签 c# .net web-config desktop-application windows-applications

我有一个 Windows 桌面应用程序,Web 应用程序正从该应用程序启动。

private void Home_Load(object sender, EventArgs e)
       {
           string url = string.Format("http://localhost:49916/Express/Login.aspx?yek@soh={0}", System.Configuration.ConfigurationSettings.AppSettings["HK"].ToString());

           Process.Start("IExplore.exe", url);
           this.Close();
       }

它在我的机器上运行良好。然后我创建了一个安装程序来安装它,它工作正常但是当我在生产机器上运行我新安装的程序时我得到以下异常:

System.NullReferenceException: Object reference not set to an instance of an object.
   at HospitalClient_App.Home.Home_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

如果我换行

 Process.Start("IExplore.exe", url); 

Process.Start("IExplore.exe","http://localhost:49916/Express/Login.aspx?yek@soh=6775228");

然后程序运行。

我的app.config如下:

<configuration>
    <appSettings>
        <add key="HK" value="PRO2"/>
        <add key="ClientSettingsProvider.ServiceUri" value=""/>
    </appSettings>
    <startup>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
</configuration>

我该如何解决这个问题?是什么导致了这个问题?

最佳答案

我的心理调试器看到潜在 NRE 的唯一地方是:

ConfigurationSettings.AppSettings["HK"].ToString()

(即 deprecated ,使用 ConfigurationManager.AppSettings[] )。

它在那里抛出的事实会告诉你没有带键的 Appsetting HK可以找到,导致ConfigurationSettings.AppSettings["HK"]返回 null ,导致null.ToString()抛出上述异常。

确保 <add key="HK" value="..." />在您相关配置的应用程序设置部分。

关于c# - System.Configuration.ConfigurationSettings.AppSettings 在生产中不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443079/

相关文章:

c# - 如何在 Dot Net Core 3.1 中使用 FromSqlInterpolated/Database.ExecuteSqlInterpolated 执行带有输出参数的存储过程?

C#:检查 StringBuffer 中有多少 UTF-8 编码字节的快速方法?

.net - SQL Server 2016 Express 是否支持始终加密?

c# - CoCreateInstance 失败,返回 0x80131500

asp.net-mvc - Windows 身份验证和 MVC : proper way to exclude individual file/route

visual-studio - 在调试过程中将转换后的Web.config与IIS Express一起使用

C# 检查小数是否有超过 3 位小数?

c# - 如何在 ASP.NET MVC 5 中为特定 Controller 指定路由选项

c# - 为什么Electron杀死带有冒号参数的Windows进程?

iis - 异常消息 : WebSockets is unsupported in the current application configuration