To consume Document Attachment service, the software developers need to consume the Binary service too. Both services are working tightly together.
This document provides a step-by-step guide to do this using Microsoft Visual Studio 2008 as an example.
Not applicable.
Private binaryClient As New EntityBinary.BinaryClient
Private binaryData As EntityBinary.BinaryData
Private docAttachmentClient As New EntityDocumentAttachment.DocumentAttachmentClient
Private docAttachmentData As EntityDocumentAttachment.DocumentAttachmentData
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
binaryClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
docAttachmentClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.ImpersonationEnd Sub
binaryClient.ChannelFactory.Credentials.Windows.ClientCredential = New System.Net.NetworkCredential("username", "password", "exact-software")
docAttachmentClient.ChannelFactory.Credentials.Windows.ClientCredential = New System.Net.NetworkCredential("username", "password", "exact-software")
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If OpenFileDialog1.FileNames.Count > 0 Then
For Each selectedFilenamePath As String In OpenFileDialog1.FileNames
txtUploadFilePath.Text = selectedFilenamePath
Next
Else
txtUploadFilePath.Text = OpenFileDialog1.FileName
End If
End IfEnd Sub
Private Sub UploadOperation(ByVal operation As String)
Dim chunkSize As Long = 50 * 1024 '50KB
Dim fileInfo As FileInfo = Nothing
Dim fileLength As Integer = 0
'*** Call Binary Service ***
For Each FilePath As String In OpenFileDialog1.FileNames
Try
'Get upload attachment information
fileInfo = New FileInfo(FilePath)
Dim fileSizeInBytes As Long = fileInfo.Length
Using iStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
'Call binary service to create a message header
binaryData = New EntityBinary.BinaryData
binaryData = binaryClient.Create(binaryData)
'Get total size for last packet
Dim lastPacketDataSize As Long = fileSizeInBytes Mod chunkSize
'Total packet
Dim totalChunkPackets As Integer = Int((fileSizeInBytes / chunkSize)) + IIf(lastPacketDataSize > 0, 1, 0)
Dim completePacketData() As Byte = Nothing
completePacketData = New Byte(chunkSize - 1) {}
Dim countPacket As Integer = 0
Dim chunkSequenceNumber As Integer
Dim chunkPacketToWrite() As Byte
Dim dataToRead As Long = iStream.Length
While dataToRead > 0
If countPacket = totalChunkPackets - 1 Then
'Special handle for last packet of data due to the data size could less than 50 KB
Dim lastPacketData() As Byte = Nothing
lastPacketData = New Byte(lastPacketDataSize - 1) {}
fileLength = iStream.Read(lastPacketData, 0, lastPacketDataSize)
chunkPacketToWrite = lastPacketData
'Read chunk data and assign as [complete packet]
fileLength = iStream.Read(completePacketData, 0, chunkSize)
chunkPacketToWrite = completePacketData
'Upload chunk
With binaryData
.Sequence = New EntityBinary.EntityInteger With {.Value = countPacket, .IsDirty = True}
.Data = New EntityBinary.EntityObject With {.Value = chunkPacketToWrite, .IsDirty = True}
End With
binaryData = binaryClient.Save(binaryData)
chunkSequenceNumber = binaryData.Sequence.Value
If Not String.IsNullOrEmpty(chunkSequenceNumber) Then
dataToRead = dataToRead - fileLength
'Count total packet
countPacket += 1
countPacket -= 1
End While
End Using
Catch ex As Exception
MessageBox.Show("Error Occurred :" & ex.Message, "Entity Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'*** Call Document Attachment Service ***
Dim docAttachmentData As New EntityDocumentAttachment.DocumentAttachmentData
With docAttachmentData
If Len(txtDocumentAttachmentID.Text) > 0 Then
.ID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocumentAttachmentID.Text), .IsDirty = True}
.AttachmentFileName = New EntityDocumentAttachment.EntityString With {.Value = Path.GetFileName(fileInfo.FullName), .IsDirty = True}
.AttachmentFileExtension = New EntityDocumentAttachment.EntityString With {.Value = fileInfo.Extension, .IsDirty = True}
.DocumentID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocID.Text), .IsDirty = True}
.MessageID = New EntityDocumentAttachment.EntityGuid With {.Value = binaryData.MessageID.Value, .IsDirty = True}
Select Case operation.ToLower
Case "create"
docAttachmentData = docAttachmentClient.Create(docAttachmentData)
MessageBox.Show("Document Attachment created successfully")
Case "save"
docAttachmentData = docAttachmentClient.Save(docAttachmentData)
MessageBox.Show("Document Attachment saved successfully")
Case "update"
docAttachmentClient.Update(docAttachmentData)
MessageBox.Show("Document Attachment updated successfully")
End Select
'Assign document attachment id return from service
If Not docAttachmentData.ID.Value.Equals(System.Guid.Empty) Then
txtDocumentAttachmentID.Text = docAttachmentData.ID.Value.ToString
'Assign value return from service to respective text box in document attachment information section
txtAttachmentFileName.Text = docAttachmentData.AttachmentFileName.Value
txtAttachmentFileExtension.Text = docAttachmentData.AttachmentFileExtension.Value
txtDocID2.Text = docAttachmentData.DocumentID.Value.ToString
txtVersionNumber.Text = docAttachmentData.VersionNumber.Value
MessageBox.Show("Error Occurred :" & ex.Message, "Entity Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
UploadOperation("Create")End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
UploadOperation("Save")End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
UploadOperation("Update")End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'Existing Document Attachment ID
docAttachmentClient.Delete(docAttachmentData)
MessageBox.Show("Document attachment deleted successfully")
End TryEnd Sub
Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
docAttachmentData = docAttachmentClient.Retrieve(docAttachmentData)
MessageBox.Show("Document attachment retrieved successfully")
Private Sub btnValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate.Click
If Not String.IsNullOrEmpty(txtDocumentAttachmentID.Text) Then
docAttachmentData.ID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocumentAttachmentID.Text), .IsDirty = True}
docAttachmentData.ID = New EntityDocumentAttachment.EntityGuid With {.IsDirty = True, .IsNothing = True}
If Not String.IsNullOrEmpty(txtDocID.Text) Then
docAttachmentData.DocumentID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocID.Text), .IsDirty = True}
docAttachmentData.DocumentID = New EntityDocumentAttachment.EntityGuid With {.IsDirty = True, .IsNothing = True}
If Not String.IsNullOrEmpty(txtAttachmentFileName.Text) Then
docAttachmentData.AttachmentFileName = New EntityDocumentAttachment.EntityString With {.Value = txtAttachmentFileName.Text, .IsDirty = True}
docAttachmentData.AttachmentFileName = New EntityDocumentAttachment.EntityString With {.IsDirty = True, .IsNothing = True}
docAttachmentData = docAttachmentClient.Validate(docAttachmentData)
MessageBox.Show("Document Attachment validated successfully")
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
'Call Document Attachment Service: Retrieve operation with PrepareDownload = True
'Set to DOWNLOAD MODE *** very important flag for download ***
.PrepareDownload = New EntityDocumentAttachment.EntityBoolean With {.Value = True, .IsDirty = True}
'Download path
txtDownloadPath.Text = "C:\" & docAttachmentData.AttachmentFileName.Value
Dim fs As New FileStream(txtDownloadPath.Text, FileMode.OpenOrCreate, FileAccess.Write)
Dim writer As BinaryWriter = New BinaryWriter(fs)
'Call Binary Service:Retrieve to download binary data chunk by chunk
Dim binaryData As New EntityBinary.BinaryData
binaryData.MessageID = New EntityBinary.EntityGuid With {.Value = docAttachmentData.MessageID.Value, .IsDirty = True}
Dim sequence As Integer = 0
binaryData.Sequence = New EntityBinary.EntityInteger With {.Value = sequence, .IsDirty = True}
binaryData = binaryClient.Retrieve(binaryData)
Dim chunk As Byte()
chunk = binaryData.Data.Value
While Not IsNothing(chunk)
writer.Write(chunk, 0, chunk.Length)
writer.Flush()
chunk = Nothing
sequence = sequence + 1
'Check if data is EOF (End Of File)
If IsNothing(binaryData.Data.Value) Then Exit While
writer.Close()
fs.Close()
MessageBox.Show("Document Attachment downloaded successfully")
Exception:
With blank Document ID, click <Validate> button, you will see a general exception information.
Fault Exception:
In order to use fault exception, you need to import <System.ServiceModel> by adding the following line at the beginning of your code: Imports System.ServiceModel
Create a standard fault exception handling function using the code as below:
Private Sub ShowError(ByVal exc As Object)
Dim fe As Object
Select Case True
Case InStr(exc.GetType.ToString, "EntityBinary")
fe = DirectCast(exc, FaultException(Of EntityBinary.EntityFault))
Case InStr(exc.GetType.ToString, "EntityDocumentAttachment")
fe = DirectCast(exc, FaultException(Of EntityDocumentAttachment.EntityFault))
Dim sb As New System.Text.StringBuilder
sb.AppendLine("An error has occured!")
sb.AppendLine("")
sb.AppendFormat("{0}: [{1}]" & vbCrLf, "Exception Type", fe.Code.Name)
sb.AppendFormat("{0}: [{1}]" & vbCrLf, "Exception Message", fe.Message)
If Not IsNothing(fe.Detail.Exceptions) AndAlso UBound(fe.Detail.Exceptions) > -1 Then
For Each info In fe.Detail.Exceptions
With sb
.AppendFormat("{0}: [{1}]" & vbCrLf, "Property", info.PropertyName)
.AppendFormat("{0}: [{1}]" & vbCrLf, "Message", info.Message)
.AppendLine("")
MessageBox.Show(sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
Catch fe As FaultException
ShowError(fe)
With blank Document ID, click <Validate> button, you will see more detailed exception information: