命令行c#代码有效,但winforms代码没有

use*_*962 5 c# echosign

我正在开发一个Adobe Echosign演示C#winforms应用程序.我的代码是他们的命令行代码的直接副本(有修改),但是,我的代码在传输数据后返回错误.

这是命令行代码EchoSign的作品

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);
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码块,它从系统返回错误:

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;
    }
}  
Run Code Online (Sandbox Code Playgroud)

两个代码块之间有什么不同?错误可能位于FileInfo[]DocumentCreationInfo()块中.我可能不是按系统要​​求创建对象.

任何建议表示赞赏.

Mic*_*ter 1

该错误似乎是将您读取的文档的字节直接分配给变量fileInfos[0].file。在FileInfo 的文档中,它指出 file 参数必须是使用 base64 编码的原始文件内容,但您可以分配原始文件内容而不对其进行编码。当像第一个示例(命令行)那样使用文件流调用构造函数时,构造函数似乎会自动处理此问题。您可以尝试在 Winforms 示例中更改这些行:

    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;
Run Code Online (Sandbox Code Playgroud)

进入这段代码并尝试是否有效:

    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);
Run Code Online (Sandbox Code Playgroud)

您应该使用提供的构造函数而不是直接赋值,以确保所有变量/参数都得到正确处理。

您在评论中提到的关于不接受 3 个参数的构造函数的错误可能是EchoSignTest.构造函数调用之前的前缀造成的,因为这似乎是您自己的程序的命名空间,而不是所提供 API 的正确命名空间。