【发布时间】:2014-06-03 10:16:43
【问题描述】:
我有一个 WCF 服务,它返回一个 DataSet 并且它工作得很好。我实际上想返回一个 DataTable 而不是 DataSet。当我更改代码以返回 DataTable 并启动服务(自托管在控制台应用程序上)时,它运行并且我可以看到服务的 WSDL 文档。但是,在使用该服务时,我收到一条错误消息:
An error occurred while receiving the HTTP response (along with the service url).
This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server(possibly due to the service shutting down).
See server logs for more details.
现在,我在某处读到其他开发人员在返回 DataTable 时遇到了类似的问题,在返回 DataSet 时解决了这个问题。我做了同样的事情并且能够解决问题。
我的问题:
但是,为什么在返回 DataTable 时会出现这样的问题让我很感兴趣? 是否有一些与我没有看到的基础知识相关的重要内容?
WCF 服务(with DataTable)有如下代码:
public DataTable ValidateLogin(string UserName, string Password)
{
DataTable dt = new DataTable();
LINQHelperDataContext valLog = new LINQHelperDataContext();
var result = from user in valLog.GetTable<USER_DETAILS_T>()
join acctype in valLog.GetTable<Account_type_t>()
on user.UserAccountType equals acctype.AccountTypeId
where user.UserId == UserName
where user.UserPassword == Password
select new {user.UserFirstName,acctype.AccountTypeName };
// select user.UserFirstName;
int rowCount=result.Count();
dt.Columns.Add();
dt.Columns.Add();
if (rowCount == 1)
{
foreach (var res in result)
{
dt.Rows.Add(res.UserFirstName,res.AccountTypeName);
break;
}
return dt;
}
else
{
dt.Rows.Add("Not Found", "Not Found");
return dt;
}
}
}
以下是代码(with DataSet):
public DataSet ValidateLogin(string UserName, string Password)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
LINQHelperDataContext valLog = new LINQHelperDataContext();
var result = from user in valLog.GetTable<USER_DETAILS_T>()
join acctype in valLog.GetTable<Account_type_t>()
on user.UserAccountType equals acctype.AccountTypeId
where user.UserId == UserName
where user.UserPassword == Password
select new {user.UserFirstName,acctype.AccountTypeName };
// select user.UserFirstName;
int rowCount=result.Count();
dt.Columns.Add();
dt.Columns.Add();
if (rowCount == 1)
{
foreach (var res in result)
{
dt.Rows.Add(res.UserFirstName,res.AccountTypeName);
break;
}
ds.Tables.Add(dt);
return ds;
}
else
{
dt.Rows.Add("Not Found", "Not Found");
ds.Tables.Add(dt);
return ds;
}
}
}
【问题讨论】:
-
尝试为数据表设置名称,如下所述stackoverflow.com/questions/12702/…
标签: .net wcf entity-framework datatable dataset