470,568 Members | 1,535 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 4249
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Ben | last post: by
1 post views Thread by andrewcw | last post: by
3 posts views Thread by Aaron Clamage | last post: by
3 posts views Thread by NoŽl Danjou | last post: by
6 posts views Thread by Uttam | last post: by
86 posts views Thread by Michael Adams | last post: by
2 posts views Thread by Clemens Vasters | 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.