473,382 Members | 1,733 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,382 software developers and data experts.

cannot be serialized : does not have a default public constructor

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[i]

<<<
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;
}
}

}
}


Nov 17 '05 #1
7 9276
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}

Nov 17 '05 #2
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[i];
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}

"Dale Preston" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}


Nov 17 '05 #3
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",EnableS ession=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" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}


Nov 17 '05 #4
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" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}


Nov 17 '05 #5
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" <johnagrandy-at-yahoo-dot-com> wrote in message
news:ub**************@TK2MSFTNGP10.phx.gbl...
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" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}



Nov 17 '05 #6
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" <johnagrandy-at-yahoo-dot-com> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
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[i];
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}

"Dale Preston" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
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/Arch...6_Archive.html and

http://www.dalepreston.com/Blog/Arch...4_Archive.html.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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[i]

<<<
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;
}
}

}
}



Nov 17 '05 #7
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" <da******@nospam.nospam> wrote in message
news:Ob**************@TK2MSFTNGP14.phx.gbl...
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" <johnagrandy-at-yahoo-dot-com> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
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[i];
listItem = new ListItem();
listItem.Value = arrayElement.Key;
listItem.Text = arrayElement.Value;
RadioButtonList1.Items.Add(listItem);
}

"Dale Preston" <da******@nospam.nospam> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
> 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/Arch...6_Archive.html and
>
> http://www.dalepreston.com/Blog/Arch...4_Archive.html.
>
> HTH
>
> Dale Preston
> MCAD, MCDBA, MCSE
>
> "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> news:OO**************@TK2MSFTNGP09.phx.gbl...
>> 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[i]
>>
>> <<<
>> 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;
>> }
>> }
>>
>> }
>> }
>>
>>
>>
>>
>
>



Nov 17 '05 #8

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

Similar topics

2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
7
by: Wayne Brantley | last post by:
I have found what appears to be an error in streaming with Datasets. It causes an error of 'Cannot find relation 0' when recreating the dataset from a stream. Here is how you reproduce it. ...
7
by: Ant | last post by:
Hi, I'm using the Random class to return 5 random numbers which then are added to a string. When I do it in a controls event, such as button_ click, the numbers are random as expected, but when...
4
by: jdhavo | last post by:
Just a quick question. Does this mean that an XmlTextWriter cannot be used in a webservice? Is there some special coding required to use this class?
12
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.