Quick Start: Invoking a service using MTOM in a .NET project

The following C# code example invokes a process named MyApplication/EncryptDocument from a Microsoft .NET project using MTOM. (See Invoking LiveCycle using MTOM.)

An unsecured PDF document based on a PDF file named loan.pdf is passed to the AEM Forms process using MTOM. The process returns a password-encrypted PDF document that is saved as a PDF file named EncryptedDocument.pdf .

???/** 
    * Ensure that you create a .NET project that uses  
    * MS Visual Studio 2008 and version 3.5 of the .NET 
    * framework. This is required to invoke a  
    * LiveCycle service using MTOM. 
    *  
    * For information, see "Invoking LiveCycle using MTOM" in Programming with AEM forms  
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using EncryptDocumentMTOM.ServiceReference1; 
using System.IO; 
 
//Invoke the EncryptDocument process using MTOM 
namespace EncryptDocumentUsingMTOM 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Specify the name of the PDF file to encrypt 
                   String pdfFile = "C:\\Adobe\loan.pdf"; 
 
                   //Create an EncryptDocumentClient object 
                   MyApplication_EncryptDocumentClient encryptProcess = new MyApplication_EncryptDocumentClient(); 
                   encryptProcess.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://[server]:[port]/soap/services/MyApplication/EncryptDocument?blob=mtom"); 
                   BasicHttpBinding b = (BasicHttpBinding)encryptProcess.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
 
                   //Enable BASIC HTTP authentication 
                   encryptProcess.ClientCredentials.UserName.UserName = "administrator"; 
                   encryptProcess.ClientCredentials.UserName.Password = "password"; 
                   b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
                   b.MaxReceivedMessageSize = 4000000; 
                   b.MaxBufferSize = 4000000; 
                   b.ReaderQuotas.MaxArrayLength = 4000000; 
     
                   //Reference the PDF file to send to the EncryptDocument process 
                   FileStream fs = new FileStream(pdfFile, FileMode.Open); 
 
                   //Create a BLOB object 
                   BLOB inDoc = new BLOB(); 
 
                   //Get the length of the file stream  
                   int len = (int)fs.Length; 
                   byte[] ByteArray = new byte[len]; 
 
                   //Populate the byte array with the contents of the FileStream object 
                   fs.Read(ByteArray, 0, len); 
                   inDoc.MTOM = ByteArray; 
     
                   //Invoke the EncryptDocument short-lived process 
                   BLOB outDoc = encryptProcess.invoke(inDoc); 
                   byte[] encryptDoc = outDoc.MTOM; 
 
                   //Create a new file containing the encrypted PDF document 
                   string FILE_NAME = "C:\\Adobe\EncryptedDocument.pdf"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                   BinaryWriter w = new BinaryWriter(fs2); 
                   w.Write(encryptDoc); 
                   w.Close(); 
                   fs2.Close(); 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
           } 
       } 
} 
Note: Many quick starts that show how to perform LiveCycle service operations include a MTOM code example.

// Ethnio survey code removed