views:

496

answers:

2

I have a web service that's serializing a class (class is from the web service) into an MSMQ, then a windows service is checking the queue and deserializing. The windows service has a web reference to get the class.

If I deserialize inside the web service, everything comes out fine. However, when I deserialize from the windows service everything works except two string arrays that are coming out as null, so I believe something is not getting transferred properly over the web reference.

Here's a snippet from the class in question:

[Serializable, XmlInclude(typeof(EmailType))]
public partial class Email
{
[System.Xml.Serialization.XmlElement("BodyParameters")]
public string[] BodyParameters
{
 get
 {
  return this.bodyParameters;
 }
 set
 {
  this.bodyParameters = value;
 }
}

[System.Xml.Serialization.XmlElement("SubjectParameters")]
public string[] SubjectParameters
{
 get
 {
  return this.subjectParameters;
 }
 set
 {
  this.subjectParameters = value;
 }
}
}

The Reference.cs file I get in my windows service looks like this:

/// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("BodyParameters")]
    public string[] BodyParameters {
        get {
            return this.bodyParametersField;
        }
        set {
            this.bodyParametersField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("SubjectParameters")]
    public string[] SubjectParameters {
        get {
            return this.subjectParametersField;
        }
        set {
            this.subjectParametersField = value;
        }
    }

Is there some special way I have to reference the class or set up the string[]'s in the class for it to be referenced properly?

Here's the output I get if I serialize to a file:

<?xml version="1.0" encoding="utf-8"?>
<Email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" MessageType="None" PackageID="0"     To="[email protected]" Subject="testing..." Body="this is a test" IsHTML="false">
  <BodyParameters>one two</BodyParameters>
  <BodyParameters>three four</BodyParameters>
  <BodyParameters>test test</BodyParameters>
  <SubjectParameters>foo</SubjectParameters>
</Email>

Keep in mind, everything except BodyParameters and SubjectParameters comes out fine in the windows service. Web service, everything works.

A: 

I ran into this years ago when developing a multi-tier app. I hope your situation is the same as mine was, so this will be helpful.

Our setup was that we had one server dedicated to just serving up web services to pass data between all the other components.

Classes were defined in the web service project. Example:

<Serializable()> _
Public Class RetailInformation_StoreInformation
... 
End Class

When we had a client class try to deseriallize the serialized data we couldn't do it. We tried copying the dll that contained the RetailInformation_StoreInformation class into the client applications, but it would just not deserialize.

What we eventually discovered was this..

Say we have a client application named StoreInfoDisplayApp

In the StoreInfoDisplayApp project, we had added a web reference to the web service named RetailInfoService.

We found that we could NOT deserialize RetailInformation_StoreInformation from the dll like this:

Private Function DeSerializeStoreInfo(ByVal path As String) As RetailInformation_StoreInformation
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(RetailInformation_StoreInformation))
        Dim reader As System.IO.Stream = File.OpenRead(path)
        Dim returnvalue As RetailInformation_StoreInformation = CType(ser.Deserialize(reader), RetailInformation_StoreInformation)
        reader.Close()
        Return returnvalue
    End Function

because the compiler (or the runtime - memory is hazy) looked at this as a *StoreInfoDisplayApp.RetailInformation_StoreInformation*

Instead, we had to change all instances of

*RetailInformation_StoreInformation*

to

*RetailInfoService.RetailInformation_StoreInformation*

to specify that the type we were deserializing was the same type served up by the web service. Then it worked like a peach!

David Stratton
Well, I'm just referencing the web service, not a dll. And like I said, other fields are deserialized, just not the string[]'s
Luke
+1  A: 

You need to put [XmlArray] on the SubjectParameters property like this


    [System.Xml.Serialization.XmlArrayAttribute(ElementName="SubjectParameters")]
    public string[] SubjectParameters {
        get {
            return this.subjectParametersField;
        }
        set {
            this.subjectParametersField = value;
        }
    }
Mike Two
I've tried it that way too, with no luck.
Luke