由于项目升级到.NET3.5框架下,要在整体实现方法不变的情况下,处理WebService中序列化datatable对象。现在需要解决在.net3.5框架下序列化出来得数据跟在.net2.0框架下序列化出来得数据格式需要一致。该问题没有找到合适的解决方案。欢迎大家讨论。谢谢!
在.NET3.5框架下,WebService中序列化datatable对象。序列化后的数据中出现多了个“d”属性。如下:
{"d":" {\"__type\":\"SFCity.Entities.CBS_Employee, SFCity.Entities, Version=3.2.13.7, Culture=neutral, PublicKeyToken=null\",\"ID\":\"0bf732c7-d8c2-4955-9262-d3c87fad61b1\", \"FEmployeeID\":\"00002\",\"FName\":\"小李\",\"FBranch\":\"AJ\",\"FStation \":\"业务员\",\"FDuty\":\"部门经理\",\"FPlace\":\"福州\",\"FEmail\":\"\", \"FProvince\":\"14:::湖北省\",\"FCity\":\"161:::武汉\",\"FAddress\":\"hhhhhh \"}"}
.在.net2.0时,WebService中序列化datatable对象。序列化后的数据为:"{\"__type\": \"SFCity.Entities.CBS_Employee, SFCity.Entities, Version=3.2.13.7, Culture=neutral, PublicKeyToken=null\",\"ID\":\"0bf732c7-d8c2-4955-9262-d3c87fad61b1\", \"FEmployeeID\":\"00002\",\"FName\":\"小李\",\"FBranch\":\"AJ\",\"FStation \":\"业务员\",\"FDuty\":\"部门经理\",\"FPlace\":\"福州\",\"FEmail\":\"\", \"FProvince\":\"14:::湖北省\",\"FCity\":\"161:::武汉\",\"FAddress\":\"hhhhhh \"}"
在web.config配置文件如下:
Code
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2000000">
<add name="DataTableConverter" type="Ext.Converters.DataTableConverter,Ext"/>
</converters>
</jsonSerialization>
<profileService enabled="false" readAccessProperties="propertyname1,propertyname2" writeAccessProperties="propertyname1,propertyname2"/>
</webServices>
<scriptResourceHandler enableCompression="true" enableCaching="true"/>
</scripting>
</system.web.extensions>
在DataTableConverter类源码如下:
Code
public class DataTableConverter : JavaScriptConverter
{
public DataTableConverter()
{
this._supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(DataTable) });
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotSupportedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataTable table = obj as DataTable;
if (table == null)
{
throw new ArgumentException("obj");
}
IDictionary<string, object> dictionary = new Dictionary<string, object>(2);
//如果需要分页,则把总行数保存到这个扩展属性中。
if (table.ExtendedProperties["TotalCount"] != null)
{
dictionary["TitleCount"] = int.Parse(table.ExtendedProperties["TotalCount"].ToString());
}
else
{
dictionary["TitleCount"] = table.Rows.Count;
}
if (table.Rows.Count > 0)
{
DataRow[] rowArray = new DataRow[table.Rows.Count];
for (int j = 0; j < rowArray.Length; j++)
{
rowArray[j] = table.Rows[j];
}
dictionary["Rows"] = rowArray;
return dictionary;
}
dictionary["Rows"] = new string[0];
return dictionary;
}
private ReadOnlyCollection<Type> _supportedTypes;
public override IEnumerable<Type> SupportedTypes
{
get { return this._supportedTypes; }
}
}
在客户端是用EXTJS2.2版本实现的如下:
Code
SFCity.IMManageGrid = function(config){
var store = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({url:'Services/IMLog.asmx/MyIMLog',isWebService:true}),
reader : new Ext.data.JsonReader({
root:'Rows',
totalProperty:'TotalCount',
fields:['FID','FIpAddress','FUserName','FSource','FSalesman','FPostDate','FSign']
}),
listeners : {beforeload : this.onBeforeLoad,scope : this},
sortInfo:{field:'FPostDate',direction:'DESC'},
remoteSort:true
});
var sm = new Ext.grid.CheckboxSelectionModel();
var dataColumns = new Ext.grid.ColumnModel([
sm,
{header: "IP地址",dataIndex:'FIpAddress',sortable: true,width:130},
{header: "会员ID",dataIndex:'FUserName',sortable: true,width:130},
{header: "来源",dataIndex:'FSource',sortable: true,width:130},
{header: "时间",dataIndex:'FPostDate',renderer:Ext.util.Format.dateTime, sortable: true,align:'center',width:150},
{header: "重要",dataIndex:'FSign', sortable: true,align:'center',width:120,renderer:function(value){return value?'是':'否';}}
]);
this.pagingBar= new Ext.PagingToolbar({
pageSize: 25,
store: store,
displayInfo: true,
autoShow : true,
displayMsg:'当前显示 {0} - {1} 条记录/共 {2} 条记录',
refreshText:'刷新',
beforePageText:'第',
afterPageText:'页/共 {0} 页',
firstText:'第一页',
lastText:'最后一页',
nextText:'下一页',
prevText:'上一页',
emptyMsg:"暂无记录"
});
//
config = config||{};
config = Ext.apply(config,{
store:store,
cm:dataColumns,
sm:sm,
loadMask:{msg:'正在加载数据中,请稍候……'},
title:'IM聊天记录',
frame:true,
trackMouseOver:true,
listeners :{rowdblclick : this.ondblClick ,scope : this},
bbar: this.pagingBar
});
this.config = config;
SFCity.IMManageGrid.superclass.constructor.call(this, config);
this.refresh();
}
Ext.extend(SFCity.IMManageGrid, Ext.grid.GridPanel, {
doQuery : function(){
this.refresh();
},
refresh : function(){
this.pagingBar.cursor = 0;
this.getStore().load({params:{start:this.pagingBar.cursor, limit:this.pagingBar.pageSize}});
}
});