cannot be serialized : does not have a default public constructor

J

John Grandy

make a call to XML Web Service WebMethod ... returns object[] myArray with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}
 
D

Dale Preston

Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}
 
J

John Grandy

Hi Dale, and thanks for the response.

Object[] array = null;
ListItem listItem = null;
StringKeyStringValue arrayElement = null;
WebReference1.WebService1 webService = null;

webService = new WebReference1.WebService1();

array = webService.Method1();
for (int i=0; i < array.Length; i++)
{
arrayElement = (StringKeyStringValue) array;
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}



Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}

 
J

John Grandy

Hmmmm ... I read your article, and I added the following line right above
the definition of my Web Method

[XmlInclude(typeof(StringKeyStringValue))]


However, on the ASP.NET Web App side, the call to the Web Method returns an
array of XMLAttributes, but on attempting to cast an XMLAttribute to my
StringKeyStringValue type a runtime error is thrown.

[WebMethod(Description="GetQuestionAnswers",EnableSession=false)]
[XmlInclude(typeof(StringKeyStringValue))]
public ArrayList Method1()
{

ArrayList al = null;
NameValueCollection nvc = null;

try
{
nvc = BusinessTier.Method1();
al = new ArrayList();
for (int i=0; i < nvc.Count; i++)
{
StringKeyStringValue sksv = new
StringKeyStringValue(nvc.GetKey(i),nvc.GetValues(i)[0]);
al.Add(sksv);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Source + " " + ex.Message);
al = new ArrayList();
}
finally {}
return al;
}










Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}

 
J

John Grandy

Note that my References.cs file does not contain any mention of my custom
type.

I assume this is because my custom type is not *directly* returned by any
web-method.

It is only indirectly returned : one of my web-methods returns an ArrayList
that contains elements of my custom type.


Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}

 
D

Dale Preston

Actually, your web method can't return an array list. It will cast the
arraylist to an array of objects. Make sure you cast the object back to the
original type on the client side. To make the types the same, versus
identical but different, create the type on the client by referencing the
type on the web service. My first article demonstrates that.

I am still going to look at your code from your first post of today.

Dale

John Grandy said:
Note that my References.cs file does not contain any mention of my custom
type.

I assume this is because my custom type is not *directly* returned by any
web-method.

It is only indirectly returned : one of my web-methods returns an ArrayList
that contains elements of my custom type.


Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."



using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}


 
D

Dale Preston

As I mentioned in the response to your third post today, you have to make
your client and web service talk the same language about
StringKeyStringValue objects. What you have is like identical twin objects.
Exactly alike but not the same object. On the client side, change the
declaration of arrayElement like this:

WebReference1.WebService1.StringKeyStringValue arrayElement = null;

There are comments in my second article about how to use the local copy of a
class by modifying the reference.cs. If your class is not listed in
Reference.cs I assume it is because your class is either not public or not
returned by a web method. So create a web method that returns a
StringKeyStringValue object.

HTH

Dale Preston



John Grandy said:
Hi Dale, and thanks for the response.

Object[] array = null;
ListItem listItem = null;
StringKeyStringValue arrayElement = null;
WebReference1.WebService1 webService = null;

webService = new WebReference1.WebService1();

array = webService.Method1();
for (int i=0; i < array.Length; i++)
{
arrayElement = (StringKeyStringValue) array;
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}



Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

John Grandy said:
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public constructor."



using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}


 
J

John Grandy

Ok, I had actually previously figured out that I had to write a simple web
method that returns an instance of the StringKeyStringValue type in order
for a corresponding stub definition to be included in References.cs

In the webservices newsgroup, it was suggested to me that I modify
References.cs to delete this stub definition and include the following line
of code:

using UtilityStorageLibrary;

"UtilityStorageLibrary" is the namespace used in my custom class library
that contains the definition of the StringKeyStringValue type

The logic is that any StringKeyStringValue objects returned by web-methods
will be received by the proxy class and on deserialization will be converted
into instances of the local StringKeyStringValue type.

I made these changes, and I no longer receive any compile-time or run-time
errors ...

However,

Object[] array = WebReference1.WebService1.WebMethod1();
StringKeyStringValue sksv = (StringKeyStringValue) array[0];
String s1 = sksv.Key;
String s2 = sksv.Value;

reveals that sksv.Key and sksv.Value are both null

So, apparently sending across an array of instances of a custom type is a
bit more tricky than sending across a single instance of a custom type.


Dale Preston said:
As I mentioned in the response to your third post today, you have to make
your client and web service talk the same language about
StringKeyStringValue objects. What you have is like identical twin
objects.
Exactly alike but not the same object. On the client side, change the
declaration of arrayElement like this:

WebReference1.WebService1.StringKeyStringValue arrayElement = null;

There are comments in my second article about how to use the local copy of
a
class by modifying the reference.cs. If your class is not listed in
Reference.cs I assume it is because your class is either not public or not
returned by a web method. So create a web method that returns a
StringKeyStringValue object.

HTH

Dale Preston



John Grandy said:
Hi Dale, and thanks for the response.

Object[] array = null;
ListItem listItem = null;
StringKeyStringValue arrayElement = null;
WebReference1.WebService1 webService = null;

webService = new WebReference1.WebService1();

array = webService.Method1();
for (int i=0; i < array.Length; i++)
{
arrayElement = (StringKeyStringValue) array;
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}



Dale Preston said:
Can you show the code that calls the web service and the code that access
the array afterwards?

In the mean time, here's my short tutorial on returning objects from
web
services and solutions to some of the problems that come up doing so.

http://www.dalepreston.com/Blog/Archives/2005_02_06_Archive.html and

http://www.dalepreston.com/Blog/Archives/2005_02_24_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
make a call to XML Web Service WebMethod ... returns object[] myArray
with
no error ...

myArray[] contains objects of type StringKeyStringValue

runtime error occurs on accessing properties of myArray

<<<
ex.Message "Server was unable to process request. --> There was an error
generating the XML document. -->
UtilityStorageLibrary.StringKeyStringValue
cannot be serialized because it does not have a default public
constructor."



using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace UtilityStorageLibrary
{
[Serializable()]
public class StringKeyStringValue
{
private String stringKey = null;
private String stringValue = null;

public StringKeyStringValue()
{
}

public StringKeyStringValue(String stringKey, String stringValue)
{
this.stringKey = stringKey;
this.stringValue = stringValue;
}

public string Key
{
get
{
return stringKey;
}
}

public string Value
{
get
{
return stringValue;
}
}

}
}


 

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

Top