By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,919 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,919 IT Pros & Developers. It's quick & easy.

Object "type not expected" when using serialization in web services

P: 1
Hi all,

Hope I am posting this in the right forum.

I seem to be having a problem when using serialization and web services.

<b>Scenario:</b>

I have built a web service which includes a multitude of web methods. All the web methods have various parameters, but one parameter which they all have in common is an 'object' parameter. This is due to the fact that the web service works differently according to the type of object being passed into it.

Now as for the objects, I have constructed a set of custom classes which satisfy my need. For example's sake, let us say we have:

Teacher class
Student Class
LectureRoom Class
Lecture Class

We will call these the "School Package classes"

I am then wrapping these classes within other custom classes which I use not only to transport my objects around but also to keep track of statuses etc. Example:

Request Class
Response Class

The school package classes would be serialized and saved within the request or response class. So effectively, the objects which the web service really sees are the request and response classes which will in turn have the school package classes inside of them.

<b>The Problem:</b>

The problem is found when a school package object is passed within the response and request object. On deserialisation or transportation of these objects, the web service always throws an exception as follows:

{"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type <CLASSNAME> was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

From what I could gather and the research I have done, the problem lies with the web service not knowing of the object types at compile time.

<b>Make Shift Solution:</b>
For a quick and dirty solution I created methods within the web service which use the object types needed. For Example:

public void Junk(Student myStudent){}

This seemed to have worked, but is not what I want, as with about 40 custom classes, this is both horrible coding and very tedious.

I have also tried researching the xmlinclude and soapinclude found in the System.Xml.Serialization library, but dont really know how to use these. I have tried adding them above the web service class declaration and the web methods which use the objects in this format:

[XmlInclude(typeof(Student))]

But yet to no avail. I have officially run out of ideas and would greatly appreciate anyone who has experience or ideas regarding this problem.

Thank you very much in advance for your time and excuse the length of the post.
Apr 22 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.