InvalidCaseException in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5

E

Eric Porter

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.XmlSerializationWriter1.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...
 
E

Eric Porter

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 said:
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.XmlSerializationWriter1.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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top