470,566 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

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

Hi all,

Hope I am posting this in the right forum.

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


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:


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
0 2485

Post your reply

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

Similar topics

9 posts views Thread by Arash Dejkam | last post: by
2 posts views Thread by jman | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.