C # code command line works, but winforms code doesn't

I am working on an Adobe Echosign application to demonstrate C # winforms. My code is a direct copy of the command line code (with changes), however my code returns an error after data transfer.

This is the command line code from EchoSign that works

public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient) { FileStream file = getTestPdfFile(fileName); secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1]; fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, file); SenderInfo senderInfo = null; string[] recipients = new string[1]; recipients[0] = recipient; DocumentCreationInfo documentInfo = new DocumentCreationInfo( recipients, testPrefix + Path.GetFileName(file.Name), testMessage, fileInfos, SignatureType.ESIGN, SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED ); if (formFieldLayerTemplateKey != null) { secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1]; formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo(formFieldLayerTemplateKey); documentInfo.formFieldLayerTemplates = formFieldLayerTemplates; } DocumentKey[] documentKeys; documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo); Console.WriteLine("Document key is: " + documentKeys[0].documentKey); } 

This is my code block that returns an error from its system:

 public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient) { try { SenderInfo senderInfo = new SenderInfo(); senderInfo = null; FileStream FileToSign = getTestPdfFile(fileName); byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf"); secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1]; fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo(); fileInfos[0].fileName = fileName; fileInfos[0].mimeType = null; fileInfos[0].file = bytes; RecipientInfo[] docRecipient = new RecipientInfo[1]; docRecipient[0] = new RecipientInfo(); docRecipient[0].email = recipient; DocumentCreationInfo documentInfo = new DocumentCreationInfo(); documentInfo.recipients = docRecipient; documentInfo.name = testPrefix + Path.GetFileName(FileToSign.Name); documentInfo.message = testMessage; documentInfo.fileInfos = fileInfos; documentInfo.signatureType = SignatureType.ESIGN; documentInfo.signatureFlow = SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED; if (formFieldLayerTemplateKey != null) { secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1]; formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo(); formFieldLayerTemplates[0].formKey = formFieldLayerTemplateKey; documentInfo.formFieldLayerTemplates = formFieldLayerTemplates; } EchoSignDocumentService19PortTypeClient ES = new EchoSignDocumentService19PortTypeClient(); DocumentKey[] documentKeys = new DocumentKey[1]; documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo); Console.WriteLine("Document key is: " + documentKeys[0].documentKey); } catch (NullReferenceException ex) { string errMessage = ex.Message; } catch (Exception ex) { string errMessage = ex.Message; } } 

What is the difference between the two blocks of code? The error may be in the FileInfo[] or DocumentCreationInfo() blocks. Perhaps I do not create objects as required by the system.

Any suggestions are welcome.

+5
source share
1 answer

The error seems to be a direct assignment of the bytes of the document you are reading to the variable fileInfos[0].file . The documentation for FileInfo states that the file parameter must be raw file content encoded using base64, but you assign the contents of the raw file without encoding it. When a constructor is called with a file stream, as in the first example (on the command line), the constructor seems to handle this automatically. You can try changing these lines in the Winforms example:

  FileStream FileToSign = getTestPdfFile(fileName); byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf"); secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1]; fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo(); fileInfos[0].fileName = fileName; fileInfos[0].mimeType = null; fileInfos[0].file = bytes; 

into this code and try if this works:

  FileStream FileToSign = getTestPdfFile(fileName); secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1]; fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, FileToSign); 

You should use the provided constructors instead of the direct assignment to ensure that all variables / parameters are processed correctly.

The error you mentioned in your comments about a constructor that does not accept 3 arguments may be the result of the EchoSignTest. prefix EchoSignTest. before calling the constructor, because it seems that this is your own program namespace, not the correct namespace provided by the API.

+1
source

Source: https://habr.com/ru/post/1200940/


All Articles