PC Review


Reply
Thread Tools Rate Thread

cannot be serialized : does not have a default public constructor

 
 
John Grandy
Guest
Posts: n/a
 
      23rd Mar 2005
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;
}
}

}
}




 
Reply With Quote
 
 
 
 
Dale Preston
Guest
Posts: n/a
 
      23rd Mar 2005
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:(E-Mail Removed)...
> 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;
> }
> }
>
> }
> }
>
>
>
>



 
Reply With Quote
 
John Grandy
Guest
Posts: n/a
 
      23rd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> 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;
>> }
>> }
>>
>> }
>> }
>>
>>
>>
>>

>
>



 
Reply With Quote
 
John Grandy
Guest
Posts: n/a
 
      23rd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> 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;
>> }
>> }
>>
>> }
>> }
>>
>>
>>
>>

>
>



 
Reply With Quote
 
John Grandy
Guest
Posts: n/a
 
      23rd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> 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;
>> }
>> }
>>
>> }
>> }
>>
>>
>>
>>

>
>



 
Reply With Quote
 
Dale Preston
Guest
Posts: n/a
 
      24th Mar 2005
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:ublpYF$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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:(E-Mail Removed)...
> >> 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;
> >> }
> >> }
> >>
> >> }
> >> }
> >>
> >>
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
Dale Preston
Guest
Posts: n/a
 
      24th Mar 2005
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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:(E-Mail Removed)...
> >> 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;
> >> }
> >> }
> >>
> >> }
> >> }
> >>
> >>
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
John Grandy
Guest
Posts: n/a
 
      24th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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:(E-Mail Removed)...
>> >> 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;
>> >> }
>> >> }
>> >>
>> >> }
>> >> }
>> >>
>> >>
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheriting from class with no public constructor Jack Jackson Microsoft VB .NET 0 20th Jul 2007 07:44 PM
Hiding Default Constructor in VB Express 2005 (Public Not Creatable in VB6) Holger Boskugel Microsoft VB .NET 7 15th Nov 2006 04:16 PM
XMLSerializer: How come some public properties aren't serialized? Carmine Moleti Microsoft Dot NET Compact Framework 1 3rd Oct 2006 08:17 AM
Cannot instantiate type 'DataBoundLiteralControl' because there is no public parameterless constructor. hellosticky@gmail.com Microsoft Dot NET Framework 0 6th Sep 2006 05:21 AM
Overloading a constructor/default constructor =?Utf-8?B?UmljaA==?= Microsoft VB .NET 4 9th Jun 2005 12:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:04 AM.