【发布时间】:2014-09-02 20:37:35
【问题描述】:
情况(Windows 窗体): 我已经创建了一个应用程序,比如说一个药房应用程序,它添加了患者、药物、医生……这是一个 Winforms 应用程序。我可以毫无问题地添加患者...我有一个包含连接字符串的配置文件。我可以毫无问题地访问数据库。在页面加载时,Winform 会填充 4 个组合框(2 个字母状态、药物、医生、保险公司)。这是一个 N 层应用程序(UI、EntityObject (EO)、业务流程层 (BPL)、数据访问层 (DAL)、数据库 (DB))。 Winform 流程(WindowUI > BPL > DAL > DB)(这是 Intranet。
情况(Web 表单、服务) 我现在需要远程访问应用程序(Web 表单、Internet)...我在与窗口项目相同的解决方案中创建了一个 WCF 服务项目。我没有对 winform 应用程序的流程进行任何更改。该服务直接在 BPL 之后进行。从流中提出的 Web(WebUI > WCF 服务 > BPL > DAL > DB)
有一个实用程序类,其中包含一个 DB opener 方法,该方法将 SQLCommand 对象返回给 BPL。
问题: 当我尝试“测试”该服务时,我在 PharmUtil catch 语句中得到一个空引用,而我在运行 Windows 窗体时没有得到。我错过了什么……?
"Call to the BPL from the service:"
Namespace pharmapp
public DataSet StateDS()
{
return StateBPL.StateFillDS(); //returns the state dataset to the service consumer
}
"Calls the util class and the DAL"
Namespace pharmapp
public class StateBPL
{
Public static DataSet StateFillDS()
{
var cmdobj = new SQLCommand();
cndobj = PharmAppUtil.OpenDB(); //calls the utilities class
cmdobj.commandText = "spGetStates"; //adds stored procedure name
return StateDAL.StateDSFill(cmdobj); // call the Data access class and
//returns a dataset
}
}
> "Utilities calls returns a command object to the BPL minus the
> stored procedure name"
> Utilties Class:
Namespace pharmapp
> public class PharmAppUtil
> {
> SqlConnection conn = new SqlConnection();
> public static SqlCommand OpenDB()
> {
> SqlConnection conn = new SqlConnection();
> try
> {
> var connSettings = ConfigurationManager.ConnectionStrings;
> if (connSettings.Count < 1)
> {
> Debug.WriteLine("NO Connection string found.");
> return null;
> }
> else
> {
> conn.ConnectionString =
> ConfigurationManager.ConnectionStrings["PharmaConn"].ToString();
> conn.Open();
> SqlCommand cmd = new SqlCommand();
> cmd.Connection = conn;
> cmd.CommandType = CommandType.StoredProcedure;
> return cmd;
> }
> }
> catch (SqlException ex)
> {
> Debug.WriteLine(ex.Message);
> return null;
> }
> catch (NullReferenceException ex)
> {
> Debug.WriteLine(ex.Message);
> return null;
> }
> finally
> {
>
> }
> //return ;
> }
【问题讨论】:
-
当它在 pharmutil 类中命中时,它会下降到 nullref 捕获:conn.ConnectionString = > ConfigurationManager.ConnectionStrings["PharmaConn"].ToString();但是,当 Windows 窗体运行时...没有问题...仅来自服务调用...仅供参考,我目前正在使用 WCF 测试客户端进行测试...
标签: c# .net winforms web-services wcf