473,435 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,435 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 4365
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 any data type and any method with pre-defined...
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 used the .net SDK to generate the class. I have...
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# and I can't figure out the correct way to do it. ...
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 code: DerivedClass myClass = new DerivedClass(5,...
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 client computer and execute it using remoting or...
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. For instance, online chats by language since July...
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 Clemens Vasters and I am a Program Manager on the...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
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. I am having a strange problem with std::string as...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.