首页 > 编程 > .NET > 正文

ASP.NET 2.0 WebService中传递DataTable参考

2024-07-10 13:09:50
字体:
来源:转载
供稿:网友

  在2.0正式版发布之前,就满天的看到关于datatable支持序列化的新特性宣传,满以为从此以后使用datatable就和dataset一样方便了,结果在应用项目的时候才发现并非那么回事。
  datatable是支持序列化了,但是微软并没有把他做的特别方便,还需要我们自己来做一些工作之后才能够在webservice里面传递datatable,否则在引用datatable的时候会发现datatable变成了一个什么proxy类型。
  首先编写类datatableschemaimporterextension,代码如下:
using system;
using system.collections.generic;
using system.text;
using system.xml.serialization.advanced;
using system.collections;
using system.xml.schema;
using system.xml.serialization;
using system.codedom;
using system.codedom.compiler;
using system.xml;
using system.data;

namespace xrinehart.tools.webservice.schemaimporter
{
    class datatableschemaimporterextension : schemaimporterextension
    {

        // datatableschemaimporterextension is used for webservices, it is used to recognize the schema for datatable within wsdl

        hashtable importedtypes = new hashtable();

        public override string importschematype(string name, string schemanamespace, xmlschemaobject context, xmlschemas schemas, xmlschemaimporter importer, codecompileunit compileunit, codenamespace mainnamespace, codegenerationoptions options, codedomprovider codeprovider)
        {

            ilist values = schemas.getschemas(schemanamespace);

            if (values.count != 1)
            {

                return null;

            }

            xmlschema schema = values[0] as xmlschema;

            if (schema == null)

                return null;

            xmlschematype type = (xmlschematype)schema.schematypes[new xmlqualifiedname(name, schemanamespace)];

            return importschematype(type, context, schemas, importer, compileunit, mainnamespace, options, codeprovider);

        }

        public override string importschematype(xmlschematype type, xmlschemaobject context, xmlschemas schemas, xmlschemaimporter importer, codecompileunit compileunit, codenamespace mainnamespace, codegenerationoptions options, codedomprovider codeprovider)
        {

            if (type == null)
            {

                return null;

            }

            if (importedtypes[type] != null)
            {

                mainnamespace.imports.add(new codenamespaceimport(typeof(dataset).namespace));

                compileunit.referencedassemblies.add("system.data.dll");

                return (string)importedtypes[type];

            }

            if (!(context is xmlschemaelement))

                return null;

            if (type is xmlschemacomplextype)
            {

                xmlschemacomplextype ct = (xmlschemacomplextype)type;

                if (ct.particle is xmlschemasequence)
                {

                    xmlschemaobjectcollection items = ((xmlschemasequence)ct.particle).items;

                    if (items.count == 2 && items[0] is xmlschemaany && items[1] is xmlschemaany)
                    {

                        xmlschemaany any0 = (xmlschemaany)items[0];

                        xmlschemaany any1 = (xmlschemaany)items[1];

                        if (any0.namespace == xmlschema.namespace && any1.namespace == "urn:schemas-microsoft-com:xml-diffgram-v1")
                        {

                            string typename = typeof(datatable).fullname;

                            importedtypes.add(type, typename);

                            mainnamespace.imports.add(new codenamespaceimport(typeof(datatable).namespace));

                            compileunit.referencedassemblies.add("system.data.dll");

                            return typename;

                        }

                    }

                }

            }

            return null;

        }

    }


}
  为此类添加进一个项目中,并将此项目进行强命名后编译。

  然后,把该assembly程序集加入到gac中。

  最后修改本机的machine.config,代码如下:
      <sectiongroup name="system.xml.serialization" type="system.xml.serialization.configuration.serializationsectiongroup, system.xml, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089">
      <section name="schemaimporterextensions" type="system.xml.serialization.configuration.schemaimporterextensionssection, system.xml, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" />
      <section name="datetimeserialization" type="system.xml.serialization.configuration.datetimeserializationsection, system.xml, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" />
      <section name="xmlserializer" type="system.xml.serialization.configuration.xmlserializersection, system.xml, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" />
      </sectiongroup> 
  <system.xml.serialization>
     <schemaimporterextensions>
            <add name="datatableschemaimporterextension" type="xrinehart.tools.webservice.schemaimporter.datatableschemaimporterextension, xrinehart.tools.webservice.schemaimporter,version=1.0.0.0,culture=neutral,publickeytoken=5a627ce15fb94702" />
    </schemaimporterextensions>
 </system.xml.serialization>

  完成以上的步骤后,再编译webservice,重新引用(或者更新web引用),就可以正确的识别datatable类型了。
  其实datatable只实现了序列化,但webservice并不能自己反序列化为可识别的格式,所以需要自己手动增加。由此可以衍生为各种业务实体bussinessentity类对象也可以通过以上方式实现直接传递。

  希望对大家有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表