阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WCF中序列化DataTable出错

Posted on 2010-10-13 13:39  宽田  阅读(1495)  评论(1编辑  收藏  举报

  今天用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. ……"

  网上有人给出了解决方法,即设置DataTable的TableName属性即可。

 

样例代码:

  建立服务契约。契约中一个返加DataTable的方法,一个是返回DataSet的方法。

namespace NavigateService
{
    [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;
        }
    }
}