ASP.NET 2.0 WebService中传递DataTable参考

None.gif   在2.0正式版发布之前,就满天的看到关于DataTable支持序列化的新特性宣传,满以为从此以后使用DataTable就和DataSet一样方便了,结果在应用项目的时候才发现并非那么回事。
None.gif  DataTable是支持序列化了,但是微软并没有把他做的特别方便,还需要我们自己来做一些工作之后才能够在WebService里面传递DataTable,否则在引用DataTable的时候会发现DataTable变成了一个什么Proxy类型。
None.gif  首先编写类DataTableSchemaImporterExtension,代码如下:
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Xml.Serialization.Advanced;
None.gif
using  System.Collections;
None.gif
using  System.Xml.Schema;
None.gif
using  System.Xml.Serialization;
None.gif
using  System.CodeDom;
None.gif
using  System.CodeDom.Compiler;
None.gif
using  System.Xml;
None.gif
using  System.Data;
None.gif
None.gif
namespace  Xrinehart.Tools.WebService.SchemaImporter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class DataTableSchemaImporterExtension : SchemaImporterExtension
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
// DataTableSchemaImporterExtension is used for WebServices, it is used to recognize the schema for DataTable within wsdl
InBlock.gif

InBlock.gif        Hashtable importedTypes 
= new Hashtable();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
public override string ImportSchemaType(string name, string schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            IList values 
= schemas.GetSchemas(schemaNamespace);
InBlock.gif
InBlock.gif            
if (values.Count != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
return null;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            XmlSchema schema 
= values[0as XmlSchema;
InBlock.gif
InBlock.gif            
if (schema == null)
InBlock.gif
InBlock.gif                
return null;
InBlock.gif
InBlock.gif            XmlSchemaType type 
= (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(name, schemaNamespace)];
InBlock.gif
InBlock.gif            
return ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (type == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
return null;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (importedTypes[type] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mainNamespace.Imports.Add(
new CodeNamespaceImport(typeof(DataSet).Namespace));
InBlock.gif
InBlock.gif                compileUnit.ReferencedAssemblies.Add(
"System.Data.dll");
InBlock.gif
InBlock.gif                
return (string)importedTypes[type];
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (!(context is XmlSchemaElement))
InBlock.gif
InBlock.gif                
return null;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
if (type is XmlSchemaComplexType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                XmlSchemaComplexType ct 
= (XmlSchemaComplexType)type;
InBlock.gif
InBlock.gif                
if (ct.Particle is XmlSchemaSequence)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    XmlSchemaObjectCollection items 
= ((XmlSchemaSequence)ct.Particle).Items;
InBlock.gif
InBlock.gif                    
if (items.Count == 2 && items[0is XmlSchemaAny && items[1is XmlSchemaAny)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        XmlSchemaAny any0 
= (XmlSchemaAny)items[0];
InBlock.gif
InBlock.gif                        XmlSchemaAny any1 
= (XmlSchemaAny)items[1];
InBlock.gif
InBlock.gif                        
if (any0.Namespace == XmlSchema.Namespace && any1.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1")
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif
InBlock.gif                            
string typeName = typeof(DataTable).FullName;
InBlock.gif
InBlock.gif                            importedTypes.Add(type, typeName);
InBlock.gif
InBlock.gif                            mainNamespace.Imports.Add(
new CodeNamespaceImport(typeof(DataTable).Namespace));
InBlock.gif
InBlock.gif                            compileUnit.ReferencedAssemblies.Add(
"System.Data.dll");
InBlock.gif
InBlock.gif                            
return typeName;
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return null;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif  为此类添加进一个项目中,并将此项目进行强命名后编译。
None.gif
None.gif  然后,把该Assembly程序集加入到GAC中。
None.gif
None.gif  最后修改本机的machine.config,代码如下:
None.gif      
< sectionGroup name = " system.xml.serialization "  type = " System.Xml.Serialization.Configuration.SerializationSectionGroup, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " >
None.gif      
< section name = " schemaImporterExtensions "  type = " System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "   />
None.gif      
< section name = " dateTimeSerialization "  type = " System.Xml.Serialization.Configuration.DateTimeSerializationSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "   />
None.gif      
< section name = " xmlSerializer "  type = " System.Xml.Serialization.Configuration.XmlSerializerSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "  requirePermission = " false "   />
None.gif      
</ sectionGroup >
None.gif 
None.gif 
None.gif 
None.gif 
None.gif  
< system.xml.serialization >
None.gif     
< schemaImporterExtensions >
None.gif            
< add name = " dataTableSchemaImporterExtension "  type = " Xrinehart.Tools.WebService.SchemaImporter.DataTableSchemaImporterExtension, Xrinehart.Tools.WebService.SchemaImporter,Version=1.0.0.0,Culture=neutral,PublicKeyToken=5a627ce15fb94702 "   />
None.gif    
</ schemaImporterExtensions >
None.gif 
</ system.xml.serialization >
None.gif
None.gif  完成以上的步骤后,再编译WebService,重新引用(或者更新Web引用),就可以正确的识别DataTable类型了。
None.gif  其实DataTable只实现了序列化,但WebService并不能自己反序列化为可识别的格式,所以需要自己手动增加。由此可以衍生为各种业务实体BussinessEntity类对象也可以通过以上方式实现直接传递。
None.gif
None.gif  希望对大家有所帮助。
None.gif
None.gif

转载于:https://www.cnblogs.com/fxwdl/archive/2007/02/27/657973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值