我有一个 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/