.net web service with datatable

smegghead
smegghead used Ask the Experts™
on
Hi

I've created a web-service which has a method which returns a DataSet, however, when this dataset arrives in the client machine I'm just accessing...

MyDataSet.Tables[0]

I've tried to shorten this by passing back the DataTable to the client, thus avoiding the bulky DataSet.

However, when I test this component, it now throws up the error

Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface

All the documentation seems to point to the DataTable being serializable / marshalled by value etc... but it just doesn't like it.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Yup, it should be ok! So you code must be not quite right. Trim it and post some so we can help...

Author

Commented:
I've just created a new web-service project and replaced the hello world example with...

            [WebMethod]
            public DataTable HelloWorld()
            {
                  return new DataTable("Test");
            }

This throws up the same error (it's not a compile error, it only occurs when it is run)

Server Error in '/foree' Application.
--------------------------------------------------------------------------------

Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.]
   System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, Boolean canBePrimitive, Boolean throwOnNoDefaultCtor, MemberInfo memberInfo)
   System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean throwOnDefaultCtor)
   System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source)
   System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)
   System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, XmlSchemaForm form)
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, XmlSchemaForm form)
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlSchemaForm form, Boolean repeats)
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType)
   System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, XmlSchemaForm form, Type choiceIdentifierType)
   System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers)
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement)

[InvalidOperationException: There was an error reflecting 'HelloWorldResult'.]
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement)
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement)
   System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, Boolean serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members, Boolean validate)
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs)

[InvalidOperationException: Method Service1.HelloWorld can not be reflected.]
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs)
   System.Web.Services.Description.SoapProtocolReflector.ReflectMethod()
   System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding)
   System.Web.Services.Description.ProtocolReflector.Reflect()
   System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url)
   System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri)
   System.Web.Services.Protocols.DocumentationServerProtocol.Initialize()
   System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

[InvalidOperationException: Unable to handle request.]
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)

[InvalidOperationException: Failed to handle request.]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, String path, String pathTranslated, Boolean useAppConfig)
   System.Web.MapHandlerExecutionStep.Execute()
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.0.3705.288; ASP.NET Version:1.0.3705.288
The datatable is not serializable - you have to pass a dataset.  Its an error in the documentation
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Sorry, I should've been more specific.  The DataTable is not INHERENTLY serializable.  You could write the serialization/deserialization code yourself, but its much easier just to put the DataTable into a DataSet and pass it that way.

Author

Commented:
re: "Its an error in the documentation"

is this 100% ???

I've searched the microsoft web-site and find no mention of this 'error'
I don't know what MS's "official" position is, but earlier this year I posted the same question to an MS newsgroup and got this response:

http://www.dotnet247.com/247reference/msgs/4/21203.aspx

Author

Commented:
Yeah, I think I read that post when searching for it, but was confused that the MS documentation says that it is serializiable.
A DataTable *is* in fact serializable, I have an example here that does it.  The problem seems to be that when the DataTable is part of a strongly-typed dataset it throws that error.  If the dataset is created at runtime, it can be serialized.  This is a snippet from the sample I mentioned (from ComponentOne C1.ZIP .Net Studio Serialization example, http://componentone.com/).

        Dim da As OleDbDataAdapter = New OleDbDataAdapter(rs, conn)
        Dim ds As DataSet = New DataSet()
        Try
            da.Fill(ds)
        Catch
            MessageBox.Show("Could not load data from " + MDBFILE + ".", "Error")
        End Try

        ' ds.Tables(0) is serializable!
Hi guys:

For what it is worth, I found this question because I had exactly the same problem.
Same reason (use of datatable instead of dataset for the same reasons as exposed here)
Changing to dataset solved the problem.

Thanks for pointinh me in the right direction!

Dabas

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial