472,325 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

InvalidCaseException in Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write5_ArrayOfAnyType

Help!

Below is some code for a Web Service which has two methods, First() and
Second(). However, when the code is run, whichever of the two methods
appears second in the source fails with "System.InvalidCastException:
Specified cast is not valid" in
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write5
_ArrayOfAnyType(Object o) of the temporary C# DLL that Web Service
processing creates when serializing data.

My theory is that because both methods are serialized as ArrayOfAnyType,
there is only one WriteN_ArrayOfAnyType method created in the temporary C#
DLL, which treats all objects therein as the type of objects in the first
method in the Web Service, hence the invalid cast when attempting to
serialize the data from the second.

Can anyone tell me what directives I can put where, to communicate to the
Soap/XML system that the two methods, "First()" and "Second()", in the code
below return different types of data, which will, I hope, prevent this
invalid cast exception.

Many thanks
Eric
------------------------------- the code in
question -------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Web.Services;

namespace NewServe {

// ------------------------------------------------------------------
public class FirstClass {

public string one = "";
public string two = "";

public FirstClass() {
one = "";
two = "";
}

public FirstClass(string one, string two) {

this.one = one;
this.two = two;
}
}
// ------------------------------------------------------------------
public class SecondClass {

public string proto = "";
public string steno = "";
public string trito = "";

public SecondClass() {
proto = "";
steno = "";
trito = "";
}

public SecondClass(string one, string two, string three) {

this.proto = one;
this.steno = two;
this.trito = three;
}
}
// ------------------------------------------------------------------
public class Seconds : IEnumerable, IEnumerator {

private ArrayList contents = new ArrayList();
private int position = -1;

public Seconds() {
if (this.contents == null) {
this.contents = new ArrayList();
}
} // public secondss.

public void Add ( object o) {
this.contents.Add(o);
}

public SecondClass this[int index] {
get {
return (SecondClass) this.contents[index];
}
}
#region IEnumerable Members
public IEnumerator GetEnumerator() {
this.Reset();
return (IEnumerator) this;
}
#endregion
#region IEnumerator Members
public void Reset() {
this.position = -1;
}
public object Current {
get {
return this.contents[this.position];
}
}
public bool MoveNext() {
if (this.position < (this.contents.Count - 1)) {
this.position++;
return true;
} else {
return false;
}
}
#endregion
}

// ------------------------------------------------------------------
public class Service1 : System.Web.Services.WebService {

// ----------------------------------------------------------
public Service1() {
InitializeComponent();
} // public Service1
#region Component Designer generated code
private IContainer components = null;
private void InitializeComponent()
{
}
// ----------------------------------------------------------
protected override void Dispose( bool disposing ) {
if(disposing && components != null) {
components.Dispose();
} // if (disposing...
base.Dispose(disposing);
} // protected override void Dispose...
#endregion

// ----------------------------------------------------------
[WebMethod ]
[XmlInclude(typeof(SecondClass))]
[XmlInclude(typeof(Seconds))]
public Seconds Second() {

Seconds rv = new Seconds();
rv.Add(new SecondClass("one", "two", "three"));
rv.Add(new SecondClass("one", "two", "three"));
return rv;

} // public Seconds Second...

// ----------------------------------------------------------
[WebMethod]
[XmlInclude(typeof(FirstClass))]
public ArrayList First() {

ArrayList rv = new ArrayList();
rv.Add(new FirstClass("One", "two"));
rv.Add(new FirstClass("three", "four"));
return rv;

} // public ArrayList First()...

} // class Service1...

} // namespace NewServe...
Nov 21 '05 #1
1 4304
I may have developed a work-around for this problem (I haven't given it a
comprehensive beating yet).

I have contrived to invent two new classes, FirstHolder and SecondHolder.
Inside FirstHolder is an ArrayList of FirstClass objects. Inside
SecondHolder is a Seconds collection of SecondClass objects. The Web
methods now return return the holding objects, not the ArrayList or Seconds
collection directly. Now, the HTTP test page seems to work for both
methods, regardless of order.

Am I sacreficing the high moral ground with this circumvention?
"Eric Porter" <bo********@hotmail.com> wrote in message
news:cc*******************@news.demon.co.uk...
Help!

Below is some code for a Web Service which has two methods, First() and
Second(). However, when the code is run, whichever of the two methods
appears second in the source fails with "System.InvalidCastException:
Specified cast is not valid" in
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write5 _ArrayOfAnyType(Object o) of the temporary C# DLL that Web Service
processing creates when serializing data.

My theory is that because both methods are serialized as ArrayOfAnyType,
there is only one WriteN_ArrayOfAnyType method created in the temporary C#
DLL, which treats all objects therein as the type of objects in the first
method in the Web Service, hence the invalid cast when attempting to
serialize the data from the second.

Can anyone tell me what directives I can put where, to communicate to the
Soap/XML system that the two methods, "First()" and "Second()", in the code below return different types of data, which will, I hope, prevent this
invalid cast exception.

Many thanks
Eric
------------------------------- the code in
question -------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Web.Services;

namespace NewServe {

// ------------------------------------------------------------------
public class FirstClass {

public string one = "";
public string two = "";

public FirstClass() {
one = "";
two = "";
}

public FirstClass(string one, string two) {

this.one = one;
this.two = two;
}
}
// ------------------------------------------------------------------
public class SecondClass {

public string proto = "";
public string steno = "";
public string trito = "";

public SecondClass() {
proto = "";
steno = "";
trito = "";
}

public SecondClass(string one, string two, string three) {

this.proto = one;
this.steno = two;
this.trito = three;
}
}
// ------------------------------------------------------------------
public class Seconds : IEnumerable, IEnumerator {

private ArrayList contents = new ArrayList();
private int position = -1;

public Seconds() {
if (this.contents == null) {
this.contents = new ArrayList();
}
} // public secondss.

public void Add ( object o) {
this.contents.Add(o);
}

public SecondClass this[int index] {
get {
return (SecondClass) this.contents[index];
}
}
#region IEnumerable Members
public IEnumerator GetEnumerator() {
this.Reset();
return (IEnumerator) this;
}
#endregion
#region IEnumerator Members
public void Reset() {
this.position = -1;
}
public object Current {
get {
return this.contents[this.position];
}
}
public bool MoveNext() {
if (this.position < (this.contents.Count - 1)) {
this.position++;
return true;
} else {
return false;
}
}
#endregion
}

// ------------------------------------------------------------------
public class Service1 : System.Web.Services.WebService {

// ----------------------------------------------------------
public Service1() {
InitializeComponent();
} // public Service1
#region Component Designer generated code
private IContainer components = null;
private void InitializeComponent()
{
}
// ----------------------------------------------------------
protected override void Dispose( bool disposing ) {
if(disposing && components != null) {
components.Dispose();
} // if (disposing...
base.Dispose(disposing);
} // protected override void Dispose...
#endregion

// ----------------------------------------------------------
[WebMethod ]
[XmlInclude(typeof(SecondClass))]
[XmlInclude(typeof(Seconds))]
public Seconds Second() {

Seconds rv = new Seconds();
rv.Add(new SecondClass("one", "two", "three"));
rv.Add(new SecondClass("one", "two", "three"));
return rv;

} // public Seconds Second...

// ----------------------------------------------------------
[WebMethod]
[XmlInclude(typeof(FirstClass))]
public ArrayList First() {

ArrayList rv = new ArrayList();
rv.Add(new FirstClass("One", "two"));
rv.Add(new FirstClass("three", "four"));
return rv;

} // public ArrayList First()...

} // class Service1...

} // namespace NewServe...

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c#...
3
by: Noël Danjou | last post by:
VS.NET 2003, C# and .NET Framework 1.1. Hello, I am trying to serialize a small sample class using the XmlSerializer class. ** Here is the...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the...
86
by: Michael Adams | last post by:
I don't know if you have noticed, but it seems like Microsoft is losing interest in C#, and putting their energy into Visual Basic.NET instead. ...
2
by: Clemens Vasters | last post by:
"Is anybody from Microsoft reading this?" Well ... actually .... no .... welll ... maybe if you're really lucky. Let me explain ... My name is...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject :...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. ...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.