WCF сериализация base64Binary не кодируется правильно - PullRequest
0 голосов
/ 31 мая 2011

Привет!

Итак, у меня есть базовый класс, который определяет свойство base64Binary, которое будет возвращено как внедренный файл в XML. Я не получаю никаких ошибок, однако возвращаемая строка не кодируется как base64. Есть идеи? Я включил базовый класс, а также код, где он вызывается. Любая помощь будет значительно высоко ценится !!

'--------------------------------------------------
'BinaryObjectType type
'--------------------------------------------------
<XmlType(TypeName:="BinaryObjectType",Namespace:=Declarations.SchemaVersion),Serializable, _
EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Class BinaryObjectType
    '*********************** format attribute ***********************
    <XmlAttribute(AttributeName:="format", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __format As String

    <XmlIgnore()> _
    Public Property format As String
        Get
            format = __format
        End Get
        Set(ByVal Value As String)
            __format = Value
        End Set
    End Property

    '*********************** mimeCode attribute ***********************
    <XmlAttribute(AttributeName:="mimeCode", Form:=XmlSchemaForm.Unqualified, Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __mimeCode As String

    <XmlIgnore()> _
    Public Property mimeCode As String
        Get
            mimeCode = __mimeCode
        End Get
        Set(ByVal Value As String)
            __mimeCode = Value
        End Set
    End Property

    '*********************** encodingCode attribute ***********************
    <XmlAttribute(AttributeName:="encodingCode", Form:=XmlSchemaForm.Unqualified, DataType:="normalizedString", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __encodingCode As String

    <XmlIgnore()> _
    Public Property encodingCode As String
        Get
            encodingCode = __encodingCode
        End Get
        Set(ByVal Value As String)
            __encodingCode = Value
        End Set
    End Property

    '*********************** characterSetCode attribute ***********************
    <XmlAttribute(AttributeName:="characterSetCode", Form:=XmlSchemaForm.Unqualified, DataType:="normalizedString", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __characterSetCode As String

    <XmlIgnore()> _
    Public Property characterSetCode As String
        Get
            characterSetCode = __characterSetCode
        End Get
        Set(ByVal Value As String)
            __characterSetCode = Value
        End Set
    End Property

    '*********************** uri attribute ***********************
    <XmlAttribute(AttributeName:="uri", Form:=XmlSchemaForm.Unqualified, DataType:="anyURI", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __uri As String

    <XmlIgnore()> _
    Public Property uri As String
        Get
            uri = __uri
        End Get
        Set(ByVal Value As String)
            __uri = Value
        End Set
    End Property

    '*********************** filename attribute ***********************
    <XmlAttribute(AttributeName:="filename", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __filename As String

    <XmlIgnore()> _
    Public Property filename As String
        Get
            filename = __filename
        End Get
        Set(ByVal Value As String)
            __filename = Value
        End Set
    End Property

    '*********************** XmlText field ***********************
    <XmlText(DataType:="base64Binary"), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __Value As Byte()

    <XmlIgnore()> _
    Public Property Value() As Byte()
        Get
            Value = __Value
        End Get
        Set(ByVal val As Byte())
            __Value = val
        End Set
    End Property

    '*********************** Constructor ***********************
    Public Sub New()
    End Sub
End Class

Вот фрагмент, где он строится:

Dim binFile As New BinaryObjectType

Select Case Trim(UCase(objFromDB.FileFormat))
    Case "PDF"
        binFile.mimeCode = "application/pdf"
    Case "DOC"
        binFile.mimeCode = "application/ms-word"
    Case "DOCX"
        binFile.mimeCode = "application/ms-word"
    Case "GIF"
        binFile.mimeCode = "image/gif"
    Case "JPG"
        binFile.mimeCode = "image/jpeg"
    Case "JPEG"
        binFile.mimeCode = "image/jpeg"
    Case "PNG"
        binFile.mimeCode = "image/png"
    Case Else
        binFile.mimeCode = "text/html"
    End Select

    binFile.Value = objFile
    attachmentArea.EmbeddedData = binFile
    attachmentAreaList.Add(attachmentArea)
    candidateProfile.AttachmentCollection = attachmentAreaList        
    candidateProfileList.Add(candidateProfile)        
    candidateArea.CandidateProfileCollection = candidateProfileList        
    candidateAreaList.Add(candidateArea)        
    dataArea.Show = showArea        
    dataArea.CandidateCollection = candidateAreaList        
    showResponse.DataArea = dataArea

    Return showResponse

Я не получаю ошибок и получаю результат для binFile.Value, но он не кодируется в base64.

Есть идеи? Заранее спасибо !!!

1 Ответ

0 голосов
/ 31 мая 2011

Как вы видите, что строка не закодирована в base64? Я попробовал подобный сценарий (см. Ниже), и Byte () был правильно закодирован в base-64.

Public Class StackOverflow_6186859_751090

    Public Class Declarations
        Public Const SchemaVersion As String = "http://my.namespace"
    End Class

    'BinaryObjectType type 
    '-------------------------------------------------- 
    <XmlType(TypeName:="BinaryObjectType", Namespace:=Declarations.SchemaVersion), Serializable(), _
     EditorBrowsable(EditorBrowsableState.Advanced)> _
     Public Class BinaryObjectType
        <XmlAttribute(AttributeName:="format", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
             EditorBrowsable(EditorBrowsableState.Advanced)> _
             Public __format As String
        <XmlIgnore()> _
        Public Property format() As String
            Get
                format = __format
            End Get
            Set(ByVal Value As String)
                __format = Value
            End Set
        End Property

        <XmlAttribute(AttributeName:="mimeCode", Form:=XmlSchemaForm.Unqualified, Namespace:=Declarations.SchemaVersion), _
         EditorBrowsable(EditorBrowsableState.Advanced)> _
        Public __mimeCode As String
        <XmlIgnore()> _
        Public Property mimeCode() As String
            Get
                mimeCode = __mimeCode
            End Get
            Set(ByVal Value As String)
                __mimeCode = Value
            End Set
        End Property

        <XmlText(DataType:="base64Binary"), _
         EditorBrowsable(EditorBrowsableState.Advanced)> _
        Public __Value As Byte()
        <XmlIgnore()> _
        Public Property Value() As Byte()
            Get
                Value = __Value
            End Get
            Set(ByVal val As Byte())
                __Value = val
            End Set
        End Property

    End Class

    Public Shared Sub Test()
        Dim ms As MemoryStream = New MemoryStream()
        Dim ser As XmlSerializer = New XmlSerializer(GetType(BinaryObjectType))
        Dim obj As BinaryObjectType = New BinaryObjectType()
        obj.format = "PNG"
        obj.mimeCode = "image/png"
        obj.Value = Encoding.UTF8.GetBytes("Some random text which will be encoded as bytes")
        ser.Serialize(ms, obj)
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()))
    End Sub
End Class
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...