今天用wcf传送datatable时出现下列错误。我估计是序列化的问题。
"An error occurred while receiving the HTTP response to ****.svc.
This could be due to the service endpoint binding not using the HTTP protocol. ……"
This could be due to the service endpoint binding not using the HTTP protocol. ……"
网上有人给出了解决方法,即设置DataTable的TableName属性即可。
样例代码:
建立服务契约。契约中一个返加DataTable的方法,一个是返回DataSet的方法。
namespace NavigateService
{
[ServiceContract]
public interface INavigateService
{
[OperationContract]
DataTable GetTableAllData(string tableName);
[OperationContract]
DataSet GetDataSet(List<DataSetObject> tables);
}
}
{
[ServiceContract]
public interface INavigateService
{
[OperationContract]
DataTable GetTableAllData(string tableName);
[OperationContract]
DataSet GetDataSet(List<DataSetObject> tables);
}
}
实现契约,MsSql类是自己建立的一个类,用于取数据。
注意:在返回DataTable的GetTableAllData方法中, tableResult设置了TableName属性。这样Wcf序列化时就不会出错。
而返回DataSet的方法不需要作任何设置。
using System.Data;
using DL;
namespace NavigateService
{
public class NavigateService : INavigateService
{
MsSql msSql = null;
public NavigateService()
{
msSql = new MsSql();
}
public DataTable GetTableAllData(string tableName)
{
DataTable tableResult = null;
tableResult = msSql.GetTableAllData(tableName);
tableResult.TableName = "navigateData";
return tableResult;
}
public DataSet GetDataSet(List<DataSetObject> tables)
{
DataSet resultDataSet = new DataSet();
resultDataSet = msSql.GetDataSet(tables);
return resultDataSet;
}
}
}
using DL;
namespace NavigateService
{
public class NavigateService : INavigateService
{
MsSql msSql = null;
public NavigateService()
{
msSql = new MsSql();
}
public DataTable GetTableAllData(string tableName)
{
DataTable tableResult = null;
tableResult = msSql.GetTableAllData(tableName);
tableResult.TableName = "navigateData";
return tableResult;
}
public DataSet GetDataSet(List<DataSetObject> tables)
{
DataSet resultDataSet = new DataSet();
resultDataSet = msSql.GetDataSet(tables);
return resultDataSet;
}
}
}