2009-09-17 60 views
4

我有一個Web服務將序列化一個類(類是從Web服務)到MSMQ,然後一個Windows服務正在檢查隊列和反序列化。 Windows服務有一個Web引用來獲得該類。從Web服務引用的C#類不正確反序列化

如果我反序列化Web服務內部,一切都很好。然而,當我從Windows服務反序列化時,除了兩個字符串數組以外,所有工作都是空的,所以我相信某些東西不能通過Web引用正確傳輸。

下面是該類問題的一個片段:

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

的Reference.cs文件我在窗口服務中獲得如下:

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

有我有一些特殊的方法引用類或在類中設置字符串[]以便它被正確地引用?

這裏的輸出我得到的,如果我序列化到一個文件:

<?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> 

請記住,除了BodyParameters和SubjectParameters一切都在窗口服務出精品。 Web服務,一切正常。

+0

建議:刪除'[Serializable接口]'屬性,因爲它不使用XML序列化。然後,序列化到一個文件,併發布文件,以便我們可以看到XML的格式。 – 2009-09-17 17:00:52

+0

另一個建議:幹 - 在項目中使用相同的類庫進行序列化/反序列化。 – jro 2009-09-18 01:29:58

回答

1

我在開發多層應用程序時遇到了這個年頭。我希望你的情況和我的情況一樣,所以這會有所幫助。

我們的設置是我們有一臺服務器專門用於提供Web服務,以在所有其他組件之間傳遞數據。

類在Web服務項目中定義。例如:

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

當我們有一個客戶端類嘗試重新序列化數據時,我們無法做到這一點。我們嘗試將包含RetailInformation_StoreInformation類的dll複製到客戶端應用程序中,但它不會反序列化。

我們最終發現了這個..

假設我們有一個名爲StoreInfoDisplayApp

在StoreInfoDisplayApp項目的客戶機應用程序,我們已經增加了一個Web引用名爲RetailInfoService Web服務。

我們發現,我們無法從這樣的dll反序列化RetailInformation_StoreInformation:

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 

因爲編譯器(或運行時 - 內存是朦朧)搜索這爲StoreInfoDisplayApp。RetailInformation_StoreInformation

相反,我們不得不改變

RetailInformation_StoreInformation的所有實例

RetailInfoService.RetailInformation_StoreInformation

指定我們反序列化的類型與Web服務提供的類型相同。然後它像桃子一樣工作!

+0

那麼,我只是參考了Web服務,而不是一個DLL。就像我說的,其他字段是反序列化的,而不是字符串[]的 – Luke 2009-09-17 20:33:46

1

你需要把[XmlArray]在SubjectParameters財產這樣


    [System.Xml.Serialization.XmlArrayAttribute(ElementName="SubjectParameters")] 
    public string[] SubjectParameters { 
     get { 
      return this.subjectParametersField; 
     } 
     set { 
      this.subjectParametersField = value; 
     } 
    } 
+0

我也試過,也沒有運氣。 – Luke 2009-09-21 19:31:08

相關問題