ASP.net 2.0 WebService pass the DataTable reference

Source: Internet
Author: User
Tags count new features reference serialization
asp.net|web| Reference

Prior to the release of the 2.0 official release, the new features of the DataTable support serialization were seen all over the sky, and it was thought that using the DataTable from then on would be as convenient as the dataset, and the result would be that it was not the case when the project was applied.
The DataTable supports serialization, but Microsoft does not make it particularly convenient, and it requires us to do some work before we can pass the DataTable in WebService. Otherwise, when referencing the DataTable, you will find that the DataTable becomes a type of proxy.
First write the class datatableschemaimporterextension, the code is as follows:
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 are 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;

}

}


}
Add this class to a project, and then compile the project after it has been strongly named.

Then, add the Assembly assembly to the GAC.

Finally modify the machine.config of this machine, the code is as follows:
<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>

After you complete the steps above, compile WebService, and then dereference (or update the Web reference) to correctly identify the DataTable type.
In fact, the DataTable only implements serialization, but WebService cannot deserialize itself into a recognizable format, so it needs to be manually incremented. This can be derived into various business entities bussinessentity class objects can also be achieved through the above way direct transmission.

Hope to help you.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.